job script not working when using variable

hello

i have a job  script that work perfectly when the sourceparth is not a variable in the copy-item like this,  

copy-item c:\source "\\$ip\c$" -Recurse , but when i set $sourcepath="c:\$folder", the job does not start any more

#begin script

$folder= read-host "enter source folder name"
$sourcepath="c:\$folder"
$jobs = foreach ($ip in gc C:\cmp.txt) {
    Start-Job -Name "Copyjob to $ip" -ArgumentList $ip -ScriptBlock {
      param($ip)

      if (Test-Connection -ComputerName $ip -Count 1 -Quiet)
      {
        copy-item $sourcePath "\\$ip\c$" -Recurse
      }
   }
}
$jobs | Wait-Job

any idea...?

thanks

April 30th, 2015 3:04am

Hi

you have to pass $sourcePath in ArgumentList, as you did for $ip, for the same reason : your job has his own scope, wich is different from the calling scope.

hope it helps

  • Proposed as answer by jrv 21 hours 56 minutes ago
  • Marked as answer by zizou 21 hours 45 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 30th, 2015 3:19am

thank you
April 30th, 2015 5:25am

Hi

you have to pass $sourcePath in ArgumentList, as you did for $ip, for the same reason : your job has his own scope, wich is different from the calling scope.

hope it helps

  • Proposed as answer by jrv Thursday, April 30, 2015 9:11 AM
  • Marked as answer by zizou Thursday, April 30, 2015 9:23 AM
Free Windows Admin Tool Kit Click here and download it now
April 30th, 2015 7:17am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics