Skip to content

Commit fe6b2ee

Browse files
committed
script to assign ip
1 parent 8b788d6 commit fe6b2ee

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

scripts/AssignIP-Workflow.ps1

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
workflow AssignIP-Workflow {
2+
param (
3+
[parameter(Mandatory = $false)]
4+
[Object]$RecoveryPlanContext
5+
)
6+
7+
# Client id of user assigned managed identity, make sure the identity has enough permission to update resoruces.
8+
$identityClientId = "18ada7c8-9bf0-4ff4-bbf0-f1ad8c20d436"
9+
# Network inteface name of adminVM
10+
$nicName = "adminvmzone1951_z1"
11+
# IP config name for secondary IP
12+
$ipConfig2Name = "ipconfig2"
13+
# Virtual network name
14+
$vnetName = "wlsd_VNET"
15+
# Reource group name of who has virtual network deployed
16+
$vnetResourceGroup = "haiche-dynamic-cluster-forms-reports"
17+
# Subnet name which has IP address for adminVM
18+
$subNetName = "Subnet"
19+
# Secondary IP address
20+
$secondaryPrivateIPAddress = "10.0.0.16"
21+
# Source reource group name
22+
$sourceResourceGroup="haiche-dynamic-cluster-forms-reports"
23+
# Target resource group name
24+
$targetResourceGroup="haiche-dynamic-cluster-forms-reports-asr"
25+
26+
$direction = $RecoveryPlanContext.FailoverDirection
27+
if ( $direction -ne "PrimaryToSecondary") {
28+
$temp=$sourceResourceGroup
29+
$sourceResourceGroup=$targetResourceGroup
30+
$targetResourceGroup=$temp
31+
}
32+
33+
InlineScript {
34+
# Ensures you do not inherit an AzContext in your runbook
35+
Disable-AzContextAutosave -Scope Process
36+
37+
$AzureContext = (Connect-AzAccount -Identity -AccountId ${identityClientId}).context
38+
39+
# set and store context
40+
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
41+
42+
#$nic = Get-AzNetworkInterface -Name $Using:nicName -ResourceGroupName $Using:resourceGroup
43+
$nic = Get-AzNetworkInterface | Where-Object { ($_.ResourceGroupName -eq $Using:sourceResourceGroup) -and ($_.Name -eq $Using:nicName) }
44+
Remove-AzNetworkInterfaceIpConfig -Name $Using:ipConfig2Name -NetworkInterface $nic
45+
Set-AzNetworkInterface -NetworkInterface $nic
46+
Write-Output("Complete removing IP from source network interface")
47+
48+
$nic2 = Get-AzNetworkInterface | Where-Object { ($_.ResourceGroupName -eq $Using:targetResourceGroup) -and ($_.Name -eq $Using:nicName) }
49+
Write-Output("Complete querying target network interface")
50+
$vnet = Get-AzVirtualNetwork | Where-Object { ($_.ResourceGroupName -eq $Using:vnetResourceGroup) -and ($_.Name -eq $Using:vnetName) }
51+
Write-Output("Complete querying vnet")
52+
$subNet = Get-AzVirtualNetworkSubnetConfig -Name $Using:subNetName -VirtualNetwork $vnet
53+
Write-Output("Complete querying sub net")
54+
$ipconf2 = New-AzNetworkInterfaceIpConfig -Name $Using:ipConfig2Name -Subnet $subNet -PrivateIpAddress $Using:secondaryPrivateIPAddress
55+
Write-Output("Complete creating new nic ip config")
56+
# $nic2.IpConfigurations | Format-Table Name, PrivateIPAddress
57+
# Write-Output("Update ip")
58+
$nic2.IpConfigurations.Add($ipconf2)
59+
Write-Output("Complete assigning secondary ip to target nic")
60+
$nic2.IpConfigurations | Format-Table Name, PrivateIPAddress
61+
Set-AzNetworkInterface -NetworkInterface $nic2
62+
Write-Output("Done")
63+
}
64+
}

0 commit comments

Comments
 (0)