top of page

Using Automation Accounts to Schedule Reboots for AVD Hosts Running on Azure Local

  • davidpereira20
  • Jan 2
  • 3 min read

Updated: Jan 7





Automating the reboot of Azure Virtual Desktop (AVD) hosts running on Azure Local can help ensure optimal performance and maintenance with minimal manual intervention. In this guide, we’ll explore how to use Azure Automation Accounts to schedule and manage these reboots efficiently.


Scenario

You have a pool of Azure Virtual Desktop (AVD) hosts deployed on Azure Local, AKA Azure Stack HCI. To ensure smooth operations and maintain optimal performance, you need to schedule regular restarts or execute maintenance scripts on these hosts.

This setup allows you to automate routine tasks, reduce manual effort, and minimize potential downtime for end users.


How to run PS scripts against the hosts:


1- Create an Automation account.

In your Azure tenant create an Automation Account and give it a meaningful name for the task at hand.

2- Create a new Runbook.

Give it a name select PowerShell and type a description and select Create.

3- Configure whatever script you want to run in this example I'm restarting named AVD hosts on the Azure Local cluster but you can create something smarter or simpler to the same effect.


Click Save after you type or paste your script and publish.


NOTE: At this point you can also test the script but there's still a couple elements missing, keep in mind that when you test a script it will actually run it, so if you're testing a reboot script it will indeed restart whatever VM you're running it against.

# Name of the AVD hosts to reboot and name of the Azure Local Hosts
param ( 

    [string[]]$vmNames = @("AVDHost1,AVDHost2,AVDHost3"), 

    [string[]]$nodes = @("AzureLocalHost1", "AzureLocalHost2") 

) 

  

# Retrieve the credential from the Automation Account 

$credential = Get-AutomationPSCredential -Name "HyperVAdminCredential" 

  

# Check if the credential was successfully retrieved 

if ($null -eq $credential) { 

    throw "The credential 'HyperVAdminCredential' is not found or is empty. Please provide a valid credential." 

} 

  

# Debug logging 

Write-Output "Credential successfully retrieved." 

  

# Iterate through each VM name and each node 

foreach ($vmName in $vmNames) { 

    foreach ($node in $nodes) { 

        try { 

            # Retrieve the VM object 

            $vm = Invoke-Command -ComputerName $node -Credential $credential -ScriptBlock { 

                param($vmName) 

                Get-VM -Name $vmName -ErrorAction Stop 

            } -ArgumentList $vmName 

  

            if ($null -eq $vm) { 

                Write-Output "VM $vmName not found on node $node." 

                continue 

            } 

  

            # Check the VM state 

            $vmState = $vm.State 

            Write-Output "VM $vmName found on node $node in state: $vmState" 

  

            # Restart or start the VM based on its state 

            if ($vmState -eq "Running") { 

                Invoke-Command -ComputerName $node -Credential $credential -ScriptBlock { 

                    param($vmName) 

                    Restart-VM -Name $vmName -Force -ErrorAction Stop 

                } -ArgumentList $vmName 

                Write-Output "Successfully restarted VM: $vmName on node: $node" 

            } 

            elseif ($vmState -eq "Off") { 

                Invoke-Command -ComputerName $node -Credential $credential -ScriptBlock { 

                    param($vmName) 

                    Start-VM -Name $vmName -ErrorAction Stop 

                } -ArgumentList $vmName 

                Write-Output "VM $vmName was off on node: $node. It has been started." 

            } 

            else { 

                Write-Output "VM $vmName on node: $node is in state: $vmState. No action taken." 

            } 

        } 

        catch { 

            Write-Error "An error occurred while processing VM `${vmName}` on node `${node}`: $($_.Exception.Message)" 

        } 

    } 

} 

4- Create credentials for the script to connect to Hyper-V


Navigate back to the Automation account and under Shared Resources click Credentials, the user will require permissions to restart a VM in Hyper-V or whatever you're trying to run with your script.

5- Create a 'Hybrid Worker Group' under Automation Account > Process Automation


Hybrid Workers allow you to run scripts on your On-Prem workloads like in the case of Azure Local, you can install this extension in any supported VM in the Azure Local Cluster. Create a new Hybrid Worker Group and select one or more VMs to install the extension.


6- Create a Schedule Under Automation Account > Shared Resources



7- Link Runbook with Schedule


Go back to your runbook expand resources and select Schedules click Add a Schedule

Select the previously created schedule and configure any parameters your script is expecting.

Make sure you select Hybrid Worker under Parameters otherwise the script won't work on Azure Local VMs and select the Hybrid Worker Group previously configured.


In summary, there are many ways to achieve this, and you can script various maintenance and operational tasks to suit your specific needs. Hopefully, this guide has provided you with a solid foundation and the basic structure to build and configure your own solution.


Comments


Never Miss a Post.
Subscribe Now!

Thanks for submitting!

  • Youtube
  • Grey Twitter Icon
Tech Talk Lockup.png
bottom of page