question

FrederickArendorff avatar image
0 Votes"
FrederickArendorff asked FrederickArendorff commented

Local registry values returned by Get-Childitem when invoked on remote computer

Hi

Probably a silly question and a straightforward answer, but it confuses me a bit.
It seems to me the command below retrieves the remote keys and items, but the values of these are from the local machine. Why is that?

  • Invoke-Command -ComputerName Computer01 -ScriptBlock {Get-ChildItem -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v4'}

  • Invoke-Command -ComputerName Computer01 -ScriptBlock {Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4'}

Regards
Frederick







windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered FrederickArendorff commented

Probably because the properties are not evaluated until after the invoke-command has completed and the PSPath of the object points to a "local" name.

 cls
 "This demonstrates the 'Problem'"
 Invoke-Command -ComputerName test10b -ScriptBlock {
     "This portion of the script is running on {0}" -f $env:COMPUTERNAME
     "Here is reg.exe"
     reg.exe query HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName /s
        
     "Here is the computername from the registry"
     Get-ChildItem 'HKLM:\System\CurrentControlSet\Control\ComputerName' 
 }


97281-capture.jpg


 cls
 "This shows one way to enumerate the properties and values."
 Invoke-Command -ComputerName test10b -ScriptBlock {
     "This portion of the script is running on {0}" -f $env:COMPUTERNAME
     "Here is reg.exe"
     reg.exe query HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName /s
     "Here is Get-Childitem"
     $Keys = Get-ChildItem HKLM:\System\CurrentControlSet\Control\ComputerName
     foreach ($k in $keys){
           $k.Name
           $props = (Get-Item -Path $k.pspath).property
           foreach ($p in $props) {
              $v = Get-ItemPropertyvalue -Path $k.pspath -name  $p        
              "   {0} - {1}" -f $p , $v
           }
       }  
 }


97200-capture1.jpg


To read the DotNet values I had to add -recurse.

 cls
 "DotNet reg key with multiple subkeys" 
 Invoke-Command -ComputerName test10b -ScriptBlock {
     "This portion of the script is running on {0}" -f $env:COMPUTERNAME
     "Here is reg.exe"
     reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4" /s
     "Here is Get-Childitem"
     $Keys = Get-ChildItem -recurse "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4"
     foreach ($k in $keys){
           $k.Name
           $props = (Get-Item -Path $k.pspath).property
           foreach ($p in $props) {
              $v = Get-ItemPropertyvalue -Path $k.pspath -name  $p        
              "   {0} - {1}" -f $p , $v
           }
       }  
 }




capture.jpg (155.6 KiB)
capture1.jpg (117.6 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi MotoX80

Thanks for a very thoroughly and elaborated answer.

Best Regards
Frederick

0 Votes 0 ·