Have you ever experienced that some of the Virtual Desktop Agents remain turned off after a scheduled reboot via the Delivery Group? This is something I could notice several times of the last years in various environments. Most of the time this only affects a few machines and it is not a big deal. During a current customer engagement I could see the same issues again but sometimes over 40% of the VDAs have been affected (not every day). This can happen if there is a boot storm occurring (PVS servers are exhausted) or the VDA registration process is not happening after 20 minutes (CTX223434). That was not the case in this specific environment. The interesting thing is that we never saw a power on action for the problem VDAs in VMware vCenter. You will find a lot of people in Citrix Discussions or Reddit who are struggling with the same problem but seems like there is not a real solution for this behavior.
That is why I came up with the idea to write a PowerShell script which needs to be run after the restart schedule which is doing the following things:
- Checking for all faulty VDAs for the given Machine Catalog (PowerState: Off, RegistrationState: Unregistered, MaintenanceMode: False)
- Powering on each VDA with a delay of 15 seconds (New-BrokerHostingPowerAction)
- Sends an email notification (if enabled)
The great thing is we do not need to work with VMware PowerCLI, because we can start the VDAs with the integrated Citrix PowerShell SDK. This makes it work for all the environments out there, doesn’t matter if you are running Citrix Hypervisor, VMware, AHV etc.
Hope this is helping some people and I appreciate some feedback 🙂
Citrix_VDA_BootFix_V1.2.ps1
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
#========================================================================== # # Check for Unregistered VDAs after Scheduled Reboot # # AUTHOR: Julian Mooren # DATE : 13.08.2020 # # VERSION: 1.2 # - Added Mail Notifications # - Improved Error Handling + Logging # - Excluding VDAs in Maintenance Mode # #========================================================================== # Custom variables [edit] $BaseLogDir = "C:\Logs" $PackageName = "Citrix_VDA_Startup" $Catalog = "FARMP10" $SendMail = $true $SMTPSERVER = "EX-01.flashmob-saulgau.de" $SendMailFrom = "citrix@flashmob-saulgau.de" $SendMailTo= "julian.mooren@flashmob-saulgau.de" # Global variables $date = Get-Date -Format "dd-MM-yy_hh-mm" $LogDir = (Join-Path $BaseLogDir $PackageName).Replace(" ","_") $LogFileName = "$($PackageName)_$($Catalog)_$date.log" $LogFile = Join-path $LogDir $LogFileName [int]$BrokenVDA = "0" # Create the log directory if it does not exist if (!(Test-Path $LogDir)) { New-Item -Path $LogDir -ItemType directory | Out-Null } Start-Transcript $LogFile | Out-Null # Import the Citrix Powershell Snapins Write-Host "I" "Import the Citrix Powershell Snapins" try { Add-PSSnapin Citrix* Write-Host "S" "The Citrix Powershell Snapins was imported successfully" } catch { Write-Host "E" "An error occurred trying to import the Citrix Powershell Snapins (error: $($Error[0]))" Exit 1 } # Check for Machine Catalog Write-Host "I" "Checking for Existence of Machine Catalog: $($Catalog)" if((Get-BrokerDesktop -CatalogName $Catalog) -eq $null ) { Write-Host "E" "Machine Catalog: $($Catalog) is not available" Write-Host "I" "Exiting Script....." exit 1 } else {Write-Host "S" "Machine Catalog: $($Catalog) is available"} $VDAs = Get-BrokerDesktop -CatalogName $Catalog -RegistrationState Unregistered -PowerState Off -InMaintenanceMode $false | Select HostedMachineName if($VDAs -ne $null) { foreach($VDA in $VDAs) { Write-Host "I" "$($VDA.HostedMachineName) was powered off. Booting up the machine...." try { New-BrokerHostingPowerAction -MachineName $VDA.HostedMachineName -Action TurnOn | Out-Null $BrokenVDA++ } catch { Write-Host "E" "Problem with turning on the VDA: $($VDA.HostedMachineName)" Write-Host "I" "Exiting Script....." exit 1 } Write-Host "I" "Waiting for 15 seconds before starting the next VDA...." Sleep 15 } } else {Write-Host "I" "All VDAs are registered. Nothing to do....."} # Send E-Mail Notification if($SendMail -eq $true) { Write-Host "I" "Preparing E-Mail Notification." $NewMsg = new-object Net.Mail.MailMessage $SMTP = new-object Net.Mail.SmtpClient($SMTPSERVER) $NewMsg.IsBodyHTML = $true $NewMsg.From = $SendMailFrom $NewMsg.To.Add($SendMailTo) $NewMsg.Subject = "Citrix VDA Health Status - Catalog $($Catalog)" $NewMsg.Body = @" <p>The Machine Catalog $Catalog had $BrokenVDA unhealthy VDAs.</p> "@ try { $SMTP.Send($NewMsg) Write-Host "S" "E-Mail Notification was send successfully" } catch { Write-Host "E" "(error: $($Error[0]))" exit 1 } } Stop-Transcript |
Nice job
The script is working fine when I starting it in Powershell. But when I set it up as an Task Schedule is not working. Hav you tried to run the script from Task Schedule?
Did you configure the “log on as a batch job” setting?
Screenshot: https://imgur.com/a/irreaNj
Thanks.
I had to set the Action in Task Schedule to:
Action: Start a Program
Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments: -Executionpolicy bypass -file C:\thescript.ps1
That did the trick.
But it seems to run the script only work with Domain Admins role.