Looking for assistance to Automate groups of WVD VMs to Power On / Off in a cycle

BrianPitt 176 Reputation points
2021-02-03T17:22:24.267+00:00

We have 20 WVD VMs running in a pool. Using an Automation Runbook and Schedule with Tags, we have it set where 5 VMs are powered on every day at 7 AM. At 5 PM, due to business hours specified in scaling script and number of VMs to leave running outside business house, the number of running VMs is dropped to 2.

Currently, to change which 5 VMs are set to run / power on / off, we have to change which VMs have the tag set in the schedule so the Automation Runbook does its job.

What I am looking for is a way to automate things up where we can group the 20 VMs into 4 groups of 5 and every 7 days, 1 of the groups takes over and 5 power on at 7 AM - 5 PM and 2 remain running from 5 PM - 7 AM. The following week, the next group of 5 would take over, with the previous group moving to the back of the pack. Over 4 weeks, each group of 5 VMs would take their turn running on the schedule.
Hope that made sense.....is there a way to look into doing this?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,128 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,114 questions
Azure Virtual Desktop
Azure Virtual Desktop
A Microsoft desktop and app virtualization service that runs on Azure. Previously known as Windows Virtual Desktop.
1,362 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,361 Reputation points MVP
    2021-02-06T15:43:36.327+00:00

    The code around getting the VMs and start the VMs could look like this:

    64815-azureautomationvariables.jpg

    # Get basic variables  
    $day = Get-AutomationVariable -Name 'WVDdayofweek'  
    $week = Get-AutomationVariable -Name 'WVDweek'  
    $WVDgroupTag = $week  
      
    # Just some output  
    Write-Output "Current Day is $day"  
    Write-Output "Current Week is $week"  
      
    # Add your code to get and start all VMs with Tag WVDgroup = x here  
    Write-Output "Starting WVD host of WVDhostgroup =  $WVDgroupTag"  
      
      
    ########################################  
    # Prepare and set variables for next run  
    if ($day -lt 7) {  
        $day += 1  
        Set-AutomationVariable -Name 'WVDdayofweek' -Value $day  
        }  
    else {  
        $day = 1  
        Set-AutomationVariable -Name 'WVDdayofweek' -Value $day  
        if ($week -lt 4) {  
            $week += 1  
            Set-AutomationVariable -Name 'WVDweek' -Value $week  
            }  
        else {  
            $week = 1  
            Set-AutomationVariable -Name 'WVDweek' -Value $week  
            }  
        }  
      
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,361 Reputation points MVP
    2021-02-05T20:27:52.783+00:00

    Just an idea (draft):

    Create 2 Azure Automation variables (for example):

    • DayOfWeek
    • Week
      Set both variables at the beginning = "1"

    Create a Tag for each VM of the 4 WVD Host Groups. For instance Tag Name: "WVDgroup" and provide a value to each of the 4 groups (1, 2, 3, 4). Each of the 20 WVD host should have set the "WVDgroup" tag from "1" to "4".
    5 VMs with Tag "WVDgroup =1", 5 VMs with Tag "WVDgroup =2", 5 VMs with Tag "WVDgroup =3", ....

    In the Automation runbook for "WVD start" add a query for the 2 Automation variables "DayOfWeek" and "Week"
    If "Week" equals 1" start WVD host with Tag "WVDgroup equals 1 "
    Set variable "DayOfWeek+ 1"
    If "DayOfWeek equals 7" set variables "Week +1" and "DayOfWeek = 1"
    This way after 7 days the Week variable is 2 and the second WVD host group (with Tag "WVDgroup = 2") will be startet, and so on

    If "Week equals 4" and "DayOdWeek equals 7" set "Week = 1" and "DayOfWeek = 1 " (start over again)

    Does this help?


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. BrianPitt 176 Reputation points
    2021-02-06T12:54:02.247+00:00

    Thank you for the information. Going to try and set this up and see how it goes

    I appreciate it

    0 comments No comments

  3. BrianPitt 176 Reputation points
    2021-02-09T11:29:00.267+00:00

    Thank you!! This is looking like its going to work....I really really appreciate it!

    0 comments No comments