When dealing with a group of Azure IaaS VM's, starting/restarting/stopping them can require a lot of button clicking and waiting if you use the Manage Azure website. PowerShell is a much easier way to manage Azure boxes, and can be done from your work machine with an internet connection, without the use of any VPN or remoting technology.

You can start managing your VM's in Azure IaaS with these few simple steps.

  1. Download and install the Azure PowerShell module on your desktop: http://www.windowsazure.com/en-us/documentation/articles/install-configure-powershell/

  2. Open a new Admin PowerShell window and execute the cmdlet Add-AzureAccount. This will launch and authentication screen to temporarily associate the PowerShell Azure module with your Azure account.

  3. Import the Azure PowerShell module into your PowerShell session with the following command. The location below was the default installation location.

<code>Import-Module 'C:Program Files (x86)Microsoft SDKsWindows </code>
<code>AzurePowerShellAzureAzure.psd1'</code>

4.Before using anything with the Azure PowerShell module, you'll need to get the Azure subscription object using the Get-AzureSubscription cmdlet. See an example of this below.

You may have to repeat steps 2, 3, and 4 when starting a new Azure PowerShell session.

One of the first easy things you can do with your Azure VM's is starting and stopping a group of VM's together. The script will run sequentially to start or stop these VM's. If you're like me, you've got a cluster of servers in a SQL 2012 AlwaysOn Availability Group.

You will need to change a few things in these scripts to suit your purposes:

  1. Change "AzureAccount@hotmail.com" to your Azure account, the same one you associated with the PowerShell Azure module earlier. For MSDN-based Azure subscriptions like mine, this address was my Hotmail email address.

  2. Change "Visual Studio Premium with MSDN" to the name of your Azure Subscription. This was the name my MSDN-based account was given by default.

  3. Populate the $vms variable with a list of Azure VM's in the cluster you're looking to start/stop as a group, replacing "yourVMName-dom" and so forth.

Stop a list of Azure Accounts:

Get-AzureSubscription | ? {$_.ActiveDirectoryUserId -eq 'AzureAccount@hotmail.com' -and $_.SubscriptionName -match "Visual Studio Premium with MSDN" } | Set-AzureSubscription -SubscriptionId $_.SubscriptionID
$vmHash =@{}
$vms = "yourVMName-dom","yourVMNamesql1","yourVMNamesql2","yourVMNamewitness"
Get-AzureVM | foreach{$vmHash.Add($_.Name,$_.ServiceName)}
foreach ($vm in $vms) {
$currentVMService = $vmHash[$vm]
Write-Host "Current VM:$($vm)"
$thisvm = Get-AzureVM -ServiceName $currentVMService -Name $vm
Write-Host "Stopping VM:$($thisvm.Name)"
Stop-AzureVM -Name $thisvm.Name -ServiceName $thisvm.ServiceName #-StayProvisioned
}

Note about the -StayProvisioned tag above. Specifying this option will retain some IP settings, but will cause your VM's to continue to accrue Azure credit, even while stopped. Use with care

Start a list of Azure Accounts:

Get-AzureSubscription | ?{$_.ActiveDirectoryUserId -eq 'AzureAccount@hotmail.com' -and $_.SubscriptionName -match "Visual Studio Premium with MSDN" } | Set-AzureSubscription -SubscriptionId $_.SubscriptionID
$vmHash =@{}
$vms = "yourVMName-dom","yourVMNamesql1","yourVMNamesql2","yourVMNamewitness"
Get-AzureVM | foreach{$vmHash.Add($_.Name,$_.ServiceName)}
foreach ($vm in $vms) {
$currentVMService = $vmHash[$vm]
Write-Host "Current VM:$($vm)"
$thisvm = Get-AzureVM -ServiceName $currentVMService -Name $vm
Write-Host "Starting VM:$($thisvm.Name)"
Start-AzureVM -Name $thisvm.Name -ServiceName $thisvm.ServiceName
}

Like me, I think you'll find that using PowerShell to manage your Azure IaaS VM's is much easier and faster, while also providing yet another window into the remarkable versatile world of PowerShell scripting.

Big thanks and credit for the assist on this blog post to Cody Gros, SharePoint Solutions Architect and my coworker at Sparkhound.


Information and material in our blog posts are provided "as is" with no warranties either expressed or implied. Each post is an individual expression of our Sparkies. Should you identify any such content that is harmful, malicious, sensitive or unnecessary, please contact marketing@sparkhound.com

Get Email Notifications