简体   繁体   English

您可以使用 PSCredential object 运行 quser 吗?

[英]Can you run quser with a PSCredential object?

Question问题

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无法使用的。

Code代码

When running with no credential object I get the following.在没有凭据 object 的情况下运行时,我得到以下信息。

quser /server:'testserver1'

Output 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 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 获取属性所需的所有字符串解析。

Code代码

$Server = 'testserver1'
$Credential = Get-Credential
Invoke-Command -ComputerName $Server -ScriptBlock { quser } -Credential $Credential

Output Output

 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.

相关问题 带有新对象的 Quser (Powershell) - Quser with New-Object (Powershell) Powershell-将PSCredential对象传递到新的命令窗口 - Powershell - pass PSCredential object to new command window Get-Credential创建字符串对象而不是PSCredential对象(Powershell) - Get-Credential Creating a String Object instead of PSCredential Object (Powershell) 如何将PSCredential对象从C#代码传递给Powershell函数 - How to Pass PSCredential object from C# code to Powershell Function New-Object:找不到“PSCredential”的重载和参数计数:“2” - New-Object : Cannot find an overload for “PSCredential” and the argument count: “2” New-Object:找不到“PSCredential”的重载和参数计数:“2” - New-Object : Cannot find an overload for “PSCredential” and the argument count: “2” 新对象PSCredential不起作用-语法上使用Unicode标点符号 - New Object PSCredential not working - using Unicode punctuation syntactically 使用PSCredential对象以非交互方式验证命令 - Using PSCredential Object to authenticate commands non-interactively PowerShell 5.1 创建 PSCredential 对象的新方法是什么 - PowerShell 5.1 What's the newer way of creating a PSCredential object 在C#中以编程方式在ScriptBlock中传递Parameter对象(PSCredential) - Pass a Parameter object (PSCredential) inside a ScriptBlock programmatically in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM