Invoke-Command parallel run with multiple get-commandlet info

Hello,

I am trying to ping/get basic computer information from remove computers. I currently use foreach loop with CIMSession, but, it takes lot of time when most of the systems are offline. Invoke-Command works great when I run it to get single instance of service etc. So, what would be the best process to get the following information using invoke-command?

Invoke-command -computername (gc .\pc.txt) -scriptblock{Win32_operatingsystem, win32_networkconfigurationadapter, win32_operatingsystem} -throttlelimit 100 -ErrorAction stop

It would be great if you can suggest alternate way of doing it. At any point, I’m looking into 2000-4000 systems and foreach loops dont seem to help much

 

Frankly, with that many systems it seems like your biggest problem isn’t the efficiency of a foreach loop, but rather how to display the collected information in a useful, accessible way. Are you trying to check them all at once? Are they broken down into domains or vlans or anything where you could handle them in smaller groups?

With a network of that size, I seriously doubt that PowerShell is your best option for basic management tasks like ping checks and getting the OS version. You should probably be investing in some proper network management software.

You can use Get-CimInstance with -ComputerName. ComuterName parameter accepts array of computer, since you have multiple CIM/WMI classes to use, I suggest Invoke-Command (with -AsJob for better performance). And use Get-CimInstance -Class Win32_OperatingSystem…

$ComputerList = Get-Content -Path c:\ServerList.txt
Invoke-Command -AsJob -ComputerName $ComputerList -ScriptBlock {
$OperatingSystem = Get-CimInstance -Class Win32_OperatingSystem 
$Network = Get-CimInstance -Class win32_networkadapterconfiguration
# more classes ... 
}

Thanks for your comments. When I use one command

So this works for me

$ComputerList = gc -path .\txt
try{
$OS = Invoke-command -computername $computerlist -Scriptblock {Get-CImInstance Win32_operatingSystem} -ErrorAction Stop
}catch{write-output "$_ not online}

When I try the command you mentioned, I keep getting connection errors. My goal is to generate a hash table and output the value to a csv. Here is what I wanted to do. Not sure where it is not working.

$ComputerList = Get-Content -Path c:\ServerList.txt
try{
Invoke-Command -AsJob -ComputerName $ComputerList -ScriptBlock {
$OperatingSystem = Get-CimInstance -Class Win32_OperatingSystem 
$Network = Get-CimInstance -Class win32_networkadapterconfiguration
$obj = @{
Computername = $_
IPAddress=$Network.IPAddress
OS = $OperatingSystem.Caption
}
}catch{write-verbose "$_ not online"
}finally{
$Out = New-Object -TypeName PSObject -Property $Obj
write-Output $out
}

You can use what is working for you, just add more code in the script block based on the requirement. -AsJob is not mandatory.