I have written powershell script to split one file (Containing hostnames) into ten files, perform ping (i.e. powershell test-connection command) to hostnames in each file. To speed up the process, i used start-job command to process each file hostnames. If I remove the start-job command the scripts works just fine, as desired. But when i use the start-job command, the array used inside the "scriptblock" does not return the desired output. Instead, it remains empty. Following is the partial script (for only one file processing). Please guide, what Am I missing here?
Start-Job -Name "Ping-Part-1" -ScriptBlock {
foreach ($Comp1 in $file_part1_content) {
If (test-Connection -ComputerName $Comp1 -Count 1 -Quiet) {
$project1 = Create-New-Object
$project1.AliveStatus = "Alive"
$project1.Hostname = $Comp1
$resultsarray_file1 += $project1
}
Else {
$project1 = Create-New-Object
$project1.AliveStatus = "Not Alive"
$project1.Hostname = $Comp1
$resultsarray_file1 += $project1
}
}
}
Create-New-Object
? there's no such thing – 4c74356b41Start-Job
works by creating new process with separateRunspace
with separate variables ($resultsarray_file1
in particular). – user4003407