/asr-automation-recovery/scripts/ASR-AddSingleNSGPublicIp.ps1

https://github.com/Azure/azure-quickstart-templates · Powershell · 136 lines · 74 code · 25 blank · 37 comment · 11 complexity · 49449736b3e2878b4c1b5e110c7a9ae7 MD5 · raw file

  1. <#
  2. .DESCRIPTION
  3. This will create a Public IP address for the failed over VM - only in test failover.
  4. Pre-requisites
  5. 1. when you create a new Automation Account, make sure you have chosen to create a run-as account with it.
  6. 2. If you create a run as account on your own, give the Connection Name in the variable - $connectionName
  7. What all you need to change in this script?
  8. 1. Give the name of the Automation account in the variable - $AutomationAccountName
  9. 2. Give the Resource Group name of the Automation Account in $AutomationAccountRg
  10. Do you want to add a NSG to the failed over VM? If yes, follow the below steps - you can skip this step if you dont want to add an NSG.
  11. 1. Create the NSG that you want to apply
  12. 2. Create a new Azure automation string variable <RecoveryPlanName>-NSG (example testrp-NSG). Save it with the value of the NSG you want to use.
  13. 3. Create a new Azure automation string variable <RecoveryPlanName>-NSGRG (example testrp-NSGRG). Save it with the value of the NSG's Resource group you want to use.
  14. How to add the script?
  15. Add this script as a post action in boot up group for which you need a public IP. All the VMs in the group will get a public IP assigned.
  16. If the NSG parameters are specified, all the VM's NICs will get the same NSG attached.
  17. Clean up test failover behavior
  18. Clean up test failover will not delete the IP address. You will need to delete the IP address manually
  19. .NOTES
  20. AUTHOR: RuturajD@microsoft.com
  21. LASTEDIT: 27 January, 2017
  22. #>
  23. workflow ASR-AddSingleNSGPublicIp {
  24. param (
  25. [parameter(Mandatory=$false)]
  26. [Object]$RecoveryPlanContext
  27. )
  28. $connectionName = "AzureRunAsConnection"
  29. $AutomationAccountName = "" #Fill this up with you Azure Automation Account Name
  30. $AutomationAccountRg = "" #Fill this up with you Azure Automation Account Resource Group
  31. # This is special code only added for this test run to avoid creating public IPs in S2S VPN network
  32. if ($RecoveryPlanContext.FailoverType -ne "Test") {
  33. exit
  34. }
  35. try
  36. {
  37. # Get the connection "AzureRunAsConnection "
  38. $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
  39. "Logging in to Azure..."
  40. #Add-AzureRmAccount `
  41. Login-AzureRmAccount `
  42. -ServicePrincipal `
  43. -TenantId $servicePrincipalConnection.TenantId `
  44. -ApplicationId $servicePrincipalConnection.ApplicationId `
  45. -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
  46. }
  47. catch {
  48. if (!$servicePrincipalConnection)
  49. {
  50. $ErrorMessage = "Connection $connectionName not found."
  51. throw $ErrorMessage
  52. } else{
  53. Write-Error -Message $_.Exception
  54. throw $_.Exception
  55. }
  56. }
  57. $VMinfo = $RecoveryPlanContext.VmMap | Get-Member | Where-Object MemberType -EQ NoteProperty | select -ExpandProperty Name
  58. Write-output $RecoveryPlanContext.VmMap
  59. Write-output $RecoveryPlanContext
  60. # Get the NSG based on the name
  61. # if he has not passed this value just create the public IP and go ahead
  62. $NSGValue = $RecoveryPlanContext.RecoveryPlanName + "-NSG"
  63. $NSGRGValue = $RecoveryPlanContext.RecoveryPlanName + "-NSGRG"
  64. Write-Output $NSGValue
  65. Write-Output $NSGRGValue
  66. $NSGnameVar = Get-AzureRMAutomationVariable -AutomationAccountName $AutomationAccountName -Name $NSGValue -ResourceGroupName $AutomationAccountRg
  67. $RGnameVar = Get-AzureRMAutomationVariable -AutomationAccountName $AutomationAccountName -Name $NSGRGValue -ResourceGroupName $AutomationAccountRg
  68. $NSGname = $NSGnameVar.value
  69. $NSGRGname = $RGnameVar.value
  70. Write-Output $NSGname
  71. Write-Output $NSGRGname
  72. #For all VMs in the group - loop and get the VMs
  73. $VMs = $RecoveryPlanContext.VmMap;
  74. $vmMap = $RecoveryPlanContext.VmMap
  75. foreach($VMID in $VMinfo)
  76. {
  77. $VM = $vmMap.$VMID
  78. if( !(($VM -eq $Null) -Or ($VM.ResourceGroupName -eq $Null) -Or ($VM.RoleName -eq $Null))) {
  79. #this is when some data is anot available and it will fail
  80. Write-output "Resource group name ", $VM.ResourceGroupName
  81. Write-output "Rolename " = $VM.RoleName
  82. InlineScript {
  83. $azurevm = Get-AzureRMVM -ResourceGroupName $Using:VM.ResourceGroupName -Name $Using:VM.RoleName
  84. write-output "Azure VM Id", $azurevm.Id
  85. $NicArmObject = Get-AzureRmResource -ResourceId $azurevm.NetworkProfile.NetworkInterfaces[0].Id
  86. write-output "Nic Arm Object Id = ", $NicArmObject.Id
  87. $VMNetworkInterfaceObject = Get-AzureRmNetworkInterface -Name $NicArmObject.Name -ResourceGroupName $NicArmObject.ResourceGroupName
  88. write-output "Nic Interface Id", $VMNetworkInterfaceObject.Id
  89. $PIP = New-AzureRmPublicIpAddress -Name $azurevm.Name -ResourceGroupName $Using:VM.ResourceGroupName -Location $azurevm.Location -AllocationMethod Dynamic -Confirm:$false
  90. If($PIP -ne $Null) {
  91. Write-output "Public IP Id = ", $PIP.Id
  92. $VMNetworkInterfaceObject.IpConfigurations[0].PublicIpAddress = $PIP
  93. }
  94. if (($Using:NSGname -ne $Null) -And ($Using:NSGRGname -ne $Null)) {
  95. $NSG = Get-AzureRmNetworkSecurityGroup -Name $Using:NSGname -ResourceGroupName $Using:NSGRGname
  96. Write-output $NSG.Id
  97. $VMNetworkInterfaceObject.NetworkSecurityGroup = $NSG
  98. }
  99. #Update the properties now
  100. Set-AzureRmNetworkInterface -NetworkInterface $VMNetworkInterfaceObject
  101. }
  102. }
  103. }
  104. }