Set Azure Resource Manager VM AvailabilitySet

This script will allow you to : - Add an ARM VM to an AvailabilitySet - Change an ARM VM’s AvailabilitySet - Remove an ARM VM from an AvailabilitySet ****Please rate this contribution if it helps you****

 
 
 
 
 
4.5 Star
(38)
7,331 times
Add to favorites
Windows Azure
6/22/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Hello...is there a NEW full script
    1 Posts | Last post February 01, 2018
    • I tried this with vers. 1.05 which failed but then noticed it said it probably would after June 2016. So is there a new complete version of the script ? One that supports managed disks , etc. ? Thanks 
  • Managed Disk
    1 Posts | Last post August 28, 2017
    • Hi Samir,
      
      Thanks for this script. 
      
      Does it support Managed Disks?
      
      Cheers
  • nothing get created
    1 Posts | Last post August 25, 2017
    • Modified the script as per last update from Jamesatighe.
      It runs through without issues but doesnt actually create the VM without any error message.
      
      
      Validating the Vm Existence  ... 
      WARNING: Breaking change notice: In upcoming release, top level properties, DataDiskNames and NetworkInterfaceIDs, will be removed from VM object because they are also in StorageProfile and Net
      workProfile, respectively.
      Validating the As Existence  ... 
      Validation of [schwpradfs1] and [ASADFSAUEPR2] : Success
      Getting the VM Object : schwpradfs1  ... 
      WARNING: Breaking change notice: In upcoming release, top level properties, DataDiskNames and NetworkInterfaceIDs, will be removed from VM object because they are also in StorageProfile and Net
      workProfile, respectively.
      WARNING: Breaking change notice: In upcoming release, top level properties, DataDiskNames and NetworkInterfaceIDs, will be removed from VM object because they are also in StorageProfile and Net
      workProfile, respectively.
      Stopping the VM : schwpradfs1  ... 
      Exporting the VM Config to a file : RGAUEPRD1-schwpradfs1.json   ... 
      Getting the Availability Set : ASADFSAUEPR2   ... 
      Recreating the Azure VM [schwpradfs1] : (Step 1 : Removing the VM...)   ... 
      Recreating the Azure VM [schwpradfs1] : (Step 2 : Creating the VM...)   ... 
      
  • Hi I don't suppose you would be prepared to update the script to support managed Disks ?
    5 Posts | Last post August 10, 2017
    • Unfortunately it just deletes the VM and fails if you use managed disks
    • Firstly thanks for the script Samir! 
      
      I have added a little bit to the script to allow for VMs with managed disks as this would not work without a little change.
      
      You need to know the Storage Account type of each disk which I have added to the script parameters as below
      
         [Parameter(Mandatory=$False, Position=3)]
         [string]$OSStorage,
      
         [Parameter(Mandatory=$False, Position=3)]
         [string]$DataStorage
      
      Then in the Set-AsSetting function simply add the following script to accommodate for Managed Disks
      
      # New section added to allow for managed disks
      if ($VmObject.StorageProfile.OsDisk.VHD -eq $null) 
      {
          $VMObject.StorageProfile.OsDisk.ManagedDisk.Id = (Get-AzureRMDisk | ?{$_.name -eq $VMObject.StorageProfile.OsDisk.Name }).Id
          $VmObject.StorageProfile.OsDisk.ManagedDisk.StorageAccountType = $OSStorage
      }
      $VmObject.StorageProfile.OsDisk.CreateOption = 'Attach'
      
      for ($s=1;$s -le $VmObject.StorageProfile.DataDisks.Count ; $s++ )
      {
      $VmObject.StorageProfile.DataDisks[$s-1].CreateOption = 'Attach'
      if ($VmObject.StorageProfile.DataDisks[$s-1].vhd -eq $null)
      {
          $VmObject.StorageProfile.DataDisks[$s-1].ManagedDisk.Id = (Get-AzureRMDisk | ?{$_.name -eq $VmObject.StorageProfile.DataDisks[$s-1].Name }).Id
          $VmObject.StorageProfile.DataDisks[$s-1].ManagedDisk.StorageAccountType = $DataStorage
      }
      
      I have tested this and it works like a charm. This means the script will now work for both vhd blobs and managed disks.
      
      The usage will be the same except for the Storage Account Type being added as below
      
      ./Set-ArmVmAvailabilitySet.ps1 -VmName <VMName> -AvailabilitySetName <AS Name> -ResourceGroup <Resource Group> -OSStorage <StandardLRS or PremiumLRS etc> -DataStorage <StandardLRS or PremiumLRS etc>
      
      Hopefully this will help a few people.
    • Hi James,
      
      I've tried adding code provided by you but it is throwing error in powershell console. It will be a great help if you can provide complete modified script which is working for you for managed disk.
    • Hi HKulshreshtha,
      
      Assuming the above is all correct, then there is a missing } at the end.
      
      {
          $VmObject.StorageProfile.DataDisks[$s-1].ManagedDisk.Id = (Get-AzureRMDisk | ?{$_.name -eq $VmObject.StorageProfile.DataDisks[$s-1].Name }).Id
          $VmObject.StorageProfile.DataDisks[$s-1].ManagedDisk.StorageAccountType = $DataStorage
      }
      should be 
      {
          $VmObject.StorageProfile.DataDisks[$s-1].ManagedDisk.Id = (Get-AzureRMDisk | ?{$_.name -eq $VmObject.StorageProfile.DataDisks[$s-1].Name }).Id
          $VmObject.StorageProfile.DataDisks[$s-1].ManagedDisk.StorageAccountType = $DataStorage
      }
      }
      
      
    • Hi,
      
      I tried this additional code, and it worked for the first VM, but failed for the second one. The reason was these parts, which would return all the disk IDs for some reason:
      
      (Get-AzureRMDisk | ?{$_.name -eq $VMObject.StorageProfile.OsDisk.Name }).Id)
      and
      (Get-AzureRMDisk | ?{$_.name -eq $VmObject.StorageProfile.DataDisks[$s-1].Name }).Id
      
      So instead, I replaced the above with the following:
      
      (Get-AzureRMDisk -ResourceGroupName $VmRG -DiskName $($VMObject.StorageProfile.OsDisk.Name)).Id
      and
      (Get-AzureRMDisk -ResourceGroupName $VmRG -DiskName $($VmObject.StorageProfile.DataDisks[$s-1].Name)).Id
      
      Hope this helps.
  • issue with diagnostics storage account
    1 Posts | Last post May 31, 2017
    • Hi Samir,
      
      The script works almost flawlessly for me. It does redeploys the machine, however I have a DS machine, and when it is chooses a diagnostics storage account, it chooses the storage account from another region machine is in WE and gets the storage account from NE for diag... Which machine of course fails to start until I remove diag.
      
      It also configures diagnostics for machines which did not had it turned on before. 
      I would appreciate your thoughts on this. I saw other also had some diag related issues.
      
      Thanks,
      Daniel
       
  • How to recreate VM from json?
    3 Posts | Last post February 22, 2017
    • I lost the VM when I ran the script.
      How can I restore that VM from JSON?
      Can you please the exact command.  
      
    • gwdacsap02d
      
      Private IP address
      10.6.5.4
      
      10.6.5.4
      
      
      Canada Central
      Virtual network/subnet
      main-vnet/Adobe-Campaign-Dev-App-Subnet
      
      gwdacsapst03
      
      I can answer it myself, may be will help others too 
      
      Log on your Azure account  by Add-AzureRmAccount and run below commands one by one:
       
       
      #Choose subscription 
      Select-AzureRmSubscription -SubscriptionId <Specify-SubscriptionId>
       
       
       
      #Get Network Interface
       
      $nic = Get-AzureRmNetworkInterface -ResourceGroupName  <Specify-resource-group-name> -Name <Specify-nic-name>
       
       
       
      #Create Availability Set and get it
       
      New-AzureRmAvailabilitySet -ResourceGroupName <Specify-resource-group-name> -Name <Specify name for availability set> -Location "<Specify Location for example Canada Central>"
       
       
       
      $avset = Get-AzureRmAvailabilitySet -ResourceGroupName <Specify-resource-group-name> -Name <the name specified in last command>
       
       
      #Set VM name, size and availability set
      $vmconfig = New-AzureRmVMConfig -VMName "<Specify-VM-name>" -VMSize "Standard_F2s" -AvailabilitySetId $avset.ID
       
      # Set VM OS disk
      $vmconfig = Set-AzureRmVMOSDisk -VM $vmconfig -Name osdisk -VhdUri <Specify VHD URL> -CreateOption attach -Linux
       
      #Set VM data disk
      $vmconfig = Add-AzureRmVMDataDisk -VM $vmConfig -Name appdisk01 -VhdUri <Specify VHD URL> -Lun 0 -CreateOption attach -Caching none -DiskSizeInGB 500
       
      #Set Network Interface
      $vmconfig = Add-AzureRmVMNetworkInterface -VM $vmconfig -Id $nic.Id
       
      #Deploy VM
      New-AzureRmVM -ResourceGroupName <Specify-resource-group-name> -vm $vmconfig -Location "<Specify Location for example Canada Central>"
    • I can answer it myself, may be will help others too 
      
      Log on your Azure account  by Add-AzureRmAccount and run below commands one by one:
       
       
      #Choose subscription 
      Select-AzureRmSubscription -SubscriptionId <Specify-SubscriptionId>
       
       
       
      #Get Network Interface
       
      $nic = Get-AzureRmNetworkInterface -ResourceGroupName  <Specify-resource-group-name> -Name <Specify-nic-name>
       
       
       
      #Create Availability Set and get it
       
      New-AzureRmAvailabilitySet -ResourceGroupName <Specify-resource-group-name> -Name <Specify name for availability set> -Location "<Specify Location for example Canada Central>"
       
       
       
      $avset = Get-AzureRmAvailabilitySet -ResourceGroupName <Specify-resource-group-name> -Name <the name specified in last command>
       
       
      #Set VM name, size and availability set
      $vmconfig = New-AzureRmVMConfig -VMName "<Specify-VM-name>" -VMSize "Standard_F2s" -AvailabilitySetId $avset.ID
       
      # Set VM OS disk
      $vmconfig = Set-AzureRmVMOSDisk -VM $vmconfig -Name osdisk -VhdUri <Specify VHD URL> -CreateOption attach -Linux
       
      #Set VM data disk
      $vmconfig = Add-AzureRmVMDataDisk -VM $vmConfig -Name appdisk01 -VhdUri <Specify VHD URL> -Lun 0 -CreateOption attach -Caching none -DiskSizeInGB 500
       
      #Set Network Interface
      $vmconfig = Add-AzureRmVMNetworkInterface -VM $vmconfig -Id $nic.Id
       
      #Deploy VM
      New-AzureRmVM -ResourceGroupName <Specify-resource-group-name> -vm $vmconfig -Location "<Specify Location for example Canada Central>"
  • will there be an impact in production if I use this Power Shell Script
    2 Posts | Last post December 29, 2016
    • Our Requirement is to Create two Virtual Machines using an existing image (specialised) and both the virtual machine should be in a single availability set.
      
      1) Will there be any impact in the production like application crash
      2) Do we need to take any pre-cautionary measures to make sure this task should get success          even if the existing VM is in Production.
      
      I had already tried this PowerShell script in a lab environment, it is working absolutely fine but still we would like to know any pre-cautionary measures has to be taken before proceeding this task. please suggest.
    • 1) What do you mean ? you need to know that the virtual machines under an AS should be configured to work as a load balanced farm or a cluster, which means that they can make the same work independently.2 VMs under a Availability Set means that if a VM crashes do an Azure issues (Hardware, network, hyper-visor, wrong fabric update), the second VM will not fail too
      
      2) If you will use the script on a production VM, keep in mind that if the script fail after the removal step, your VM will no longer exist, but i have made an Export step to export the configuration of the VM to a local file. This way you will be able to create it again (The disks of the VM will never be removed anyway)
  • Could you update script for diagnostic settings?
    1 Posts | Last post October 05, 2016
    • Could you update the script to select the same diagnostics storage and enable diagnostics as per original VM.  One can do it by hand but it ends up calling the friendly names of the diagnostics something different to the original VM.  PS also resource health break when doing this.  PPS did you notice computer name turns to '-' in the essential pane - apparently a platform issue?
  • Can I download a previous version of Azure PowerShell?
    2 Posts | Last post September 23, 2016
    • Hi Samir,
      
      I need to add a VM to a availability set (forgot to do it during the VM creation), and I found your great script.
      
      I have seen on the description that the script is likely to fail with the latest version of Azure PowerShell. :(
      
      Is there a way that you know of that I can get a previous version of the Azure PS Cmdlets from somewhere?
      
      Thanks!
      P.
    • Actually since everyone else here said they recently tried the script (as far as Sep/16) and it work, I gave it a try and all went well. As someone else said, I had to reinstall extensions and set the diagnostics again (but I took a screenshot before I made the move).
      
      The script does give a warning that the current usage of tags in the scripts is going to be deprecated soon. So the script will have to be updated then.
  • How can I configure a New VM to attach to an existing AS
    1 Posts | Last post September 23, 2016
    • I tried the script and it failed and deleted the 2nd VM as you said it might.  So I'm trying to create a new VM and configure it to be attached to the Avail. Set upon creation and have been searching for hours and I can't find anything about doing it in PowerShell.  I found info on doing it in the portal, but I need to use the C: drive blob and can only do that when I create the VM in PowerShell.  
      
      And in general, I don't understand why going to the Resource Manager/Portal will cause you to lose functionality?
      
      Thanks
1 - 10 of 29 Items