ASRPublicIp.ps1


<#PSScriptInfo
 
.VERSION 1.2
 
.GUID 8ede60db-2dee-4ffe-9a6a-ae931cf5b263
 
.AUTHOR Microsoft Corporation
 
.COMPANYNAME Microsoft Corporation
 
#>


<#
 
.DESCRIPTION
 ASR Automation script for adding Public IP address
 
#>
 

<#
.SYNOPSIS
    Runbook to add public IP addresses all VMs within a Recovery Plan
  
.DESCRIPTION
    This runbook will add a public IP address to all the virtual machines in a Recovery Plan and execute a script using Custom Script Extension.
    For RPD access, you can connect to the VMs from the Azure Portal or using PowerShell to retrieve the RDP file.
 
.PLATFORM
    Windows Server, Linux
       
.DEPENDENCIES
    Azure VM Agent is required for the custom script extension to be installed in the guest(s). Ensure this is present prior to any test- or planned failover.
     
.ASSETS
    This runbook requires the following modules present in the Azure Automation assets:
    - AzureRm.Profile
    - AzureRm.Compute
    - AzureRm.Network
  
.PARAMETER RecoveryPlanContext
    RecoveryPlanContext is the only parameter you need to define.
    This parameter gets the failover context from the recovery plan.
  
.NOTES
    Author: Kristian Nese - krnese@microsoft.com
    Last Updated: 17/10/2016
#>
 

param ( 
        [Object]$RecoveryPlanContext 
      ) 

write-output $RecoveryPlanContext

if($RecoveryPlanContext.FailoverDirection -ne 'PrimaryToSecondary')
{
    Write-Output 'Script is ignored since Azure is not the target'
}
else
{

    $VMinfo = $RecoveryPlanContext.VmMap | Get-Member | Where-Object MemberType -EQ NoteProperty | select -ExpandProperty Name

    Write-Output ("Found the following VMGuid(s): `n" + $VMInfo)

    if ($VMInfo -is [system.array])
    {
        $VMinfo = $VMinfo[0]

        Write-Output "Found multiple VMs in the Recovery Plan"
    }
    else
    {
        Write-Output "Found only a single VM in the Recovery Plan"
    }

    $RGName = $RecoveryPlanContext.VmMap.$VMInfo.CloudServiceName

    Write-OutPut ("Name of resource group: " + $RGName)

    "Logging in to Azure..."
    $Conn = Get-AutomationConnection -Name AzureRunAsConnection 
     Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

    "Selecting Azure subscription..."
    Select-AzureRmSubscription -SubscriptionId $Conn.SubscriptionID -TenantId $Conn.tenantid 

    # Get VMs within the Resource Group

    $VMs = Get-AzureRmVm -ResourceGroupName $RGName

    Write-Output ("Found the following VMs: `n " + $VMs.Name) 

    foreach ($VM in $VMs)
    {
        $VMNetworkInterfaceName = $VM.NetworkInterfaceIDs[0].Split('/')[-1]

        $VMNetworkInterfaceObject = Get-AzureRmNetworkInterface -ResourceGroupName $RGName -Name $VMNetworkInterfaceName

        $PIP = New-AzureRmPublicIpAddress -Name $VM.Name -ResourceGroupName $RGName -Location $VM.Location -AllocationMethod Dynamic

        $VMNetworkInterfaceObject.IpConfigurations[0].PublicIpAddress = $PIP

        Set-AzureRmNetworkInterface -NetworkInterface $VMNetworkInterfaceObject
        
        Write-Output ("Added public IP address to the following VM: " + $VM.Name)  
    }

    Write-Output ("Operation completed on the following VM(s): `n" + $VMs.Name)
}