简体   繁体   English

Powershell GET-PSSession 输出用户名作为列表框

[英]Powershell GET-PSSession output Username as Listbox

if i enter the following invoke-command:如果我输入以下调用命令:

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

i get the output in commandline like:我在命令行中得到如下输出:

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   

Now it should only displayed the username in a listbox like:现在它应该只在列表框中显示用户名,例如:

# 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)静态输出(示例图片)

But we want display dynamic all user which are logged in.但是我们想要动态显示所有登录的用户。

Has anyone an idea to show the commandline output as listbox like the example above (example picture)?有没有人想像上面的例子(示例图片)那样将命令行输出显示为列表框?

Thanks a lot !非常感谢 !

Welcome, Julian!欢迎,朱利安! First thing you need to do is parse the output of query session and get it into a set of objects.您需要做的第一件事是解析query session的输出并将其放入一组对象中。 I found this example for parsing that simplifies the process.我发现这个解析示例可以简化流程。

Next you need to iterate through that list and add each user name to the listbox.接下来您需要遍历该列表并将每个用户名添加到列表框中。 This should work - if you replace 'localhost' with your target computer:这应该有效 - 如果您将“localhost”替换为您的目标计算机:

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

Thanks for your suggesstion but in does not work for me.感谢您的建议,但对我不起作用。 "Get-ComputerSessions" is not a part of Cmdlet, function, etc. pp" “Get-ComputerSessions”不是 Cmdlet、函数等的一部分。pp“

But i have found a other solution to fill my Listbox:但我找到了另一种解决方案来填充我的列表框:

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

and

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

OR ( my favourite )或者(我最喜欢的

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

But i want only display the Username and (optional) the state of the rdp user.但我只想显示用户名和(可选)rdp 用户的状态。

It should looks like:它应该看起来像:

User1            active
User2            disconnected

The origin output includes some more information as i need.原始输出包括我需要的更多信息。 Example which also does not work for me:对我也不起作用的示例:

Is it possible to make a "table" or "grid-view" and put them into the listbox?是否可以制作一个“表格”或“网格视图”并将它们放入列表框中? See example above.请参见上面的示例。

Thanks a lot!非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM