Invoke-Command not returning same properties vs Enter-Pssession

Welcome Forums General PowerShell Q&A Invoke-Command not returning same properties vs Enter-Pssession

Viewing 1 reply thread
  • Author
    Posts
    • #202508
      Participant
      Topics: 2
      Replies: 1
      Points: 5
      Rank: Member

      Super noob here so forgive me for knowing next to nothing but I’ve been playing around on my own with PS while reading my study book and I wanted to try and see if I could figure out a way to automate deleting a specific folder under a specific users HKU in the registry. Ive actually gotten pretty close I think but when I invoke the command its not passing properties through the same way that it does when I actually connect in directly (pics below). Ive tried setting invoke to login using my known working admin creds and its the same thing there too so I dont think its a permissions thing. Ultimately ill need those Properties so I can identify the specific user I need to delete the item for. Below is what I’ve come up w/ and also some pics to illustrate. If anyone has any ideas Itd be much appreciated. Thanks

      $SN = Read-Host 'Please Enter FQDN'
      Invoke-Command -Cn $SN -ScriptBlock { Get-ChildItem -Include 'Volatile Environment' -Path Registry::\HKEY_USERS -Recurse }

      Enter-Pssession

      https://imgur.com/a/uexHSqZ

      Invoke

      https://imgur.com/a/A3RPyx3

      • This topic was modified 2 weeks, 5 days ago by Vdominguez.
      • This topic was modified 2 weeks, 5 days ago by Vdominguez.
    • #202841
      Senior Moderator
      Topics: 3
      Replies: 117
      Points: 605
      Helping Hand
      Rank: Major Contributor

      Please don’t post shell output in images, just copy/paste and post it with the pre tags.

      One of the problems you’ll run into is that HKEY_USERS is not mounted as a drive for PowerShell by default, which means that PowerShell normally doesn’t have access to it. If you run Get-PSDrive in a new session you’ll see that HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE are available. You can mount HKU as discussed in this thread.

      It looks like you may have done this in order to get the output you show for Enter-PSSession, but it’s impossible to know without seeing more of your code. However, even if you did do this for the remote session, it must be done for each session. That is, each time a new session starts, it starts with the default settings. When you use Invoke-Command, it creates a new session for each command that it executes in a scriptblock, unless you tell it to use a specific session (as described in Example 4 on that page).

      In your usage of Invoke-Command, you did not specify a session. Therefore, even if you had already mounted HKU on the remote computer in a session, it would not be available to that instance of Invoke-Command because it created its own session when it executed.

      Assuming that you did mount HKU in the session where you used Enter-PSSession, if you were to specify that Invoke-Command use that same session it should give you similar results.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.