You can use a RegEx expression to find the ID needed on the rwinsta command.
Suppose you want to logoff the "console" session, then you could do
$consoleID = qwinsta |
Select-String -Pattern ">console\s*\w*\s*(?<ID>\d*)\s*" |
Select -ExpandProperty Matches |
%{$_.Groups["ID"].Value}
rwinsta $consoleID
If you need to do this for a number of VMs, you can use a loop.
Something like this for example
$cmd = @"
$consoleID = qwinsta |
Select-String -Pattern ">console\s*\w*\s*(?<ID>\d*)\s*" |
Select -ExpandProperty Matches |
%{$_.Groups["ID"].Value}
rwinsta $consoleID
"@
Get-VM | %{
Invoke-VMScript -VM $ -ScriptText $cmd
}
You specify the little script in a here-string, and then you run that script in each VM through the Invoke-VMScript cmdlet.
Let me know if that works ?