Search code examples
powershellinvoke-command

Get value from invoke-command


This code returns the value of the registry from a remote machine. How to format output as a table with two columns containing information such as ComputerName and RegistryValue. Note: RunspaceId doesn't not contain RegistryValue information.

$comps = "smzmi0020d", "smzmi0025d"
$block = { (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate' -name susclientid).susclientid }

PS C:\Windows\system32> invoke-command $comps -ScriptBlock $block | select susclientID, PSComputername

susclientID PSComputerName
----------- --------------
            smzmi0020d
            smzmi0025d

PS C:\Windows\system32> invoke-command $comps -ScriptBlock $block | select *

PSComputerName RunspaceId                           Length
-------------- ----------                           ------
smzmi0020d     1be1be2f-7e57-4833-81a0-6182c5ac3672     36
smzmi0025d     10e067da-06a0-4962-ae89-898aef9fe81c     36

Solution

  • I am not being able to reproduce in my environment, but you may try something like:

    $Comps = "smzmi0020d", "smzmi0025d"
    
    Invoke-Command -ComputerName $Comps -ScriptBlock {(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate' -name susclientid)} | Select-Object -Property SusClientID, PSComputerName
    

    Note: Foreach-Object processes the objects sequentially, while Invoke-Command does that in parallel (32 computers simultaneously by default). Therefore it should be much faster!

    Invoke-Command also returuns the parameter PSComputerName which holds the name of the computer retunrning the output, so with Select-Object at the end you should be just fine. Eventually you may pipe the output to | Format-Table -AutoSize.