G
G
Gregory2015-11-14 00:06:00
PowerShell
Gregory, 2015-11-14 00:06:00

How to get a list of values ​​into a variable from the results of a command in PowerShell?

The ultimate goal is to make sure that all users have logged out of their sessions on the terminal server.
It was decided to use the qwinsta utility command.
We execute the following command in PS : We get the
qwinsta /server:DC1
result:
c2d99999883b4c928fab6d7d7b966a2a.png
I would like to get the IDs of all sessions in an array for further analysis: we are looking for values ​​other than 0, 1, 65536 in the array. If we find it, it means that not all users have completed their sessions on the server yet.
I specifically described the problem in full, as perhaps someone will suggest a more beautiful solution.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ruchkin, 2015-11-14
@grigoriyb

1. In the first line, find where the ID ends
2. Cut off the rest of the lines, and find the number at the end

PS> $out = (qwinsta)
PS> $len = ($out | select -First 1 | ? { $_ -match '.*(ID)' } | % { $matches[0].Length })
PS> $out | select -skip 1 | % { $_.Substring(0, $len) } | ? { $_ -match '(\d+)$' } | % { $matches[1] }
0
2
65537

G
Gregory, 2015-11-16
@grigoriyb

I don't know why, but I have this:
1f1a63386f1b481fbe36f8de9393a335.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question