How to assign Static Private IP address to Network interface of replicated item under Azure Recovery service vault using PowerShell

Vinmay Parkhi 0 Reputation points
2024-10-29T08:52:04.4633333+00:00
  • Using below script to assign Virtual network and static IP to replicated item under Recovery service vault. Script successfully assigns virtual network and static IP to the replicated item as per Job status. But static IP is not reflecting on Azure Portal.
#variables
 
# Step 1: Select the subscription interactively
$subscription = Get-AzSubscription | Out-GridView -PassThru -Title "Free Trial"
Select-AzSubscription -Subscription $subscription
 
# Step 2: Select the recovery services vault interactively
$vault = Get-AzRecoveryServicesVault | Out-GridView -PassThru -Title $vaultName
Set-AzRecoveryServicesAsrVaultContext -Vault $vault
 
# Step 3: Get the fabric and protection container
$fabric = Get-AzRecoveryServicesAsrFabric
$container = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric[0]
 
# Step 4: List all replicated items to confirm the correct replicated item name
$replicatedItems = Get-AzRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $container | Where-Object { $_.FriendlyName -eq $replicatedItemName }
$replicatedItems
# Step 5: Check if the replicated item exists
if (-not $replicatedItems) {
    Write-Host "Replicated item '$replicatedItemName' not found."
    return
}
$AzureNetworkID = (Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $targetNetworkName).Id
# Step 6: Update Target Virtual Network and Test Failover Network to Replicated Network
$nicUpdate = Set-AzRecoveryServicesAsrReplicationProtectedItem -InputObject $replicatedItems `
    -UpdateNic "00:0x:29:53:90:u3" `
    -RecoveryNetworkId $AzureNetworkID `
    -RecoveryNicSubnetName $subnetName `
    -TestNetworkId $AzureNetworkID `
    -TestNicSubnetName $subnetName `
    -NicSelectionType SelectedByUser
$updateNicStatus = Get-AzRecoveryServicesAsrJob | Where-Object { $_.ID -eq $nicUpdate.ID }
$updateNicStatus
# Step 7: Check the nicUpdate state and provide the corresponding message
if ($updateNicStatus.State -eq "InProgress" -and $updateNicStatus.StateDescription -eq "InProgress") {
    Write-Host "Network updating is in process for replicated item."
Start-Sleep -Seconds 15
# Step 8: Check the status again after 15 seconds
    $updateNicStatus = Get-AzRecoveryServicesAsrJob | Where-Object { $_.ID -eq $nicUpdate.ID }
    $updateNicStatus
    
    # Check after this sleep period
    if ($updateNicStatus.State -eq "Succeeded" -and $updateNicStatus.StateDescription -eq "Completed") {
        Write-Host "Network updated successfully for replicated item."
    } elseif ($updateNicStatus.State -eq "Failed") {
        Write-Host "Failed to update network for replicated item."
    } else {
        Write-Host "Network update is still in process."
    }
} else {
    Write-Host "Check for Errors."
}
# Step 9: Update Static Ip for Replicated Network
$ipUpdate = Set-AzRecoveryServicesAsrReplicationProtectedItem -InputObject $replicatedItems `
    -RecoveryNicStaticIPAddress $targetStaticIP `
    -TestNicStaticIPAddress $targetStaticIP `
    -NicSelectionType SelectedByUser
$updateIpStatus = Get-AzRecoveryServicesAsrJob | Where-Object { $_.ID -eq $ipUpdate.ID }
$updateIpStatus
# Step 10: Check the ipUpdate state and provide the corresponding message
if ($updateIpStatus.State -eq "InProgress" -and $updateIpStatus.StateDescription -eq "InProgress") {
    Write-Host "Static Ip updating is in process for replicated item."
Start-Sleep -Seconds 15
# Step 11: Check the status again after 15 seconds
    $updateIpStatus = Get-AzRecoveryServicesAsrJob | Where-Object { $_.ID -eq $ipUpdate.ID }
    $updateIpStatus
    
    # Check after this sleep period
    if ($updateIpStatus.State -eq "Succeeded" -and $updateIpStatus.StateDescription -eq "Completed") {
        Write-Host "Static Ip updated successfully for replicated item."
    } elseif ($updateIpStatus.State -eq "Failed") {
        Write-Host "Failed to update Static Ip for replicated item."
    } else {
        Write-Host "Static Ip update is still in process."
    }
} else {
    Write-Host "Check for Errors."
}
Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,531 questions
Azure Site Recovery
Azure Site Recovery
An Azure native disaster recovery service. Previously known as Microsoft Azure Hyper-V Recovery Manager.
726 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,636 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.