Hello,
I liked very much the original bgping script (http://poshtips.com/bgping-a-high-performance-bulk-ping-utility/)
I thought, it would be nice to modify the script for query multiple servers (for example, get some information from them, like Serial Number, asap).
So..
Ive cut the lines i didnt need (IP address checking, results etc.. ) and left only JOB related lines..
Param ([int]$BatchSize=3)
#list of servers
$source = Get-Content .\servers_FQDN.txt
#scriptblock
$blok = {gwmi win32_bios |select serialnumber}
$elapsedTime = [system.diagnostics.stopwatch]::StartNew()
$result = @()
$itemCount = 0
## checking running jobs
if (get-job|? {$_.name -like "Script*"}){
write-host "ERROR: There are pending background jobs in this session:" -back red -fore white
get-job |? {$_.name -like "Script*"} | out-host
write-host "REQUIRED ACTION: Remove the jobs and restart this script" -back black -fore yellow
$yn = read-host "Automatically remove jobs now?"
if ($yn -eq "y"){
get-job|? {$_.name -like "Script*"}|% {remove-job $_}
write-host "jobs have been removed; please restart the script" -back black -fore green
}
exit
}
$offset = 0
## measure object
$itemCount = $source.count
Write-Host "Script will run against $itemcount servers!"
## Script start time mark
write-host " Script started at $(get-date -Format ("yyyy/MM/dd hh:mm:ss")) ".padright(100) -back darkgreen -fore white
write-host " (contains $itemCount unique entries)" -back black -fore green
$activeJobCount = 0
$totalJobCount = 0
write-host "Submitting background jobs..." -back black -fore yellow
## end of clean, verified code
for ($offset=0; $offset -le $itemCount;$offset += $batchSize){
$activeJobCount += 1; $totalJobCount += 1; $HostList = @()
$HostList += $source |select dnshostname |sort |get-unique |select -skip $offset -first $batchsize
$j = invoke-command -computername $($source) -scriptblock $blok -asjob
$j.name = "Script`:$totalJobCount`:$($offset+1)`:$($HostList.count)"
write-host "+" -back black -fore cyan -nonewline
}
write-host "`n$totaljobCount jobs submitted, checking for completed jobs..." -back black -fore yellow
$recCnt = 0
while (get-job |? {$_.name -like "Script*"}){
foreach ($j in get-job | ? {$_.name -like "Script*"}){
$temp = @()
if ($j.state -eq "completed"){
$temp = @()
$temp += receive-job $j
$result += $temp
remove-job $j
$ActiveJobCount -= 1
write-host "-" -back black -fore cyan -nonewline
}
}
if ($result.count -lt $itemCount){
sleep 3
}
}
write-host " "
write-host " Script finished at $(get-date) ".padright(60) -back darkgreen -fore white
write-host (" Number of hosts : {0}" -f $($result.count)) -back black -fore green
write-host (" Elapsed Time : {0}" -f $($ElapsedTime.Elapsed.ToString())) -back black -fore green
write-host "TEMP variable: $temp"
$result
write-host " Script completed all requested operations at $(get-date -Format ("yyyy/MM/dd hh:mm:ss")) ".padright(60) -back darkgreen -fore white
write-host (" Elapsed Time : {0}" -f $($ElapsedTime.Elapsed.ToString())) -back black -fore greenThere is a bug i didnt discovered yet, the $result table containts $temp*number_of_jobs ... why?
If i run this script against 10servers, divided by 2 (batchsize 2), it would create 5 background jobs. While $temp contains serial number from every single server, $result will multiply it * 5.
I can use $temp as result as well, but original author used $result for a reason ...
I think the problem starts with line
foreach($jinget-job|?{$_.name-like"Script*"}){
... can u correct this script, so i will get results only 1 time? In case of 10 servers with batchsize of 2, it will create 5jobs containing 2 queries ...
Elapsed time is now ~24sec, i think it should be done in around 6..