Saturday, September 6, 2014

Deploy 10 Virtual Machines in Azure

 

Prompts for ServiceName, also securely passes Admin/Pass to Add-AzureProvisioningConfig

Creates 10 Servers hostname: Thing#

 

<#

 

.NOTES

Author: Drew Robinson

Last Updated: 9/2/2014

#>

 

 

# Change color to yellow, prompt human for Servicename, reset color back to default

[console]::ForegroundColor = "yellow"

 

$Servicename = Read-Host -Prompt "Please Enter Servicename for this deployment: (servicename is the internet facing name, [name].cloudapp.net)"

 

[console]::ResetColor()

 

# Get Credentials (secure)

$Cred=Get-Credential -Message "Please Enter the Administrator Username and Password for the VMs, note: you can't use Admin/Administrator, password must be complex"

 

# Get-VM Images and filter Windows Server 2012 R2

$VMImages=Get-AzureVMImage |Where-Object {($_.PublisherName -ilike "Microsoft*" -and $_.ImageFamily -ilike 'Windows Server 2012 R2 Datacenter') }

 

# 1..10 = array of of 10 things, $_ = put array # here

 

1..10 |ForEach {$VMName = "thing$_"

New-AzureVMConfig -Name $VMName -InstanceSize "ExtraSmall" -ImageName $vmimages[1].ImageName |Add-AzureProvisioningConfig -Windows -AdminUsername $cred.UserName -Password $Cred.GetNetworkCredential().password -TimeZone "Central Standard Time" -Verbose |Add-AzureDataDisk -CreateNew -DiskSizeInGB '15' -DiskLabel "disk 1" -LUN '0' |New-AzureVM -location 'South Central US' -servicename $Servicename -Verbose

}

 

 

 

How to connect/bind an Azure subscription to Powershell



Here's how you  bind an Azure subscription to Powershell:


1. Click this link to download Azure Powershell

2. After you have installed Azure Powershell, login to the azure management portal 

3. Click this Link to Download you Azure Publishing file, save this file to a secure location as it has your management certificate and subscription information. 

4. In Azure Powershell, CD into the folder where the Publishing file was save.

Type the following command, Tip: you can use TAB key to complete the command

Import-AzurePublishSettingsFile .\[filename]


If you want to deploy Virtual Machines via powershell you must bind the default storage account. Type the following command

Get-AzureSubscription

If the currentstorage account is blank you'll need to define this.

Setting the Current Storage Account
1. From the Management portal, copy your Storage Account name (must be in the same region as your deployment)
2. Type the following Powershell


$AzSub = Get-AzureSubscription 
Set-AzureSubscription -SubscriptionName $AzSub.SubscriptionName -CurrentStorageAccountName [typenamehere]



Type Get-AzureSubscription

you should now see the CurrentStorageAccount is defined.