Buy Me a Coffee

Buy Me a Coffee!

Wednesday, May 14, 2014

Create SharePoint lab in Azure using Powershell

Ok, to begin standing up our lab in Azure we will need to first provision a couple of infrastructure items and then build the VMs themselves.  This corresponds to the first three items in Planning the flow of the Azure lab creation script article:
  1. Create a private network and affinity group
  2. Create storage account
  3. Create the virtual machines and start them if they don't start automatically
I ended up creating the Affinity Group first, then the Storage Account and I had trouble getting the Private Network setup, because I was misunderstand what was going on.  The XML file you download should be edited and sent back up.  Here is the script I have come up with so far, which covers the first two bullets (although in a slightly different order):

# ========================================================
#
# Script Information
#
# Title: CreateLab
# Author: Larry Smithmier
# Originally created: 5/13/2014 - 21:11:25
# Original path: CreateLab.ps1
# Description: Create a SharePoint lab in Azure
#
# ========================================================
[CmdletBinding()]
param(
[Parameter(Mandatory = $True, Position = 1)]
[string]$subscriptionName,

[Parameter(Mandatory = $True)]
[string]$labName
)
Add-AzureAccount
Write-Host "Creating " -NoNewline
Write-Host $labName -ForegroundColor Yellow -NoNewline
Write-Host " using the " -NoNewline
Write-Host $subscriptionName -ForegroundColor Yellow -NoNewline
Write-Host " Azure subscription"
Write-Host "=============================================================="

##############################################################################
Write-Host
Write-Host "1. Selecting Subscription " -NoNewline
Write-Host $subscriptionName -ForegroundColor Yellow
select-AzureSubscription $subscriptionName

##############################################################################
Write-Host
Write-Host "2. Creating Affinity Group " -NoNewline
Write-Host $labName -ForegroundColor Yellow

New-AzureAffinityGroup -Name $labName -Location "East US" -Label "East US $labName" -Description "Affinity Group for the $labName Lab in the East US region"

##############################################################################
Write-Host
Write-Host "3. Create a new Storage Account " -NoNewline
Write-Host $labName -ForegroundColor Yellow

$guid = [guid]::NewGuid()
$storageName = "$labName$guid"
$storageName = $storageName.ToLower().Replace("-","").Substring(0,23)
New-AzureStorageAccount -StorageAccountName $storageName -Label $labName -AffinityGroup $labName

##############################################################################
Write-Host
Write-Host "4. Update the Network Configuration file to contain " -NoNewline
Write-Host $labName -ForegroundColor Yellow

#using simple text replacement
$configurationFileName = Get-ChildItem ".\NetworkConfig.netcfg"

Get-AzureVNetConfig -ExportToFile $configurationFileName.FullName

$XMLFile = New-Object xml
$XMLFile.Load($configurationFileName.FullName)

$xml = New-Object xml
$xml = $XMLFile.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.ChildNodes[0].Clone()
$xml.name = $labName
$xml.AffinityGroup = $labName

$XMLFile.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.AppendChild($xml)

$configurationFileName = $configurationFileName.FullName.Replace("NetworkConfig","$labName")

$XMLFile.Save($configurationFileName)

##############################################################################
Write-Host
Write-Host "5. Creating Azure Network using the configuration file " -NoNewline
Write-Host $configurationFileName.FullName -ForegroundColor Yellow

Select-AzureSubscription $subscriptionName
Set-AzureSubscription $subscriptionName -CurrentStorageAccount $storageName
Remove-AzureVNetConfig -ErrorAction SilentlyContinue
Set-AzureVNetConfig -ConfigurationPath $configurationFileName