WordPress.com



CommonFunctions.ps1

# ===========================================================================================================

# This file will hold all of the common functions used the by the power shell scripts.

# ===========================================================================================================

function IsAdmin {

$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")

return $IsAdmin

}

function IsAzurePowerShellInstalled {

$azurePSInstalled = ((Get-Module -ListAvailable Azure) -ne $null)

}

function Any {

[CmdletBinding()]

Param($evaluationCondition, [Parameter(ValueFromPipeline = $true)] $objectToTest)

begin { $any = $false }

process {

if (-not $any -and (& $evaluationCondition $objectToTest)) {

$any = $true

}

}

end { $any }

}

CreateVirtualMachines.ps1

# ===========================================================================================================

# This file will build up the virtual machines in the cloud

# ===========================================================================================================

# Import Functions From Other Scripts Here

. .\CommonFunctions.ps1

function GetAffinityGroup {

Param([Parameter(Mandatory=$true, HelpMessage="The name of the location to create or get the affinity group for. ie: West US`n")]

[string] $locationName)

[string] $affinityLabel = ('Sample ' + $locationName)

[string] $affinityName = ($affinityLabel.Replace(' ', [System.String]::Empty) + 'Affinity')

$group = Get-AzureAffinityGroup -Name $affinityName -ErrorAction SilentlyContinue

if ($group -eq $null) {

Write-Host 'Unable to find the Affinity Group. Creating a new one.'

New-AzureAffinityGroup -Name $affinityName -Label $affinityLabel -Description 'Affinity Group to be used for the samples' -Location $locationName -Verbose -ErrorAction Stop

}

else { Write-Host ('Affinity Group "' + $affinityName + '" Already Created') }

return (Get-AzureAffinityGroup -Name $affinityName | Select-Object -First 1)

}

function GetStorageAccountForLocation {

Param([Parameter(Mandatory=$true, HelpMessage="The name of the location to get the storage account for. ie: West US`n")]

[string] $locationName,

[Parameter(Mandatory=$true, HelpMessage="The name of the affinity group that you want to assign the storage account to.`n")]

[string] $affinityGroupName)

[string] $storageAccountName = ('Sample' + $locationName.Replace(' ', [System.String]::Empty) + 'Store').ToLowerInvariant()

$storageAccountExists = Test-AzureName -Storage $storageAccountName

if ($storageAccountExists -eq $false) {

Write-Host ('Unable to find a storage account in location ' + $locationName + ', Creating a new one with name: ' + $storageAccountName)

New-AzureStorageAccount -StorageAccountName $storageAccountName -Label ('Sample ' + $locationName + ' Storage Account') -Description 'Sample Storage Account' -AffinityGroup $affinityGroupName -Verbose -ErrorAction Stop

}

else { Write-Host ('Storage Account "' + $storageAccountName + '" Already Created') }

$defaultSubscription = Get-AzureSubscription -Default

if ($defaultSubscription.CurrentStorageAccountName -ne $storageAccountName) {

Write-Host ('Setting the Current Storage Account Name for the default subscription to "' + $storageAccountName + '"')

Set-AzureSubscription -SubscriptionName $defaultSubscription.SubscriptionName -CurrentStorageAccountName $storageAccountName -ErrorAction Stop

}

return (Get-AzureStorageAccount -StorageAccountName $storageAccountName | Select-Object -First 1)

}

if ((IsAdmin) -eq $false) {

throw "You need to be an Administrator or run in Elevated mode for the script to work properly."

}

if ((IsAzurePowerShellInstalled) -eq $false) {

throw "Windows Azure PowerShell not found! Please install from "

}

[string] $cloudServiceName = 'mysamplecloud'

[string] $locationName = 'West US'

$affinityGroup = GetAffinityGroup -locationName $locationName

$storageAccount = GetStorageAccountForLocation -locationName $locationName -affinityGroupName $affinityGroup.Name

[array] $vmConfigs = @()

[string] $imageName = 'a699494373c04fc0bc8f2bb1389d6106__Win2K8R2SP1-Datacenter-201406.01-en.us-127GB.vhd'

[string] $adminName = 'TestAdmin'

[string] $adminPassword = 'SuperSecretP@ssw0rd'

[int] $publicRdpPort = 3389

$numberOfMachines = 2

for ($id = 0; $id -lt $numberOfMachines; $id++) {

$virtualMachineName = ('sample-vm-' + ($id + 1).ToString().PadLeft(2, '0'))

[int] $httpEndPointPort = (80 + $id)

$virtualMachineConfig = New-AzureVMConfig -Name $virtualMachineName -InstanceSize 'Small' -ImageName $imageName |

Add-AzureProvisioningConfig -Windows -AdminUsername $adminName -Password $adminPassword -DisableAutomaticUpdates |

Add-AzureEndpoint -Name "HTTP" -Protocol tcp -LocalPort $httpEndPointPort -PublicPort $httpEndPointPort |

Set-AzureEndpoint -Name 'RDP' -LocalPort 3389 -PublicPort ($publicRdpPort + $id) -Protocol tcp

$vmConfigs += $virtualMachineConfig

}

Write-Host ('Creating ' + $numberOfMachines + ' new Virtual Machines')

New-AzureVM -ServiceName $cloudServiceName -AffinityGroup $affinityGroup.Name -VMs $vmConfigs -WaitForBoot -Verbose -ErrorAction Stop

Write-Host "Virtual Machine(s) Created..."

Write-Host 'done'

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download