qwinsta/server:somesrv 在 Powershell 中等效? [英] qwinsta /server:somesrv equivalent in Powershell?

查看:43
本文介绍了qwinsta/server:somesrv 在 Powershell 中等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 cmd 中运行 qwinsta/server:somesrv 命令时,我可以获得登录到特定 Windows 服务器的所有当前 RDP 会话的列表.

When I run the qwinsta /server:somesrv command in cmd I can get a listing of all the current RDP sessions that are logged into a particular Windows server.

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 console                                     0  Conn    wdcon
 rdp-tcp                                 65536  Listen  rdpwd
 rdp-tcp#594       tom1                      1  Active  rdpwd
 rdp-tcp#595       bob1                      2  Active  rdpwd

是否可以从 Powershell 的远程服务器上获取这样的列表,以便数据可以在其他地方使用?

Is it possible to get a list like this on a remote server from Powershell so that the data can be used elsewhere?

推荐答案

有多种选择:

  • 使用 终端服务 PowerShell 模块.简单的解决方案.
  • 编写一个 powershell 包装器,将 qwinsta 的输出解析为对象.简单的解决方案.请参阅下面的示例
  • 使用 Cassia.DLL .Net 包装器访问 qwinsta 在幕后运行的本机 API.这是 TS 模块使用的类.难度更大,但可以根据您的需求进行定制.
  • 疯狂使用 Cassia.DLL 使用 P/Invoke 访问的本地方法(wtsapi32.dllkernel32.dll、<代码>winsta.dll).困难且过于复杂.
  • Use the Terminal Services PowerShell Module. Easy solution.
  • Writing a powershell wrapper that parses the output of qwinsta to objects. Easy solution. See example below
  • Use the Cassia.DLL .Net wrapper to access the native APIs that qwinsta runs behind the scene. This is the class that the TS Module uses. More difficult, but will have the benefit of being customized to your needs.
  • Go crazy and use the Native Methods that Cassia.DLL accesses using P/Invoke (wtsapi32.dll, kernel32.dll, winsta.dll). Hard and overcomplicated.

用于 qwinsta 的 PowerShell 包装器

PowerShell-wrapper for qwinsta

function Get-TSSessions {
    param(
        $ComputerName = "localhost"
    )

    qwinsta /server:$ComputerName |
    #Parse output
    ForEach-Object {
        $_.Trim() -replace "\s+",","
    } |
    #Convert to objects
    ConvertFrom-Csv
}

Get-TSSessions -ComputerName "localhost" | ft -AutoSize

SESSIONNAME USERNAME ID     STATE  TYPE DEVICE
----------- -------- --     -----  ---- ------
services    0        Disc                     
console     Frode    1      Active            
rdp-tcp     65537    Listen     

#This is objects, so we can manipulate the results to get the info we want. Active sessions only:
Get-TSSessions -ComputerName "localhost" | ? { $_.State -eq 'Active' } | ft -AutoSize SessionName, UserName, ID

SESSIONNAME USERNAME ID
----------- -------- --
console     Frode    1 

          

这篇关于qwinsta/server:somesrv 在 Powershell 中等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆