This script allows you to reboot a server RDS member of a farm with a broker session by closing the still open sessions.
Use :
- Change the two variables $ServerBroker and $ServerHost.
- Create a scheduled task by running the script on the remote desktop session host server.
Using the script avoids ending up with xxxxx.BACKUP-Y folders in the server’s C: \ Users folder.
During a reboot if UPDs are still mounted a temporary folder is created for each session still open.
The script :
#
# RDR-IT
# Get close and active sessions of a desktop session host server remotely and reboot the server
# Compatible with RDS farms 2012R2 / 2016/2019
#
$ServerBroker = "Broker.dom.local"
$ServerHost = "RDH-01.dom.local"
# Get sessions from broker
$Sessions = Get-RDUserSession -ConnectionBroker $ServerBroker
# Browse all open sessions and close sessions on the session host server configured in parameter
foreach($Session in $Sessions){
#Write Event
Write-EventLog -LogName "System" -Source "EventLog" -EventId 6013 -EntryType Information -Message "$Session.UserName close session"
# Write-Host $Session.UnifiedSessionID -ForegroundColor Red
Invoke-RDUserLogoff -HostServer $ServerHost -UnifiedSessionID $Session.UnifiedSessionID -Force
}
Restart-Computer