Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the steps to create a PowerShell script for automating VM shutdowns in Azure


What are the steps to create a PowerShell script for automating VM shutdowns in Azure


To automate the shutdown of Azure Virtual Machines using PowerShell, you can follow these steps:

1. Create an Azure Automation Account:
- Log into the Azure portal and navigate to the "Automation Accounts" section.
- Click on "New Automation Account" and fill in the required details such as name, subscription, resource group, and location.
- Wait for the account to be created.

2. Create a Runbook:
- Navigate to your newly created Automation Account.
- Under "Process Automation," click on "Runbooks," then click on "+ Create a runbook."
- Fill in the details:
- Name: Give your runbook a name, such as `StopIdleVM`.
- Runbook type: Choose `PowerShell`.
- Optionally add a description.
- Click "Create."

3. Add PowerShell Script to the Runbook:
- In the Runbook editor, paste the following PowerShell script:

powershell
     param (
       [string]$ResourceGroupName,
       [string]$VMName
     )

     # Get the VM status
     $vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName

     if ($vm.PowerState -eq "VM running") {
       # Stop the VM if it is running
       Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force
       Write-Output "VM $VMName stopped."
     }
     

- Click "Save" and then "Publish."

4. Create a Schedule for the Runbook:
- In the Runbook, click on "Schedules," then click on "+ Add a schedule."
- Choose "Create a new schedule."
- Fill in the details:
- Name: Give your schedule a name, such as `StopVMIdleSchedule`.
- Start time: Set to your preferred start time.
- Recurrence: Set to "Recurring."
- Frequency: Set it to "Hourly" or as needed.
- Click "Create."

5. Link the Schedule to the Runbook:
- Under "Schedules," select your newly created schedule.
- Set parameters for `ResourceGroupName` and `VMName` by replacing with your actual values.
- Click "OK."

This setup will automatically stop your Azure VM at the scheduled time if it is running. You can modify the script and schedule as needed to fit your specific requirements.

Citations:
[1] https://dev.to/rajeshkumaryadavdotcom/how-to-shutdown-your-azure-virtual-machine-automatically-when-idle-for-x-minute-a-step-by-step-guide-24jn
[2] https://ppolyzos.com/2017/08/15/schedule-startup-and-shutdown-of-your-virtual-machines-using-azure-automation/
[3] https://serverfault.com/questions/747712/azure-script-to-start-or-shut-down-multiple-vms
[4] https://learn.microsoft.com/en-us/azure/virtual-machines/auto-shutdown-vm
[5] https://stackoverflow.com/questions/53302860/powershell-cmdlet-to-gracefully-shutdown-azure-virtual-machine
[6] https://stackoverflow.com/questions/49126320/creating-autoshutdown-policy-with-azure-vm-with-powershell
[7] https://learn.microsoft.com/en-us/powershell/module/az.compute/stop-azvm?view=azps-13.2.0
[8] https://github.com/tomasrudh/AutoShutdownSchedule
[9] https://learn.microsoft.com/en-us/azure/devtest-labs/use-command-line-start-stop-virtual-machines