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

Frederick Arendorff 41 Reputation points
2021-05-17T12:55:10.55+00:00

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 for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,401 Reputation points
    2021-05-17T17:59:42.75+00:00

    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  
              }  
          }    
    }  
      
      
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.