GuidesStarting and Stopping Azure Virtual Machines Using Azure PowerShell

Starting and Stopping Azure Virtual Machines Using Azure PowerShell

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




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:TempAzureVMs.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:TempAzureVMs.TXT file and also generates a report in C:TempVMStopReport.CSV. If the script command generates an error, the error will be logged in the C:TempVMStopReport.CSV file.

### START – Stopping Azure VMs ####

$VMFile = "C:TempAzureVMs.txt"
$ReportFile = "C:TempVMStopReport.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:TempAzureVMs.TXT file and also generate a report in C:TempVMStartReport.CSV. Similar to the script to stop Azure VMs, if this script command generates an error, the error will be logged in the C:TempVMStartReport.CSV file.

### START – Starting Azure VMs ####

$VMFile = "C:TempAzureVMs.txt"
$ReportFile = "C:TempVMStartReport.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

Get the Free Newsletter!
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories