Stop or Start VMs in an Windows Azure Subscription Using PowerShell Loop

To absolute beginners, PowerShell can look complex. And it is a more powerful tool than VBScript or other command line programs traditionally native to Windows. In this Ask the Admin, I’ll show you how to create a simple loop function to automatically run a series of commands to start or stop VMs running in Windows Azure.

How to Start/Stop All VMs in an Azure Subscription with PowerShell

The Windows Azure web-based management portal is a useful tool for beginners, but when cloud environments grow, trying to manage virtual machines using the management portal can be a pain. I wanted a quick way to make sure that all VMs in an Azure subscription were stopped and deallocated from the Azure fabric. It turns out that it is quite easy if all VMs are running in the same cloud service, but if they’re not, you need to create a simple PowerShell loop.

Using a Wildcard to Stop or Start Multiple VMs

If all the VMs you want to start or stop are running in the same cloud service, you can use a wildcard to issue a command to all the VMs. For more information on using PowerShell to manage Windows Azure, see “Setup Windows Azure PowerShell Management” on Petri. (Related: Download Windows Azure Trial.)

  • Switch to the Start screen in Windows 8, type powershell, and making sure that Windows PowerShell is selected in the search results, and press CTRL+SHIFT+ENTER to start the console with administrative privileges. Enter credentials or give consent if prompted.
  • In the PowerShell console, connect to your Azure subscription. For more information on how to get your Azure subscription name and storage account information, see “Use PowerShell to Create a New Virtual Machine in Windows Azure Running Server 2012 R2.” To connect to my Azure subscription, I will run the following command: Set-AzureSubscription “Pay-As-You-Go” -CurrentStorageAccount portalvhdsxgwgzn2ml54p5 and press Enter.
  • Now type Start-AzureVM -Servicename contosodc1new -name “contoso*” and press Enter, replacing contosodc1new with the Azure cloud service name that your VMs are running in, and replace contoso* with text that can be used to identify the VMs by name.

The Start-AzureVM command will start all VMs in the contosodc1new cloud service with names beginning with contoso. The * is a wildcard that allows the command to match VMs with names starting with ‘contoso’. Despite only running the Start-AzureVM command once in this example, it will start multiple VMs matching the wildcard if they exist.

Start Virtual Machines in a Subscription Using a PowerShell Loop

Unfortunately, it’s not possible to use a wildcard in the –ServiceName parameter when using the Start-AzureVM or Stop-AzureVM commands. To stop VMs running across different cloud services, you’ll need to enumerate all the services and create a loop to stop or start the VMs.

  • Make sure you are running a PowerShell console with local administrative privileges and are connected to your Azure subscription. See the instructions above.
  • In the PowerShell console, type Get-AzureService | Foreach-Object { Stop-AzureVM -ServiceName $_.ServiceName -Name “*” –force –verbose } and press Enter.

The above command will stop all running VMs in your Azure subscription and deallocate their associated resources from the Azure fabric.

A PowerShell Foreach-Object loop
A PowerShell Foreach-Object loop.

To understand how this works, run through the following commands in the PowerShell console:

  • Type Get-AzureService and press Enter. You’ll see a list of all the cloud services in your subscription with all their associated information.
  • Now type Get-AzureService | Select ServiceName and press Enter. You’ll see just a list of the cloud service names without all the additional information. From this command, you can see it’s possible just to extract specific information.

The Get-AzureService command in the loop above enumerates the cloud service information, and then the Foreach-Object loop pipes the results in turn to the Stop-AzureVM command using the $_.ServiceName variable.

Related Articles: