简体   繁体   中英

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. 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:

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"

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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