Connecting to HKU on a remote computer

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Jonathan Warnken Jonathan Warnken 6 months, 2 weeks ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #36344
    Profile photo of Stuart Fleck
    Stuart Fleck
    Participant

    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
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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
    Profile photo of Stuart Fleck
    Stuart Fleck
    Participant

    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
    Profile photo of Jeremy Murrah
    Jeremy Murrah
    Participant

    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
    Profile photo of Stuart Fleck
    Stuart Fleck
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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
    Profile photo of Stuart Fleck
    Stuart Fleck
    Participant

    @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
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.