开发者

create var from .exe output in powershell

开发者 https://www.devze.com 2023-03-08 01:11 出处:网络
I need to make a variable from the ID number of a username qwinsta.exe /server:vm041 derpy.herp This returns the following output

I need to make a variable from the ID number of a username

qwinsta.exe /server:vm041 derpy.herp

This returns the following output

SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
                  der开发者_开发知识库py.herp                3  Disc

I need to create a variable in powershell with a value of 3 as per output above.

This code will be used to look through a number of servers to see if an individual user is logged in and disconnect them with rwinsta.exe


Here's quite a crude regular expression - I don't know qwinsta.exe so I'm not sure of the permutations of data it can return - this works with your above example though.

This will assign 3 to the variable $ID.

$output = qwinsta.exe /server:vm041 derpy.herp
$output -match ".*derpy.herp.*(\d).*"
$ID = $Matches[1]

you may be able to parameterise the username like so:

$username = "derpy.herp"
$output = qwinsta.exe /server:vm041 $username
$output -match ".*$username.*(\d).*"
$ID = $Matches[1]

HTH, Matt


You can parse the output you are obtaining, get your ID, and then use New-Variable cmdlet to instance your variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消