I'm writing SolarWinds SAM script that can monitor logged in users on a remote machine. I wanted to use quser
rather than a WMI/CIM based solution because quser
provides exactly the data that i'm looking for.
The kicker is that the monitoring application provides a PSCredential
object to use in order to authenticate with remote machines, something that quser
can't use.
When running with no credential object I get the following.
quser /server:'testserver1'
No User exists for *
The answer to this dilemma was painfully obvious once I realized it. You can simply use the Invoke-Command
cmdlet with the -Credential
and -ScriptBlock
parameter to run the quser
command on the remote server with the appropriate user context.
One final note - if you need help parsing the output of quser
into a more PowerShell friendly object, see Get-LoggedOnUser. It's a fantastic script that handles all of the string parsing necessary to get the properties from the quser
output.
$Server = 'testserver1'
$Credential = Get-Credential
Invoke-Command -ComputerName $Server -ScriptBlock { quser } -Credential $Credential
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
a-srz rdp-tcp#0 2 Active 19 6/2/2020 10:19 AM