Welcome › Forums › General PowerShell Q&A › How to get multiple values from remote machine (Invoke-Command)
- This topic has 2 replies, 2 voices, and was last updated 4 months, 1 week ago by
Participant.
-
AuthorPosts
-
-
July 15, 2020 at 4:25 pm #242735
Hello,
I try to make script to inventory my computers in LAN. I wrote script to get processor, RAM and HDD values:
PowerShell12345678910$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
-
This topic was modified 6 months, 2 weeks ago by
kolaborek08. Reason: Text formatting
-
This topic was modified 6 months, 2 weeks ago by
-
September 21, 2020 at 6:25 pm #257918
Hello kolaborek08,
Inside your Script block you need to create PSCustom object and return it.
PowerShell12345678910111213$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)/ 1GBHDD=$(Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | Select-Object -ExpandProperty Size) / 1GB;}$InventoryObjectRight now you are returning only remote computer name.
Hope that helps.
-
This reply was modified 4 months, 1 week ago by
AndySvints.
-
This reply was modified 4 months, 1 week ago by
AndySvints.
-
This reply was modified 4 months ago by
grokkit.
-
This reply was modified 4 months, 1 week ago by
-
September 22, 2020 at 10:52 am #258101PowerShell123456789101112131415$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 = $procRAM = $ram = "{0:n2} GB" -f ($ram /1GB)HDD = $hdd | ForEach-Object {"{0:n2} GB" -f ($_ /1GB)}}}
-
-
AuthorPosts
- The topic ‘How to get multiple values from remote machine (Invoke-Command)’ is closed to new replies.