2

I have a user logged in via RDP with the username "hero", However when I Enter-PSSesssion with that machine and run Get-WMIObject -class Win32_ComputerSystem | select username I get this output:

username
--------
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Are you interested in getting the info regarding the logged in user? – Vivek Kumar Singh Feb 14 '18 at 12:01
  • 3
    Per the [docs](https://msdn.microsoft.com/library/aa394102): "In a terminal services session, `UserName` returns the name of the user that is logged on to the console—not the user logged on during the terminal service session." Apparently, nobody is logged on to the console. Doing this for RDP sessions is apparently [not that easy](https://stackoverflow.com/a/23220056/4137916). (Parsing `qwinsta` output and using a helper module are other options, but that's probably food for another question.) – Jeroen Mostert Feb 14 '18 at 12:02
  • @VivekKumar I am interested in getting the username of a currently logged in user and passing it to the script. – Artūras Kalandarišvili Feb 14 '18 at 12:11
  • @JeroenMostert it works for some of the machines though. We have users all logged in through terminals (Raspberry PIs logged through RDP to Windows 10 VMs) and I am getting the logged in username on some of them, but some return an empty result. – Artūras Kalandarišvili Feb 14 '18 at 12:13
  • 1
    Then it's useful to check what `qwinsta` returns on all of these machines. I'd be a little surprised if it was the same thing everywhere. – Jeroen Mostert Feb 14 '18 at 12:23

1 Answers1

1

Here's an answer taken from here:

# replace $computer with your remote machine
$csvtxt=QWINSTA /SERVER:$computer|%{[string]::Join(',',$_.Split(' ',
  [StringSplitOptions]::RemoveEmptyEntries))}
$csv=ConvertFrom-Csv $csvtxt
$csv

This parses the output from QWINSTA into a PSObject, from here you can do a:

$csv | Select USERNAME
qbanet359
  • 183
  • 7