【问题标题】:Powershell GET-PSSession output Username as ListboxPowershell GET-PSSession 输出用户名作为列表框
【发布时间】:2019-12-12 11:57:52
【问题描述】:

如果我输入以下调用命令:

Invoke-Command -ComputerName REMOTE-COMPUTER -scriptBlock {query session} -credential CREDENTIALS  | Select-String PART-OF-USERNAME

我在命令行中得到如下输出:

SITZUNGSNAME      BENUTZERNAME       ID  STATUS 
>services                             0  Getr.                       
 console                              1  Verb.                       
 rdp-tcp#61        User1              2  Aktiv                       
 rdp-tcp#93        User2              3  Aktiv                       
 rdp-tcp#35        User3              4  Aktiv   

现在它应该只在列表框中显示用户名,例如:

# Listbox

$listBox = New-Object System.Windows.Forms.ListBox 
$listBox.Location = New-Object System.Drawing.Point(10,60) 
$listBox.Size = New-Object System.Drawing.Size(260,20) 
$listBox.Height = 350

[void] $listBox.Items.Add("User1")
[void] $listBox.Items.Add("User2")
[void] $listBox.Items.Add("User3")
$form.Controls.Add($listBox) 

static output (example picture)

但我们希望动态显示所有登录的用户。

有没有人想将命令行输出显示为列表框,如上面的示例(示例图片)?

非常感谢!

【问题讨论】:

    标签: powershell session listbox output


    【解决方案1】:

    欢迎,朱利安! 您需要做的第一件事是解析query session 的输出并将其放入一组对象中。我发现this example 用于简化流程的解析。

    接下来,您需要遍历该列表并将每个用户名添加到列表框中。这应该可以工作 - 如果您用目标计算机替换“localhost”:

    Get-ComputerSessions 'localhost' | %{ [void] $listBox.Items.Add($_.Username) }
    

    【讨论】:

    • 要添加Get-ComputerSessions 函数,您需要运行链接示例中的脚本。 Get-ComputerSessions 将解析 query session 的输出并从中生成可以通过管道传送到 Out-GridView 的对象:Get-ComputerSessions -Computer "localhost" | Out-GridView
    【解决方案2】:

    感谢您的建议,但对我不起作用。 “Get-ComputerSessions”不是 Cmdlet、函数等的一部分。pp”

    但我找到了另一种解决方案来填充我的列表框:

    $sessions = Invoke-Command -ComputerName COMPUTERNAME -scriptBlock {query.exe user /server:'localhost'} -credential CREDENTIALS | Select-String SEACHSTRING | sort-object 
    

    和

    [void] $listBox.Items.AddRange($sessions)
    

    或者(我最喜欢的)

    Invoke-Command -ComputerName COMPUTERNAME -scriptBlock {query session} -credential CREDENTIALS | %{ [void] $listBox.Items.Add($_) } | Select-String SEARCHSTRING | sort-object 
    

    但我只想显示用户名和(可选)rdp 用户的状态。

    它应该看起来像:

    User1            active
    User2            disconnected
    

    原始输出包含我需要的更多信息。对我也不起作用的示例:

    是否可以制作“表格”或“网格视图”并将它们放入列表框中?见上面的例子。

    非常感谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多