I am trying to create a script that can force logoff of users in an RDS server farm environment. There are 1 of 4 RDS servers that users can be be logged into, and I am trying to make this so you run the script, enter a username and it will force logoff of the user. So far I have:
import-module remotedesktop
$user = read-host -prompt 'Enter Username you want to disconnect'
$sid = Get-RDUserSession | Where-Object -filter {$_.UserName -eq $user} | format-wide -Property UnifiedSessionID
$server = Get-RDUserSession | Where-Object -filter {$_.UserName -eq $user} | format-wide -Property HostServer
Invoke-RDUserLogoff -HostServer "$server" -UnifiedSessionID $sid
The only problem with this in the last line it doesn't like the $sid
variable as the input for that item. If I do a write-output $sid
it displays the correct UnifiedSessionID
number.
I could be doing this the wrong way altogether, but I just need a way to disconnect user sessions based on a username input when prompted.