Connecting to HKU on a remote computer

Welcome Forums General PowerShell Q&A Connecting to HKU on a remote computer

Viewing 8 reply threads
  • Author
    Posts
    • #36344
      Participant
      Topics: 3
      Replies: 10
      Points: 0
      Rank: Member

      I am trying to connect to HKU on a remote computer.

      I have tried

      Invoke-Command -ComputerName SomeRemoteComputer -ScriptBlock {New-PSDrive -Name HKU -PSProvider registry -root Registry::HKEY_USERS}
      

      It appears to work when I attempt to connect to the new psdrive or look at the psdrive output it does not appear

      Am I trying to do something that is not supported ?

    • #36347
      Participant
      Topics: 1
      Replies: 169
      Points: 0
      Rank: Member

      Your code will create the psdrive on the remote computer not your local pc. If you want to use the psdrive the actions would need to be in your script block. Or is that what you are doing and getting no output?

    • #36348
      Participant
      Topics: 3
      Replies: 10
      Points: 0
      Rank: Member

      I checked the psdrive of the remote computer it did not show. there and the code returns error
      “Cannot find drive. A drive with the name ‘HKU’ does not exist.
      + CategoryInfo : ObjectNotFound: (HKU:String) [Get-ItemProperty], DriveNotFoundException
      + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
      + PSComputerName : SomeRemoteComputer

      When the results of the original script are run the provider does not show Registry
      When i run the same command locally the provider shows Registry

      So the desired result is to use get-itemproperty like the following example

       
      Invoke-Command -ComputerName SomeRemoteComputer -ScriptBlock {Get-ItemProperty 'HKU:\S*\Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing' |select -ExpandProperty pspath}
      
      
    • #36350
      Participant
      Topics: 8
      Replies: 38
      Points: 23
      Rank: Member

      New-PSDrive -name HKU -PSProvider Registry -Root HKEY_USERS

      edit: I just tried your original code and it works for me too, so something else must be up.

    • #36356
      Participant
      Topics: 3
      Replies: 10
      Points: 0
      Rank: Member

      If I enter-pssession SomeRemoteComputer the new-psdrive works. That is not ideal as psremoting is not enabled on each of my assets that I am attempting to reach.

    • #36381
      Keymaster
      Topics: 18
      Replies: 4872
      Points: 1,903
      Helping HandTeam Member
      Rank: Community Hero

      So, let’s be clear on how Invoke-Command works. Once it finishes running the command, it disconnects because you’re using an ad-hoc session. So it’s creating the PSDrive, and then closing the session, which removes the PSDrive.

      Registry PSDrives are not persistent – they apply only to the current session.

      You could instead create a New-PSSession. Pass that session to Invoke-Command, instead of using -ComputerName. That way, the first Invoke-Command would create the drive, and a subsequent Invoke-Command could refer to the drive, until the PSSession is closed.

    • #36399
      Participant
      Topics: 1
      Replies: 169
      Points: 0
      Rank: Member

      Please read Don’s comments carefully as this may be more about understanding how remoting works then a issue with your code. I have verified that you can create the psdrive and use it withing the same invoke-command.

      Invoke-Command -ComputerName SomeRemoteComputer -ScriptBlock {
          New-PSDrive -Name HKU -PSProvider registry -root Registry::HKEY_USERS
          cd HKU:
          dir
      }
      
    • #36402
      Participant
      Topics: 3
      Replies: 10
      Points: 0
      Rank: Member

      @Don and @Jonathan that makes it much more clear. I am grateful for the feedback and then leads me to a loaded question…. Why did Microsoft decide that they would make hkcu and hlm but not make it default for the othr registry drives?

    • #36405
      Participant
      Topics: 1
      Replies: 169
      Points: 0
      Rank: Member

      I can only assume they created the default drives to account for the most common customer needs and enable everyone else to create the custom drives they need.

Viewing 8 reply threads
  • The topic ‘Connecting to HKU on a remote computer’ is closed to new replies.