Forum Discussion
DanRobb
Sep 11, 2019Brass Contributor
Session Hosts Hanging Frequently
Wondering if anyone else is seeing regular VM hangs with the Windows 10 Enterprise for Virtual Desktops image, or has any advice on troubleshooting the issue we're experiencing? In our tenant we ...
Robin_Kinetix
Jul 15, 2020Copper Contributor
DanRobb did you ever figure this out?
DanRobb
Jul 16, 2020Brass Contributor
We were never able to figure out a proper solution despite months of back and forth with MS Support.
I ended up creating a scheduled task to reboot on the hosts every 8 hours (if nobody is logged in) which has reduced the frequency of the hangs from once every few days to once every few weeks/months.
Here's the Powershell script that is called by the scheduled task - it runs every 5 minutes.
$minHoursBetweenReboots = 8 # Minimum number of hours between reboots
function Write-Log($logMessage)
{
$logFile = "$($PSScriptRoot)\ScheduledRestart.log"
Write-Output "[LOG] [$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] $logMessage"
Write-Output "[LOG] [$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] $logMessage" | Out-File $logFile -Append
}
Write-Log "Start of script"
function End-Script
{
Write-Log "End of script"
exit 0
}
# Check for running installations. If any, then exit
if((Get-Process).Name -match "msiexec|setup|wusa")
{
Write-Output "Detected software installation in progress. Exiting..."
End-Script
}
# Check for active sessions. If any, then exit
[array]$activeSessions = & query session | Select-String -SimpleMatch "Active"
if($activeSessions.Count -gt 0)
{
Write-Log "There are $($activeSessions.Count) active sessions. Exiting..."
End-Script
}
else
{
Write-Log "There are no active sessions"
}
# Get last boot time
try
{
$osInfo = Get-WmiObject -Class Win32_OperatingSystem
[datetime]$lastBootTime = $osInfo.ConvertToDateTime($osInfo.LastBootUpTime)
Write-Log "Last boot time: $lastBootTime"
}
catch
{
Write-Log "ERROR: Unable to get last boot time. Exiting..."
End-Script
}
# If more than $minHoursBetweenReboots since last boot time then reboot
if($lastBootTime -lt (Get-Date).AddHours(-$minHoursBetweenReboots))
{
Write-Log "Last boot time was more than $minHoursBetweenReboots hours ago. Restarting..."
& shutdown -r -t 0 -f -c "Restart initiated by scheduled task/powershell script"
}
else
{
Write-Log "Last boot time was less than $minHoursBetweenReboots hours ago. Exiting..."
End-Script
}
End-Script