ksh script not working

Here's the script:

#!/usr/bin/ksh
Date=`date +%m%d%y`
CDate=`date`
FileName=cintas_hosts_and_users.$Date
echo $CDate >> $FileName
#echo $FileName
for host in `cat /collect/itthomp/cintas_hostnames.dat` 
do
echo $host  >> $FileName
ssh $host "awk -v Fname=$FileName -F: '{if($1 != "root" && $1 != "daemon" && $1 != "bin" && $1 != "sys" && $1 != "adm" && $1 != "uucp" && $1 != "guest" && $1 != "nobody" && $1 != "lpd" && $1 != "lp" && $1 != "invscout" && $1 != "snapp" && $1 != "ipsec" && $1 != "nuucp" && $1 != "pconsole" && $1 != "sshd" && $1 != "idsldap" && $1 != "sapadm")print $1 "\t" $5 >> Fname}' /etc/passwd"
done

Here's the error I get:

awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
Syntax Error The source line is 1.
The error context is
                {if( >>>  != <<<

I'm not very experienced with scripting and this is my first attempt at using awk within a ksh script. Any help please would be appreciated. Thanks...

The entire awk script is enclosed by double quotes. $1 is expanded by the local shell before it even executes ssh.

Regards and welcome to the forum,
Alister

Is there any way I can fix this or do I need to take another approach to the solution?

Thanks for the reply...

As alister mentioned the variables got expanded by the shell even before it executes ssh.

To avoid this happening, you have to escape them as highlighted below.

You can also simplify your awk program using arrays:

ssh -q $host "
awk -F':' '
        BEGIN {
                n = split ( \"root:daemon:bin:sys:adm:uucp:guest:nobody:lpd:lp:invscout:snapp:ipsec:nuucp:pconsole:sshd:idsldap:sapadm\", A )
                for ( i = 1; i <= n; i++ )
                        U[A]
        }
        !(\$1 in U) {
                print \$1, \$5
        }
' OFS='\t' /etc/passwd"