Question
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.
Code
When running with no credential object I get the following.
quser /server:'testserver1'
Output
No User exists for *
Answer
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 thequser
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 thequser
output.Code
Output