#3.26.24 RDS User Logoff script 1.1
#Baylin @ Molten Software
#log off user from any RDS session in your network by only entering username/saMaccountName
write-host "RDS User Logoff" -ForegroundColor Cyan
write-host "Enter the user's saMaccountName" -ForegroundColor Green
write-host "Examples: AThompson or Alex.Thompson"-ForegroundColor Green
write-host "CAN NOT be email or UPN like alex.thompson@molten.pw" -ForegroundColor Red
$userName = read-host "saMaccountName:"
#clear variables
$sessionId = ""
$found = ""
#edit for your domain name here
$domain = "molten"
#edit for your RDS profile path if you want to delete the user profile after logging them off.
$profilePath = "\\RDSprofiles\C$\Profiles"
#enter your RDS servers here, seperated by comma
$workstations =
"RDSServer1",
"RDSServer2",
"RDSServer3",
"RDSServer4",
"RDSServer5"
ForEach($Workstation In $Workstations){
write-host "Checking $workstation" -ForegroundColor cyan
quser /server:$workstation
$sessionId = ((quser /server:$workstation | Where-Object { $_ -match $userName }) -split ' +')[2]
if ($sessionId) {
write-host "Found user in $sessionId on $workstation" -ForegroundColor yellow
write-host "Logging user $username off from $workstation"-ForegroundColor yellow
logoff $sessionId /server:$workstation
$found = "yes"
}
}
if ($found) {
$time = Get-Date
Write-Host "User $username was found on $workstation and was logged off at $time" -ForegroundColor Magenta
#reset $found variable
$found = ""
}
Write-Host "If you want to delete the user'profile as well, type 'RDSdel' and hit enter." -ForegroundColor Cyan
function RDSdel {
$objUser = New-Object System.Security.Principal.NTAccount("$domain", "$username")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$deleteObject = $profilePath + "\UVHD-" + $strSID.Value + ".vhdx"
if (Test-Path -path $deleteObject) {
rm $deleteObject -ErrorAction Stop
Write-Host "$username 's profile @ $deleteObject has been deleted." -ForegroundColor Green
}
else {Write-Host "$username 's RDS profile disk not found or recently deleted" -ForegroundColor yellow}
}
|