Tinkering with Azure using PowerShell.  The follow code reads a CSV file to build a group of VM’s using ARM on a shared subnet / network.  When I get my brain electrified enough, I will move it from CSV to JSON and remove one of my training wheels.  I saved this code as “New-AzureLabGroup.ps1

image

param (
  [parameter(Mandatory=$False)] [string] $azEnv = "AzureCloud",
  [parameter(Mandatory=$False)] [string] $azAcct = "<your_login_ID>",
  [parameter(Mandatory=$False)] [string] $azTenId = "<your_tenant_ID>",
  [parameter(Mandatory=$False)] [string] $azSubId = "<your_subscription_ID>",
  [parameter(Mandatory=$False)] [string] $InputFile = "azurelab.csv",
  [parameter(Mandatory=$False)] [string] $rgName = "zzResGroup1",
  [parameter(Mandatory=$False)] [string] $saName = "zzstacct1",
  [parameter(Mandatory=$False)] [string] $subnetPfx = "10.0.0.0/24",
  [parameter(Mandatory=$False)] [string] $addressPfx = "10.0.0.0/16",
  [parameter(Mandatory=$False)] [string] $Location = "eastus",
  [parameter(Mandatory=$False)] [switch] $TestMode = $False,
  [parameter(Mandatory=$False)] [string] $subnetName = "zzsubnet1",
  [parameter(Mandatory=$False)] [string] $vnetName = "zzvnet1"
)

Write-Output "checking if session is authenticated..."
if ($azCred -eq $null) {
  Write-Output "authentication is required."
  $azCred = Login-AzureRmAccount -EnvironmentName $azEnv -AccountId $azAcct -SubscriptionId $azSubId -TenantId $azTenId
}
else {
  Write-Output "authentication already confirmed."
}

Write-Output "reading input file: $InputFile..."
$csvData = Import-Csv $InputFile

if ($csvData -ne $null) {
  $rg = Get-AzureRmResourceGroup -Name $rgName -Location $Location -ErrorAction SilentlyContinue
  if ($rg -eq $null) {
    Write-Output "creating resource group: $rgName..."
    $rg = New-AzureRmResourceGroup -Name $rgName -Location $Location
  }
  else {
    Write-Output "resource group already exists: $rgName"
  }
  $stAcct = Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $saName -ErrorAction SilentlyContinue
  if ($stAcct -eq $null) {
    Write-Output "creating storage account: $saName..."
    $stAcct = New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $saName -SkuName Standard_LRS -Kind Storage -Location $Location -ErrorAction SilentlyContinue
  }
  else {
    Write-Output "storage account already exists: $saName"
  }
  if ($stAcct -ne $null) {
    $stURI = $stAcct.PrimaryEndpoints.Blob.ToString()
    Write-Output "storage account URI: $stURI"
  }
  if ($laCred -eq $null) {
    Write-Output "setting credentials for NEW local admin account..."
    $laCred = Get-Credential -Message "Enter name and password of the new admin account."
  }

  Write-Output "creating virtual network: $subnetName / $subnetPfx..."
  $singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $subnetPfx
  $vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $Location -AddressPrefix $addressPfx -Subnet $singleSubnet

  foreach ($row in $csvData) {
    $vmName = $row.Name
    $vmOS = $row.OS
    $vmSize = $row.Size
    $nicName = "$vmName"+"nic1"
    if ($TestMode -ne $True) {
      $ipName = "$vmName"+"pip1"
      Write-Output "creating public IP: $ipName..."
      $pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $Location -AllocationMethod Dynamic

      Write-Output "creating NIC: $nicName..."
      $nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id

      Write-Output "preparing components for virtual machine..."

      $vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
      $vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $compName -Credential $laCred -ProvisionVMAgent -EnableAutoUpdate
      $vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus $vmOS -Version "latest"
      $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
     
      $blobPath = "$vmName"+"os.vhd"
      $osDiskUri = "$stURI"+"vhds/$blobPath"
      $diskName = "$vmName"+"osdisk"
      $vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption FromImage

      Write-Output "creating virtual machine: $vmName..."
      New-AzureRmVM -ResourceGroupName $rgName -Location $Location -VM $vm

      $vmnic = Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName
      $pvtIP = $($vmnic.IpConfigurations).PrivateIpAddress
      $pubIP = $(Get-AzureRmPublicIpAddress -Name vm02pip1 -ResourceGroupName rg3).IpAddress
      Write-Output "`tPrivate IP: $pvtIP"
      Write-Output "`tPublic IP: $pubIP"
    }
    else {
      Write-Output "testmode enabled."
      Write-Output "vm: $vmName / size: $vmSize / os: $vmOS / Location: $Location"
      $blobPath = "$vmName"+"os.vhd"
      $osDiskUri = "$stURI"+"vhds/$blobPath"
      $diskName = "$vmName"+"osdisk"
      Write-Output "`tstorage blob: $osDiskUri"
      Write-Output "`tstorage disk: $diskName"
    }
  }
}

Write-Output "Finished!!!"

Below is a sample CSV file to use.  Save it as “azurelab.csv” in the same folder where the PS1 file above is saved.  Alternatively, you can use the -InputFile parameter to reference a different CSV file.

Name,Size,OS
vm01,Standard_A1,2012-R2-Datacenter
vm02,Standard_A1,2012-R2-Datacenter
vm03,Standard_A1,2012-R2-Datacenter

Cheesy and held together with chewing gum, but it works.  Here’s a bonus: When you’re done and want to wipe it out and try again, use the following cmdlet statement:

Remove-AzureRmResourceGroup -Name $rgName -Force
Advertisements

One thought on “Annoying Azure with PowerShell

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s