Invoke-Command returning data

亡梦爱人 提交于 2019-12-24 10:31:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!