1

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

OK nice, I hadn't thought of that. I tried the psobject route, but the issue I have though is getting the information (psobject) back from the script. So I run the script using $x = Invoke-Command .\script.ps1 and I can recover the data from the queries using $x (array of "2" and "4") but I can't seem to get the psobject itself back. Do I need to return it?
I would love to get [pscustomobject]@{ComputerName=$CompNm;NumLogCpu=$NumLogCpu;MemSize=$MemSize} back as an object so I can "stack" them up for gridview.
Never mind, I just got it. I needed to assign a variable to the pscustomobject and repeat it at the end of the script to get it back as $x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.