I have a couple of VM’s in Azure that I need to temporary add secondary IP in the Windows Server (guest os).
I don’t know the Interface Aliases of NIC’s present in that systems (systems will be newly generated by ASR), I know the primary IP’s assigned.
I need to add secondary IP to only one existing NIC in the OS, I plan to use command:
New-NetIPAddress -IPAddress xxx.xxx.xxx.xxx -InterfaceAlias "NAME" -SkipAsSource $true
How can I grab name of Interface Alias from this NIC and put on command above, or maybe there is some other solution to do that?
Thanks from advance for every help.
Current code:
$PrimaryIP = "10.20.30.5"
$NetworkInterface = Get-NetIPConfiguration | Where-Object { $_.IPv4Address.IPAddress -eq $PrimaryIP }
$InterfaceAlias = $NetworkInterface.InterfaceAlias
$SecondaryIP = "10.20.30.6"
New-NetIPAddress -IPAddress $SecondaryIP -InterfaceAlias $InterfaceAlias -PrefixLength 24 -SkipAsSource $false
Start-Sleep -Seconds 60
New-NetRoute -DestinationPrefix 0.0.0.0/0 -InterfaceAlias $InterfaceAlias -NextHop 10.20.30.1
2
Answers
I don't know why but almost always when I try to assign Primary IP via GUI, default gateway is lost, so I lost connection to VM.
Finally I decided to go with this script. Maybe not look perfect but do the job:
#Assign 2 IP's on NIC with Primary IP, set Primary as SaS False and Secondary SaS True
To retrieve the interface alias (interface name) based on the
Primary IP address
of theNIC
.Here is the PowerShell command.
Output:
To add the secondary
IP
toNIC
.Here is the PowerShell command.
Output:
Once the above command is run, a
secondary IP
has been assigned to the existingNIC
that had theprimary IP
address assigned to it.Updated code:
If you’re encountering the issue where the primary IP is being swapped with the secondary IP after running the script, the following script will address the problem. Make sure to execute the script in the correct order.