2

I need to use New-PsSession to run multiple commands on a remote computer to gather information from the registry.

The problem is when I run New-PsSession, it creates a user profile with my username on the computer and after I disconnect and remove the session, the profile is correctly "unloaded" but the files stay in C:\USERS\XXX.

This is a problem because sometimes my collegues who reimage computers will see my user profile on that computer and think I'm logging on that computer even though I just ran a few commands to check registry settings.

My question: Is there a way to create a PsSession without it creating a user profile to your name on the target computer or some other way to make it clean itself up after it's disconnected and removed?

Example code to illustrate the situation:

New-PSSession -ComputerName XXXX -name TEST

start-sleep 3 

Disconnect-PSSession -Name TEST 
Remove-PSSession -Name TEST

Of course I could do something like this after the PsSession is removed:

Get-WMIObject -ComputerName X -class Win32_UserProfile | Where {$_.LocalPath -eq "C:\Users\X" } | Remove-WmiObject

It works but it feels "dirty". Am I missing something?

Rakha
  • 1,874
  • 3
  • 26
  • 60
  • Why do you believe you need `New-PSSession` rather than `Invoke-Command`? – Ansgar Wiechers Mar 01 '19 at 14:33
  • In fact I need both, when you run multiple Invoke-Command, I always heard it was better practice to do Invoke-Command -Session $Session instead of Invoke-Command -ComputerName 1234 because of the overhead it would generate. So I create the session first then do the invoke-command -session multiple times (It's part of a complex GUI application in the end). I tested it and it's really faster this way too. – Rakha Mar 01 '19 at 14:56
  • I have no idea what you think you need multiple `Invoke-Command` for. Just put everything you want to run into one script or scriptblock and run `Invoke-Command` once. `*-PSSession` cmdlets are for interactive use. For automation stick to `Invoke-Command`. – Ansgar Wiechers Mar 01 '19 at 15:02
  • It's part of a GUI application targeting multiple computers. There is a checklist to indicate what info you want to gather. Let's say I would do ONE invoke-command that gathers all info, and then show ONLY the properties checked by the user in the list, that would be fine for one computer and I wouldn't use multiple invoke-commands. But if he queries let's say 50 computers, it would be a lot slower than multiple commands. 50x3 quick invoke-command is faster than 50x1 huge invoke-command. I measured it and in the end it was better like this. – Rakha Mar 01 '19 at 15:27
  • Scriptblocks as well as scripts can be built dynamically. Your point being? – Ansgar Wiechers Mar 01 '19 at 15:48
  • I must be missing a part in the design aspect. I built a pretty complex GUI that works fine but underneath I bet you would find a lot of dirty tricks. This is probably the case. I don't doubt you are right 100% on this. Still learning. I should've built scriptblocks based on options selected and then feed that all to one invoke-command huh? I'll redesign one day! – Rakha Mar 01 '19 at 15:52

1 Answers1

1

Have you tried the following:

$option = New-PSSessionOption -nomachineprofile
New-PSSession -ComputerName XXXX -name TEST -SessionOption $option
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
  • Perfection, this is exactly what I was missing, I didn't notice there was another CMDLET for setting options. Thank you so much! – Rakha Mar 01 '19 at 14:47