Here’s part of a script I’ve been working on to list the health of an RDP environment.
In this scenario, part of it was to check for disconnected RDP sessions.
Just to check, not to kill them 😉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$ComputerName = $env:COMPUTERNAME $queryResults = (qwinsta /server:$ComputerName| foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv) ForEach ($queryResult in $queryResults) { $Hash = @{ ComputerName = $ComputerName UserName = $($queryResult.USERNAME) SessionId = $($queryResult.SESSIONNAME -replace 'rdp-tcp#','') } # Check the session state switch ($queryResult.username) { # If UserName is a number, it's an unused session {$_ -match "[0-9]"} { $Hash.Add("SessionState","InActive") break } {$_ -match "[a-zA-Z]"} { $Hash.Add("SessionState","Active") break } default { $Hash.Add("SessionState","Unknown") } } New-Object -TypeName PSObject -Property $Hash } |