Cloud Computing

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.

Sponsored Content

Maximize Value from Microsoft Defender

In this ebook, you’ll learn why Red Canary’s platform and expertise bring you the highest possible value from your Microsoft Defender for Endpoint investment, deployment, or migration.

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 Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.
External Sharing and Guest User Access in Microsoft 365 and Teams

This eBook will dive into policy considerations you need to make when creating and managing guest user access to your Teams network, as well as the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

You will learn:

  • Who should be allowed to be invited as a guest?
  • What type of guests should be able to access files in SharePoint and OneDrive?
  • How should guests be offboarded?
  • How should you determine who has access to sensitive information in your environment?

Sponsored by:

 
Office 365 Coexistence for Mergers & Acquisitions: Don’t Panic! Make it SimpleLive Webinar on Tuesday, November 16, 2021 @ 1 pm ET

In this session, Microsoft MVPs Steve Goodman and Mike Weaver, and tenant migration expert Rich Dean, will cover the four most common steps toward Office 365 coexistence and explain the simplest route to project success.

  • Directory Sync/GAL Sync – How to prepare for access and awareness
  • Calendar Sharing – How to retrieve a user’s shared calendar, or a room’s free time
  • Email Routing – How to guarantee email is routed to the active mailbox before and after migration
  • Domain Sharing – How to accommodate both original and new SMTP domains at every stage

Aimed at IT Admins, Infrastructure Engineers and Project Managers, this session outlines both technical and project management considerations – giving you a great head start when faced with a tenant migration.the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

Sponsored by: