skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    1. Assign Secondary IP if I know only Primary IP
    2. Assign SkipAsSource flag to false on primary and true to secondary
    3. Assign the default gateway for NIC

    #Assign 2 IP's on NIC with Primary IP, set Primary as SaS False and Secondary SaS True

    $PrimaryIP = "10.20.30.10"
    $NetworkInterface = Get-NetIPConfiguration | Where-Object { $_.IPv4Address.IPAddress -eq $PrimaryIP }
    
    $InterfaceAlias = $NetworkInterface.InterfaceAlias
    
    $SecondaryIP = "10.20.30.11"  
    New-NetIPAddress -IPAddress $SecondaryIP -InterfaceAlias $InterfaceAlias -PrefixLength 24 -SkipAsSource $true
    
    Start-Sleep -Seconds 60
    
    New-NetRoute -DestinationPrefix 0.0.0.0/0 -InterfaceAlias $InterfaceAlias -NextHop 10.20.30.1
    
    $NetworkInterface = Get-NetIPConfiguration | Where-Object { $_.IPv4Address.IPAddress -eq $SecondaryIP }
    
    $InterfaceAlias = $NetworkInterface.InterfaceAlias
    
    New-NetIPAddress -IPAddress $PrimaryIP -InterfaceAlias $InterfaceAlias -PrefixLength 24 -SkipAsSource $false
    

  2. How can I grab name of Interface Alias from this NIC using primary IP’s?

    To retrieve the interface alias (interface name) based on the Primary IP address of the NIC.

    Here is the PowerShell command.

    #Replace with your primary IP address
    $PrimaryIP = "192.168.1.5"
    $NetworkInterface = Get-NetIPConfiguration | Where-Object { $_.IPv4Address.IPAddress -eq $PrimaryIP }
    

    Output:

    enter image description here

    To add the secondary IP to NIC.

    Here is the PowerShell command.

    # Replace with secondary IP address
    $SecondaryIP = "192.168.1.15"  
    New-NetIPAddress -IPAddress $SecondaryIP -InterfaceAlias $InterfaceAlias -SkipAsSource $true
    

    Output:

    Once the above command is run, a secondary IP has been assigned to the existing NIC that had the primary IP address assigned to it.

    enter image description here

    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.

    Note:If the script is run to add a secondary IP and the IP is assigned dynamically, it will modify the existing IP, replacing it with a new IP instead of the secondary IP. However, if the IP is assigned statically, it will successfully add the secondary IP.

    $PrimaryIP = "192.168.1.5"
    $SecondaryIP = "192.168.1.21"
    
    # Get interface alias for the primary IP
    $NetworkInterface = Get-NetIPConfiguration | Where-Object { $_.IPv4Address.IPAddress -eq $PrimaryIP }
    $InterfaceAlias = $NetworkInterface.InterfaceAlias
    
    # Add secondary IP
    New-NetIPAddress -IPAddress $SecondaryIP -InterfaceAlias $InterfaceAlias -SkipAsSource $true -PrefixLength 24
    
    $AdapterIndex = (Get-NetAdapter | Where-Object { $_.InterfaceAlias -eq $InterfaceAlias }).ifIndex
    
    # Set the default gateway for secondary IP
    $DefaultGateway = "10.20.30.1"
    Set-NetIPInterface -InterfaceIndex $AdapterIndex -InterfaceMetric 2  
    New-NetRoute -InterfaceIndex $AdapterIndex -DestinationPrefix 0.0.0.0/0 -NextHop $DefaultGateway
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search