How to get multiple values from remote machine (Invoke-Command)

Hello,

I try to make script to inventory my computers in LAN. I wrote script to get processor, RAM and HDD values:

$computerNames = ("computer1", "computer2", "computer3")
$sessions = New-PSSession -ComputerName $computerNames -Credential (Get-Credential)
$eeee = Invoke-Command -Session $sessions -ScriptBlock {
    &{
        $env:computername; 
        Get-CimInstance -ClassName Win32_Processor | Select-Object @{Name="Procesor"; Expression={$_.Name}};
        Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object @{Name="RAM"; Expression={$_.TotalPhysicalMemory / 1GB}};
        Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | Select-Object @{Name="HDD"; Expression={$_.Size / 1GB}};
    }
}

But in result I don’t have all values what I want but only computer name, one value and RunSpaceID.

Procesor PSComputerName RunspaceId -------- -------------- ---------- Processor_name computer1 some_Runspace Processor_name computer2 some_Runspace Processor_name computer3 some_Runspace If I’ll comment line responsible for getting processor value from computer then RAM value will appear. The same is with HDD. But how can I make to get all this values at one time? Best Regards Daniel

Hello kolaborek08,

Inside your Script block you need to create PSCustom object and return it.

$InventoryObject=[PSCustom Object]@{

ComputerName = $env:computername;

Procesor = $(Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name);

RAM=$(Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty TotalPhysicalMemory)/ 1GB

HDD=$(Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | Select-Object -ExpandProperty Size) / 1GB;

}

$InventoryObject

Reference: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7

Right now you are returning only remote computer name.

Hope that helps.

$computerNames = ("computer1", "computer2", "computer3")
$sessions = New-PSSession -ComputerName $computerNames -Credential (Get-Credential)

$result = Invoke-Command -Session $sessions -ScriptBlock {    
    $proc = (Get-CimInstance -ClassName Win32_Processor).Name
    $ram = (Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory
    $hdd = (Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3").Size
 
# hard drive and ram sizes will be listed with 2 decimal places
    [PSCustomObject]@{
        Processor = $proc
        RAM = $ram = "{0:n2} GB" -f ($ram /1GB)
        HDD = $hdd | ForEach-Object {"{0:n2} GB" -f ($_ /1GB)}   
    }
}