Automate the VM Start and Stop Process using PowerShell Runbook

Introduction

In this blog we will discuss how you can automate the VM start and stop process using PowerShell Runbook.  

To create a Runbook, first you will need to create an Azure Automate Account if you have not already created one. For account creation, you can refer to this blog.

  1. After account creation, you have to import two required modules into your account. On the left menu, find and click on the Modules Gallery and search for the Az.Account. It will take some time to import all its components into your account. After successful import, repeat this step to find the other module named Az.Compute.
  2. After importing the required modules, we can now create a Runbook. Click on create Runbook and select the type as PowerShell.
  3. After filling up the required fields, click on the create button to create a Runbook. Now an editor will open, we have to write the PowerShell commands to start/stop the VM.

Elevate Your Cloud Operations with AlphaBOLD

Discover the power of automation for your Azure environment. AlphaBOLD specializes in Azure DevOps Managed Services, providing expert guidance and implementation of automated solutions.

Request a Consultation

[CmdletBinding()]param (

[Parameter(Mandatory=$true)]

[string]$ResourceGroupName =”ResourceGroupName”,

[Parameter(Mandatory=$true)]

[string]$VMName = ”VMName”,

[Parameter(Mandatory=$true)]

[string]$Operation = ”OperationName”

)

Begin    {

Write-Output ”Connecting on $(Get-Date)”

#Connect to Azure using the Run As Account

Try{

$servicePrincipalConnection=Get-AutomationConnection -Name ”AzureRunAsConnection”

Connect-AzAccount  -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

}

Catch {

if (!$servicePrincipalConnection){

$ErrorMessage = ”Connection $connectionName not found.”

throw $ErrorMessage

} else{

Write-Output -Message $_.Exception

throw $_.Exception

}

}

# Validation parameters

$ArrayOperations = ”Stop”,”Start”,”Restart”

If ($Operation -notin $ArrayOperations)

{

Throw ”Only Stop, Start, and Restart Operations are valid.”

}

# Start

Write-Output ”Starting process on $(Get-Date)”

Try{

$Status = Get-AzVM -ResourceGroupName Edwards -Status | Select-Object  @{ label = ”VMStatus”; Expression = { $_.PowerState } } | Format-Table -HideTableHeaders | Out-String

$Status = $Status -replace ”`t|`n|`r”,””

Write-Output ”The current status is ”$Status.trim()” on $(Get-Date)”

}

Catch {

Write-Output $_.Exception

throw $_.Exception

}

# Start block

# Start

Write-Output ”Starting $Operation on $(Get-Date)”

if(($Operation -eq ”Start”) -and ($Status.trim() -ne ”VM running”)){

Write-Output ”Starting $Operation Operation”

try

{

Write-Output ”Starting on $(Get-Date)”

Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName | Start-AzVM

}

catch

{

Write-Output ”Error while executing ”$Operation

}

}

# Stop block

if(($Operation -eq ”Stop”) -and ($Status.trim() -ne ”VM deallocated”)){

write-Output ”Starting $Operation Operation”

try

{

Write-Output ”Deallocating on $(Get-Date)”

Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force

}

catch

{

Write-Output ”Error while executing ”$Operation

}

}

# Restart block

if(($Operation -eq ”Restart”) -and ($Status.trim() -eq ”VM running”)){

Write-Output ”Starting $Operation Operation”

try

{

Write-Output ”Deallocating on $(Get-Date)”

Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force

Write-Output ”Starting on $(Get-Date)”

Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName | Start-AzVM

}

catch

{

Write-Output ”Error while executing ”$Operation

}

    }    }

End

{

# Exit

Write-Output ”Finished process on $(Get-Date)”

}

  1. After pasting the above code, click on save and Publish. After publishing now, we can test this Runbook by clicking on the Start button
  2. After adding the required parameters, a job will be created to do carry out the process, and you will be able to see the logs.

Read more about Automate Azure VM Start/Stop Using Azure Automation

Conclusion

This is how you can automate the process of starting and /stopping of an Azure VM. Through this automation, we can reduce the cost of running VM’s and automate the process hosted on VM, reducing human interaction and errors.

I hope this article will help you. Feel free to leave a comment below if you have questions or need further guidance.

Happy automating! 

Transform Your Azure Experience with AlphaBOLD

Ready to take your Azure operations to the next level? Connect with our team to learn how we can transform your cloud journey.

Request a Consultation

Explore Recent Blog Posts