Reputation: 26170
I'm trying to get uptime of 2000+ computers.
As winrm isn't configure on these computers I cannot use invoke-command -computername $computers.
So I tried to use start-job
to speed up things but start-job doesnt come with a throttleLimit
parameter as invoke-command. So my script fires large amount of powershell.exe until it kills my memory... Is there a way to limit the concurrent jobs?
this is what I've got now:
$jobs=@()
Get-QADComputer -SearchRoot $OU -SizeLimit 3000 |%{
$jobs+= Start-Job -ArgumentList $_.name -ScriptBlock {(param $cn)
if (Test-Connection -Quiet $cn){
$lastboottime=(Gwmi -computername $cn -Class Win32_OperatingSystem).lastbootuptime
$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)
$cn +" "+$sysuptime.days
}
}
}
$jobs|%{ Wait-Job $_ -Timeout 30 |Receive-Job ;Remove-Job $_}
Upvotes: 1
Views: 1284
Reputation: 28194
While this is a viable way to do it, you don't have to use jobs at all. Get-WMIObject
takes a String[]
for the ComputerName
parameter, and if multiple computernames are passed, it will poll multiple machines simultaneously (I think up to 32, but I don't recall exactly) & return an extra field in the results, PSComputerName
You can do this much more simply.
$servernames = @();
$servernames += get-qadcomputer -searchroot $ou|select name|%{if(test-connection -quiet $_.name) {$_.name}}
get-wmiobject -computername $servernames win32_operatingsystem|select PSComputername,LastBootTime
then calculate your uptime from there for each computer.
Edit (Kayasax) : here is the final recipe :
$alive=@()
$obj=@()
Get-QADComputer -SearchRoot $ou -SizeLimit 4000 |select -ExpandProperty name |foreach-object {
if (Test-Connection -Quiet -count 1 $_){ $alive+=$_ }
}
Get-wmiobject -computername $alive -Class Win32_OperatingSystem |select PSComputerName, lastBootUpTime |foreach-object{
$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($_.lastBootUpTime)
$props=@{"name"=$_.PSComputername;"uptime"=$sysuptime.days}
$obj+= new-Object -TypeName PSCustomObject -property $props
}
$obj |sort-object uptime -desc
Upvotes: 2