Skip to content

Instantly share code, notes, and snippets.

@sqlchow
Last active November 17, 2015 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sqlchow/76e59fd354139bbafb5b to your computer and use it in GitHub Desktop.
Save sqlchow/76e59fd354139bbafb5b to your computer and use it in GitHub Desktop.
Sample test reading wunderlist tasks from azure automation runbooks
workflow wl-provisionvm
{
sequence
{
#Step1: Setup credentials for Azure Automation
#001. Subscription name and credential
$SubscriptionName = "subscriptionname"
$PSCreds = Get-AutomationPSCredential -Name 'wunderlist-deploy'
#002. Add Azure Account
Write-Output "Authenticating to Azure"
Add-AzureAccount -Credential $PSCreds
#003. Select subscription account
Select-AzureSubscription -SubscriptionName $SubscriptionName
#Step2: Get data from Wunderlist
$WunderListObj = (Get-wunderlistlist |
Get-wunderlisttask -Title "@{resourcegroup=*").title |
Invoke-expression
#Step3: Set the Azure resource settings
$location = "North Europe"
$rgName = $WunderListObj.resourcegroup.name
$rgTags = @(@{ Name = "project"; Value = "wunderlist" }, @{ Name = "costCenter"; Value = "007" }, @{ Name = "env"; Value = "blasting" })
$storAccName = $WunderListObj.resourcegroup.storaccount
$storageAccountType = "Standard_LRS"
$vmCount = $WunderListObj.resourcegroup.vm.count
$vmNamePattern = $WunderListObj.resourcegroup.vm.pattern
$vmDataDisk = $WunderListObj.resourcegroup.vm.datadisk
$vmSize = "Basic_A0"
Write-Output "Adding VM Admin Credentials"
$vmAdminUser = "sqlchow"
$vmAdminpassword = 'Pe0pl#Ar3Str@nge'
$vmSecPassword = ConvertTo-SecureString $vmAdminpassword -AsPlainText -Force
$vmAdminCreds = New-Object System.Management.Automation.PSCredential ($vmAdminUser, $vmSecPassword);
$imgPublisherName = "MicrosoftWindowsServer"
$imgOfferingName = "WindowsServer"
$imgSKUName = "2012-R2-Datacenter"
$imgVersion = "latest"
InlineScript{
Write-Output "Provisioning resource group"
#Step1: Provision the resource group
if(!(Test-AzureResourceGroup -ResourceGroupName $using:rgName))
{
Write-Output "Creating Resourcegroup: $using:rgName"
$rg = New-AzureResourceGroup -Name $using:rgName -Location $using:location -Tag $using:rgTags
}
else
{
$rg = Get-AzureResourceGroup -Name $using:rgName
}
Write-Output "Provisioning storage account"
#Step2: Provision the storage account
# Create Storage Account if it doesn't exist
if (!(Test-AzureResource `
-ResourceName $using:storAccName `
-ResourceType "Microsoft.Storage/storageAccounts" `
-ResourceGroupName $using:rgName))
{
$storageAccount = New-AzureStorageAccount `
-Name $using:storAccName `
-ResourceGroupName $using:rgName `
-Location $using:location `
-Type $using:storageAccountType
}
else
{# Get Storage Account if already created
$storageAccount = Get-AzureStorageAccount `
-ResourceGroupName $using:rgName `
-Name $using:storAccName
}
Write-Output "Setting storage account as default"
#Step3: Set new Storage Account as default
Set-AzureSubscription -SubscriptionName $using:SubscriptionName `
-CurrentStorageAccountName $using:storAccName
Write-Output "Provision Network"
#Step4: Provision Network
Write-Output "--Creating Azure VNET"
# Define a unique name for the Azure VNET
$vnetName = "${using:rgName}-vnet"
# Define names for each Subnet within the VNET
$subnet1Name = "${using:rgName}-subnet01"
$subnet2Name = "GatewaySubnet"
# Create Virtual Network if it doesn't exist
if (!(Test-AzureResource `
-ResourceName $vnetName `
-ResourceType "Microsoft.Network/virtualNetworks" `
-ResourceGroupName ${using:rgName})) {
$subnet1 = New-AzureVirtualNetworkSubnetConfig `
-Name $subnet1Name `
-AddressPrefix "10.0.1.0/24"
$subnet2 = New-AzureVirtualNetworkSubnetConfig `
-Name $subnet2Name `
-AddressPrefix "10.0.2.0/28"
$vnet = New-AzureVirtualNetwork `
-Name $vnetName `
-ResourceGroupName ${using:rgName} `
-Location ${using:location} `
-AddressPrefix "10.0.0.0/16" `
-Subnet $subnet1, $subnet2 `
-Tag $using:rgTags
} else {
# Get Virtual Network if already created
$vnet = Get-AzureVirtualNetwork `
-Name $vnetName `
-ResourceGroupName ${using:rgName}
}
Write-Output "--Define public VIP Address"
$publicVip = @()
for ($count = 1; $count -le $using:vmCount; $count++) {
# Define a unique name for the VIP resource
$publicVipName = "${using:rgName}-vip${count}"
# Define a DNS hostname to be assigned to the VIP
$domainName = "${using:rgName}app"
# Define Public VIP Address, if not created
if (!(Test-AzureResource `
-ResourceName $publicVipName `
-ResourceType "Microsoft.Network/publicIPAddresses" `
-ResourceGroupName ${using:rgName}))
{
$publicVip += New-AzurePublicIpAddress `
-Name $publicVipName `
-ResourceGroupName ${using:rgName} `
-Location ${using:location} `
-AllocationMethod Dynamic `
-DomainNameLabel $domainName `
-Tag $using:rgTags
} else {
# Get Public VIP Address if already created
$publicVip += Get-AzurePublicIpAddress `
-Name $publicVipName `
-ResourceGroupName ${using:rgName}
}
}
Write-Output "--Creating network interfaces"
# Define an empty array for holding NIC configs for each VM
$nics = @()
# Create each NIC if not already created
for ($count = 1; $count -le $using:vmCount; $count++) {
$nicName = "${using:rgName}-nic${count}"
if (!(Test-AzureResource `
-ResourceName $nicName `
-ResourceType "Microsoft.Network/networkInterfaces" `
-ResourceGroupName ${using:rgName}))
{
$nicIndex = $count1
# Attach each NIC to related Subnet, NSG rules,
# NAT rules and Load Balancer pool
$nics += New-AzureNetworkInterface `
-Name $nicName `
-ResourceGroupName ${using:rgName} `
-Location $using:location `
-SubnetId $vnet.Subnets[0].Id `
-PublicIpAddressId $publicVip[$nicIndex].Id
} else {
# Get each NIC if already created
$nics += Get-AzureNetworkInterface `
-Name $nicName `
-ResourceGroupName $using:rgName
}
}
Write-Output "Provision VMs"
#Step5: Provision VMs
for ($count = 1; $count -le $using:vmCount; $count++)
{
$vmIndex = $count - 1
$vmName = "${using:vmNamePattern}-${count}"
if (!(Test-AzureResource `
-ResourceName $vmName `
-ResourceType "Microsoft.Compute/virtualMachines" `
-ResourceGroupName $using:rgName))
{
$osDiskLabel = "OSDisk"
$osDiskName = "${vmName}-osdisk"
$osDiskUri = `
$storageAccount.PrimaryEndpoints.Blob.ToString() `
+ "vhds/${osDiskName}.vhd"
$dataDiskSize = 20 # Size in GB
$dataDiskLabel = "DataDisk01"
$dataDiskName = "${vmName}-datadisk01"
$dataDiskUri = `
$storageAccount.PrimaryEndpoints.Blob.ToString() `
+ "vhds/${dataDiskName}.vhd"
$vmConfig = `
New-AzureVMConfig `
-VMName $vmName `
-VMSize $using:vmSize |
Set-AzureVMOperatingSystem `
-Windows `
-ComputerName $vmName `
-Credential $using:vmAdminCreds `
-ProvisionVMAgent `
-EnableAutoUpdate |
Set-AzureVMSourceImage `
-PublisherName $using:imgPublisherName `
-Offer $using:imgOfferingName `
-Skus $using:imgSKUName `
-Version $using:imgVersion |
Set-AzureVMOSDisk `
-Name $osDiskLabel `
-VhdUri $osDiskUri `
-CreateOption FromImage |
Add-AzureVMDataDisk `
-Name $dataDiskLabel `
-DiskSizeInGB $dataDiskSize `
-VhdUri $dataDiskURI `
-CreateOption empty |
Add-AzureVMNetworkInterface `
-Id $nics[$vmIndex].Id `
-Primary
New-AzureVM `
-VM $vmConfig `
-ResourceGroupName $using:rgName `
-Location $using:location `
-Tags $using:rgTags
}
}
} #Inlinescript end
}
Write-Output "Done..."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment