IP assignment

immm 20 Reputation points
2025-02-24T13:20:27.1166667+00:00

Hello,

We have one virtual machine in azure, which has an interface with a lot of IP Addresses (25 IPs+ as secondary, see please screenshot). Also I have one identical virtual machine (state is down). And in case of failure of first machine I need to turn on second machine and assign all IP-s. How can I do it with scripting(because if i start move IP-s manually it takes a lot of time)? How this script should looks? (P.s There is no another way to failover these machines)int

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,830 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pradeep Kommaraju 380 Reputation points Microsoft Employee
    2025-02-25T19:10:11.3666667+00:00

    Hello ,

    Thanks for reaching out to Microsoft Q and A forum ,

    Your Current Cloud Solution 

    Single Active VM + Stopped VM (Manual Failover) Downtime is acceptable --> This is your 100% manual current use case 

    Recommended Solution :

    Standard load balancer, Attach the public Ip's to the Lb + VMSS (scale set) with 1 Always-Running VM --> If you want some automation with similar downtime might be just a few $ more monthly (6$-8$)

    But if you want to stick on to current solution only , Here is a PowerShell script which can help you .

    Variables

    $primaryVMName = "PrimaryVM"

    $standbyVMName = "StandbyVM"

    $resourceGroupName = "YourResourceGroup"

    $location = "YourAzureRegion"

    Authenticate to Azure

    Connect-AzAccount

    Stop the primary VM

    Stop-AzVM -Name $primaryVMName -ResourceGroupName $resourceGroupName -Force

    Get the NIC of the primary VM

    $primaryVM = Get-AzVM -Name $primaryVMName -ResourceGroupName $resourceGroupName

    $primaryNIC = Get-AzNetworkInterface -Name $primaryVM.NetworkProfile.NetworkInterfaces[0].Id.Split('/')[-1] -ResourceGroupName $resourceGroupName

    Get the NIC of the standby VM

    $standbyVM = Get-AzVM -Name $standbyVMName -ResourceGroupName $resourceGroupName

    $standbyNIC = Get-AzNetworkInterface -Name $standbyVM.NetworkProfile.NetworkInterfaces[0].Id.Split('/')[-1] -ResourceGroupName $resourceGroupName

    Loop through each IP configuration in the primary NIC (excluding the primary IP)

    foreach ($ipConfig in $primaryNIC.IpConfigurations) {

        if ($ipConfig.Primary -eq $false) {

            # Remove IP configuration from primary NIC

            $primaryNIC.IpConfigurations.Remove($ipConfig)

            # Add IP configuration to standby NIC

            $standbyNIC.IpConfigurations.Add($ipConfig)

        }

    }

    Update both NICs

    Set-AzNetworkInterface -NetworkInterface $primaryNIC

    Set-AzNetworkInterface -NetworkInterface $standbyNIC

    Start the standby VM

    Start-AzVM -Name $standbyVMName -ResourceGroupName $resourceGroupName

    Please don't forget to accept the answer if this was helpful .

    Thanks,
    Pradeep


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.