Read more on "Server OS Spotlight" »

Starting and Stopping Azure Virtual Machines Using Azure PowerShell

By Nirmal Sharma (Send Email)
Posted May 7, 2018


PowerShell is a great scripting tool. It not only helps you save time, but also provides greater flexibility to execute repeated tasks. Almost all Windows roles and features ship with PowerShell cmdlets.

You can use Windows Server operating system cmdlets to perform operational tasks such as checking the status of a particular Windows service on multiple computers, generating a list of software installed on Windows computers, checking and collecting the roles and features installed on Windows Server operating systems and many more. Windows Server Tutorials

Since most organizations have moved their workloads to public clouds such as Microsoft Azure, they often need to work with virtual machine instances stored in the public cloud. While Azure administrators can use the Azure Portal to manage Azure resources, Azure PowerShell can come in very handy when there's a need to execute a series of routine tasks each day.

For example, you may need to perform start and stop operations against Azure VMs each day or on a weekly basis. Note that Azure VMs are charged based on the usage, so if you have several Azure VMs deployed to run for a few hours each day, you are required to ensure that these virtual machines are turned off to save the cost.

If you do have several virtual machines to work with, you could design a small PowerShell script that can help you shut down the VMs and a second PowerShell script to start the VMs according to specific schedules.

Once the PowerShell scripts have been designed, you can schedule them using the Windows Task Scheduler. To build both the PowerShell scripts, you can use the Azure PowerShell module. The Azure PowerShell module provides Start-AzureRMVM and Stop-AzureRMVM PowerShell cmdlets that can be used to start and stop the Azure virtual machines.

Before you start using the PowerShell scripts provided below, please note the following points:

  • Both the start and stop PowerShell scripts use a single text file residing in C:\Temp that contains the list of Azure virtual machine names.
  • The start and stop operations will only be performed on the list of virtual machines specified in the C:\Temp\AzureVMs.TXT file.
  • It is also important to note that both of these PowerShell scripts target a specific Resource Group in Azure. You will be required to modify the Resource Group Name in both the PowerShell scripts before executing.
  • You will also need to install Azure PowerShell modules on a supported Windows computer in order to run the scripts. The Azure PowerShell modules can be installed from Microsoft Web Platform Installer or PowerShell Gallery.

Script to Stop Azure Virtual Machines

The PowerShell script below performs a stop operation for the VMs specified in the C:\Temp\AzureVMs.TXT file and also generates a report in C:\Temp\VMStopReport.CSV. If the script command generates an error, the error will be logged in the C:\Temp\VMStopReport.CSV file.

### START - Stopping Azure VMs ####

$VMFile = "C:\Temp\AzureVMs.txt"
$ReportFile = "C:\Temp\VMStopReport.CSV"

Foreach ($ThisVMNow in Get-Content "$VMFile")
{

$Error.Clear()
Stop-AzureRmVM -ResourceGroupName "AzureResourceGroup" -Name "$ThisVMNow"
IF ($Error.Count -ne 0)
{
$STR = "Azure Virtual Machine was stopped : " + $ThisVMNow
Add-Content $ReportFile $STR
}
else
{
$STR = "ERROR: Failed to stop Azure Virtual Machine : " + $ThisVMNow+","+$Error[0]
Add-Content $ReportFile $STR
}
}

### END - Stopping Azure VMs ####

Script to Start Azure Virtual Machines

The PowerShell script below performs a start operation for the VMs specified in the C:\Temp\AzureVMs.TXT file and also generate a report in C:\Temp\VMStartReport.CSV. Similar to the script to stop Azure VMs, if this script command generates an error, the error will be logged in the C:\Temp\VMStartReport.CSV file.

### START - Starting Azure VMs ####


$VMFile = "C:\Temp\AzureVMs.txt"
$ReportFile = "C:\Temp\VMStartReport.CSV"

Foreach ($ThisVMNow in Get-Content "$VMFile")
{

$Error.Clear()
Start-AzureRmVM -ResourceGroupName "AzureResourceGroup" -Name "$ThisVMNow"
IF ($Error.Count -ne 0)
{

$STR = "Azure Vitual Machine was Started : " + $ThisVMNow
Add-Content $ReportFile $STR
}
else
{


$STR = "ERROR: Failed to start Azure Virtual Machine : " + $ThisVMNow+","+$Error[0]
Add-Content $ReportFile $STR
}
}

### END - Starting Azure VMs ####

Conclusion

As we explained in this article, it is easy to use PowerShell to manage Azure virtual machine resources. We provided two PowerShell scripts that you can use to stop and start the Azure virtual machines.

You can use these PowerShell scripts on a Windows computer where Azure PowerShell modules are installed and then schedule the scripts based on your requirements using the Windows Task Scheduler.


Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He specializes in directory services, Microsoft Azure, Failover clusters, Hyper-V, System Center and Exchange Servers, and has been involved with Microsoft technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Health Packs for ADHealthProf.ITDynamicPacks.Net solutions. Nirmal can be reached at nirmal_sharma@mvps.org.

Follow ServerWatch on Twitter and on Facebook

Page 1 of 1

Read more on "Server OS Spotlight" »

Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.


 

 


Thanks for your registration, follow us on our social networks to keep up-to-date