I'm using the following script to try and ping a list of computers stored in a CSV. The desired outcome is that I'll be able to verify the timeout condition.
$servers = ipcsv "\\testbox\c$\Tools\PAL Stuff\servers.csv"
$timeoutSeconds = 3
$code = {
foreach($server in $servers) {
$name = $server.name
if($ping = Test-Connection -ComputerName $($args[0]) -BufferSize 16 -quiet -count 30) {
write-host "This should be 30 pings."
} else {
write-host "This should display when the job times out"
}
}
} -ArgumentList $name
$j = Start-Job -ScriptBlock $code
if (Wait-Job $j -Timeout $timeoutSeconds) { Receive-Job $j }
Remove-Job -force $j
This script results in an error:
You must provide a value expression on the right-hand side of the '-' operator."
If I move the Argument List to here:
$j = Start-Job -ScriptBlock $code -ScriptBlock $code
I get an error that it
could not validate argument on parameter 'ComputerName'. The argument is null or empty.
I've seen articles and questions on passing variables to the scriptblock, but they're for Invoke-Command
and not Start-Job
, and I have yet to get it work in translation. I'm under the impression that if it worked, I wouldn't see the output that I'm expecting, which is the Write-Host
.