Sample Windows Azure Virtual Machines PowerShell Scripts
I sometimes show a platform that requires a bunch of Windows Azure virtual machines. It has 1 Active Directory domain controller (n123dc1), 3 SQL Server VMs for database mirroring (n123sql1, n123sql2, and n123sp20131 which also happens to have a SharePoint Server 2013 installed), and 2 members of a SharePoint 2012 Web Front End.
Between two demo sessions, I keep the VHD files in Windows Azure blob storage, as well as the virtual network because they don’t cost too much, but I don’t let the virtual machines deployed so that they don’t cost anything.
In order to restart the whole platform I use the following script (I slightly obfuscated one or two values).
#region init
Import-Module 'c:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1'
$subscription = 'Azdem169B44999X'
Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccount 'storrageazure2'
Set-AzureSubscription -DefaultSubscription $subscription
$cloudSvcName = 'n123'
#endregion
#region DC
$vmName='n123dc1'
$disk0Name = 'n123dc1-n123dc1-0-20120912092405'
$disk1Name = 'n123dc1-n123dc1-0-20120912105309'
$vNetName = 'Network123'
$subNet = 'DCSubnet'
$vm1 = New-AzureVMConfig -DiskName $disk0Name -InstanceSize ExtraSmall -Name $vmName -Label $vmName |
Add-AzureDataDisk -DiskName $disk1Name -Import -LUN 0 |
Set-AzureSubnet $subNet |
Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 50101
New-AzureVM -ServiceName $cloudSvcName -VMs $vm1 -VNetName $vNetName
#endregion
#region SQL Server
$vmName='n123sql1'
$disk0Name = 'n123dc1-n123sql1-0-20120912170111'
$disk1Name = 'n123dc1-n123sql1-0-20120913074650'
$disk2Name = 'n123dc1-n123sql1-1-20120913080403'
$vNetName = 'Network123'
$subNet = 'SQLSubnet'
$availabilitySetName = 'SQL'
$sqlvms = @()
$vm1 = New-AzureVMConfig -DiskName $disk0Name -InstanceSize Medium -Name $vmName -Label $vmName -AvailabilitySetName $availabilitySetName |
Add-AzureDataDisk -DiskName $disk1Name -Import -LUN 0 |
Add-AzureDataDisk -DiskName $disk2Name -Import -LUN 1 |
Set-AzureSubnet $subNet |
Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 50604 |
Add-AzureEndpoint -LocalPort 1433 -Name 'SQL' -Protocol tcp -PublicPort 14330
$sqlvms += ,$vm1
$vmName='n123sql2'
$disk0Name = 'n123-n123sql2-0-20121120105231'
$disk1Name = 'n123-n123sql2-0-20121120144521'
$disk2Name = 'n123-n123sql2-1-20121120145236'
$vm2 = New-AzureVMConfig -DiskName $disk0Name -InstanceSize Small -Name $vmName -Label $vmName -AvailabilitySetName $availabilitySetName |
Add-AzureDataDisk -DiskName $disk1Name -Import -LUN 0 |
Add-AzureDataDisk -DiskName $disk2Name -Import -LUN 1 |
Set-AzureSubnet $subNet |
Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 50605 |
Add-AzureEndpoint -LocalPort 1433 -Name 'SQL' -Protocol tcp -PublicPort 14331
$sqlvms += ,$vm2
New-AzureVM -ServiceName $cloudSvcName -VMs $sqlvms
#endregion
#region SharePoint
$vmNamesp1='n123sp1'
$vmNamesp2='n123sp2'
$disk0Namesp1 = 'n123-n123sp1-2012-09-13'
$disk0Namesp2 = 'n123-n123sp2-2012-09-13'
$vNetName = 'Network123'
$subNet = 'SharePointSubnet'
$availabilitySetName = 'WFE'
$spvms = @()
$vm1 = New-AzureVMConfig -DiskName $disk0Namesp1 -InstanceSize Small -Name $vmNamesp1 -Label $vmNamesp1 -AvailabilitySetName $availabilitySetName |
Set-AzureSubnet $subNet |
Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 50704 |
Add-AzureEndpoint -LocalPort 80 -Name 'HttpIn' -Protocol tcp -PublicPort 80 -LBSetName "SPFarm" -ProbePort 80 -ProbeProtocol "http" -ProbePath "/probe/"
$spvms += ,$vm1
$vm2 = New-AzureVMConfig -DiskName $disk0Namesp2 -InstanceSize Small -Name $vmNamesp2 -Label $vmNamesp2 -AvailabilitySetName $availabilitySetName |
Set-AzureSubnet $subNet |
Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 50705 |
Add-AzureEndpoint -LocalPort 80 -Name 'HttpIn' -Protocol tcp -PublicPort 80 -LBSetName "SPFarm" -ProbePort 80 -ProbeProtocol "http" -ProbePath "/probe/"
$spvms += ,$vm2
New-AzureVM -ServiceName $cloudSvcName -VMs $spvms
#endregion
#region SharePoint 2013 and SQL Server witness
$vmName='n123sp20131'
$disk0Name = 'n123-n123sp20131-0-20121120104314'
$disk1Name ='n123-n123sp20131-0-20121120220458'
$vNetName = 'Network123'
$subNet = 'SP2013Subnet'
$availabilitySetName = 'SQL'
$vm1 = New-AzureVMConfig -DiskName $disk0Name -InstanceSize Small -Name $vmName -Label $vmName -AvailabilitySetName $availabilitySetName |
Add-AzureDataDisk -DiskName $disk1Name -Import -LUN 0 |
Set-AzureSubnet $subNet |
Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 50804 |
Add-AzureEndpoint -LocalPort 80 -Name 'HttpIn' -Protocol tcp -PublicPort 8080
New-AzureVM -ServiceName $cloudSvcName -VMs $vm1
#endregion
In order to stop the whole platform, I use the following script:
#region init
Import-Module 'c:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1'
$subscription = 'Azdem169B44999X'
Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccount 'storrageazure2'
Set-AzureSubscription -DefaultSubscription $subscription
$cloudSvcName = 'n123'
#endregion
#region shutdown and delete
echo 'will shut down and remove the following'
Get-AzureVM -ServiceName $cloudSvcName | select name
Get-AzureVM -ServiceName $cloudSvcName | Stop-AzureVM
Get-AzureVM -ServiceName $cloudSvcName | Remove-AzureVM
#endregion
In order to get started, I read Michael Washam’s blog.
Benjamin