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
- 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."
}
Sign in to answer