[英]Can you run quser with a PSCredential object?
I'm writing SolarWinds SAM script that can monitor logged in users on a remote machine.我正在编写 SolarWinds SAM 脚本,它可以监控远程机器上的登录用户。 I wanted to use
quser
rather than a WMI/CIM based solution because quser
provides exactly the data that i'm looking for.我想使用
quser
而不是基于WMI/CIM 的解决方案,因为quser
提供的正是我正在寻找的数据。
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.关键是监控应用程序提供了一个
PSCredential
object 用于对远程机器进行身份验证,这是quser
无法使用的。
When running with no credential object I get the following.在没有凭据 object 的情况下运行时,我得到以下信息。
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.您可以简单地使用带有
-Credential
和-ScriptBlock
参数的Invoke-Command
cmdlet 在具有适当用户上下文的远程服务器上运行quser
命令。
One final note - if you need help parsing the output of quser
into a more PowerShell friendly object, see Get-LoggedOnUser .最后一点 - 如果您需要帮助将
quser
的 output 解析为对 PowerShell 更友好的 object,请参阅Get-LoggedOnUser 。 It's a fantastic script that handles all of the string parsing necessary to get the properties from the quser
output.这是一个很棒的脚本,可以处理从
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.