问题
I am working on a script to query remote servers for configuration information and return the data for output to gridview. The issue I am having is the format in which the data is returned.
If I do...
((Get-CimInstance -ClassName Win32_Processor).NumberOfLogicalProcessors | Measure-Object).Count
((Get-CimInstance -ClassName Win32_PhysicalMemory).Capacity | Measure-Object -Sum).Sum /1GB
in a ps1 and run that on a remote server using Invoke-Command, I get an array back with only numbers like this.
2
4
How can I gather this information with context from multiple machines efficiently? I tried remote jobs, but I can't really run more than 2 jobs at a time. I would like to spread out the work to all of the target servers like this.
回答1:
Those values actually have the computer name e.g.:
30# $r = Invoke-Command ...
31# $r[0]
4
32# $r[0].PSComputerName
hillr2
PowerShell just doesn't display that info by default for number. You could do this:
32# icm hillr2 -ScriptBlock {
>>> $NumLogCpu = (Get-CimInstance Win32_Processor).NumberOfLogicalProcessors
>>> $MemSize = ((Get-CimInstance Win32_PhysicalMemory).Capacity | Measure-Object -Sum).Sum /1GB
>>> [pscustomobject]@{NumLogCpu=$NumLogCpu;MemSize=$MemSize}
>>> }
NumLogCpu : 4
MemSize : 8.00390625
PSComputerName : hillr2
RunspaceId : fb03fedd-2771-46cf-916a-f31ec7c8298b
This requires PowerShell v3 or higher for the [pscustomobject] type accelerator.
来源:https://stackoverflow.com/questions/23664595/invoke-command-returning-data