Score:0

Stop/Start Azure VMs Using Hybrid Runbook

cn flag

I currently have runbooks that stop/start VMs on a schedule but recently the runbook to start vms is stopped because it takes too long to complete. The following message is thrown -

"The job has been stopped because it reached the fair share limit of job execution more than 3 hours. For long-running jobs, it's recommended to use a Hybrid Runbook Worker. Hybrid Runbook Workers don't have a limitation on how long a runbook can execute."

I've never used a hybrid runbook before so here's my question - can a hybrid runbook be used to stop/start multiple Azure VM's? I haven't been able to find anything on this, it looks like the hybrid approach is used to pull info or perform operations inside the VM.

I'm ultimately looking to manage VM resources (stop/start VMs) from within an Azure VM if possible. See current runbook below:

Write-Output "------------------------ Authentication ------------------------"
Write-Output "Logging in to Azure ..."

$ConnectionName = "AzureRunAsConnection"

try 
{
    # Get the connection "AzureRunAsConnection "
    $Conn = Get-AutomationConnection -Name $ConnectionName

    # Logging into Azure
    Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint 

    Write-Output "Successfully logged in to Azure." 
}

catch
{
    if (!$Conn)
    {
        $ErrorMessage = "Connection $ConnectionName not found."
        throw $ErrorMessage
    } 
    else
    {
        Write-Error -Message $_.Exception
        throw $_.Exception
    }

}

Write-Output "------------------------ Starting Virtual Machines ------------------------"


## Sequence VMs are started

$Computers = @(

 'virtualmachines(x11)'

)


foreach($c in $Computers)

{

    $AzResource = Get-AzResource -Name $c -ResourceType "Microsoft.Compute/virtualMachines"

    if($null -ne $AzResource)

    { 

        Write-Output "Starting virtual machine..." + $c

       
        Start-AzVM -ResourceGroupName $AzResource.ResourceGroupName -Name $c

 
        # Pauses 4 minutes before continuing loop

        Start-Sleep -Seconds 240

    }  

    else

    {

        throw "Virtual machine not found:" + $c

    }

}

Thanks,

Ken W MSFT avatar
gb flag
Can you post the runbook? It seems like you are running the start in a serial fashion which is why it's taking so long. You should look at running it in parallel. Here is an example https://albandrodsmemory.com/2020/10/13/start-and-stop-azure-vms-in-parallel/
jrd1989 avatar
cn flag
I just added the runbook in the initial post. Thanks for sharing this article, I'll have to see if its something I can apply. We have 11 vm's affected by this runbook. Its a blend of DB, app and web servers. Some services take longer to start so thats why we have the 4 minute pause between executing the next start cmd. Certain VM's are dependent on others so we want enough time for a VM to startup and get services going.
Ken W MSFT avatar
gb flag
If the machines need to be started in order, you can look at this runbook for inspiration. https://docs.microsoft.com/en-us/azure/devtest-labs/start-machines-use-automation-runbooks#:~:text=%20Start%20virtual%20machines%20in%20a%20lab%20in,to%20add%20a%20runbook%20to%20the...%20More%20
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.