while loop - Powershell Multijob script. Make file open when job completes instead of waiting until all jobs complete -
i'm writing script ping multiple sites , open file show results. results open finish. instead waits until jobs finished before opening files. i've had issues open of files. appreciated
$count = 500 $sites = "www.google.com","8.8.8.8","127.0.0.1" foreach ($site in $sites) { remove-item "c:\wfsupport\self service tool\$site.txt" start-job -name $site -scriptblock { param ($count,$site) ping -n $count $site } -argumentlist $count, $site } while ((get-job).state -match 'running') { foreach ($job in get-job | {$_.hasmoredata}) { $jobname = $job.name receive-job $job | out-file -encoding ascii -append "c:\wfsupport\self service tool\$jobname.txt" } start-sleep -seconds 10 } while ((get-job).state -match 'completed') { foreach ($job in get-job | {$_.hasmoredata}) { $jobname = $job.name receive-job $job | out-file -encoding ascii -append "c:\wfsupport\self service tool\$jobname.txt" invoke-item "c:\wfsupport\self service tool\$jobname.txt" } get-job | remove-job }
this because while loop checking 'running' doesn't stop until jobs stopped running. none of code below run until while loop finishes.
while ((get-job).state -match 'running') { foreach ($job in get-job | {$_.hasmoredata}) { $jobname = $job.name receive-job $job | out-file -encoding ascii -append "c:\wfsupport\self service tool\$jobname.txt" if ($job.state -like 'completed'){ invoke-item "c:\wfsupport\self service tool\$jobname.txt" $job | remove-job } } start-sleep -seconds 10 }
Comments
Post a Comment