How we can allocate static private IP to one particular virtual machine

Varma 1,390 Reputation points
2024-02-15T15:53:05.75+00:00

How we can allocate static private IP to one particular virtual machine? is there any PowerSHell command to do that for multiple machines

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,667 questions
0 comments No comments
{count} votes

Accepted answer
  1. KapilAnanth-MSFT 49,016 Reputation points Microsoft Employee
    2024-02-16T13:38:19.3066667+00:00

    @Varma

    Welcome to the Microsoft Q&A Platform. Thank you for reaching out & I hope you are doing well. I understand that you would like to change the IP Address of VMs to a Static One via Powershell (in same ResourceGroup)

    Below PowerShell script can do the trick, as long as,

    • The VMs are not part of Load Balancer Rules or Load Balancer Backend Pool
    • The VMs do not have a Public IP Address associated to them
    • You are updating the first IPconfig of a single NIC VM.
    $vms = Get-AzVM -ResourceGroupName "<RGNAME>"
    
    
    foreach ($vm in $vms)
    {
    
    $nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces.Id
    
    #This enables you to set the IP you want as user input for the VM
    Write-Host "Current IP of the VM " $vm.Name " is " $nic.IpConfigurations.PrivateIpAddress 
    $privateip = Read-Host "Enter the IP you want to set it to"
    
    #Set the NIC to update changes
    $nic | Set-AzNetworkInterfaceIpConfig -Name $nic.IpConfigurations.Name -PrivateIpAddress $privateip -SubnetId $nic.IpConfigurations.Subnet.Id 
    $nic | Set-AzNetworkInterface
    
    
    }
    
    
    

    In case there are Public IP Addresses, the "Set the NIC to update changes" section becomes :

    #Set the NIC to update changes
    $nic | Set-AzNetworkInterfaceIpConfig -Name $nic.IpConfigurations.Name -PrivateIpAddress $privateip -SubnetId $nic.IpConfigurations.Subnet.Id -PublicIpAddressId $nic.IpConfigurations.PublicIpAddress.Id
    $nic | Set-AzNetworkInterface
    

    Similarly, you can add the additional parameters as required : Refer Set-AzNetworkInterfaceIpConfig

    User's image

    Please modify the script according to your environment. Hope this helps.

    Thanks,

    Kapil


    Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. TP 110.8K Reputation points
    2024-02-15T18:56:50.37+00:00

    Hi,

    Yes, below article has sample powershell code showing how to change dynamic private ip to static private ip:

    Change private IP address to static https://learn.microsoft.com/en-us/azure/virtual-network/ip-services/virtual-networks-static-private-ip-arm-ps#change-private-ip-address-to-static

    To change multiple you would first get the VMs you want to change and then loop through them, executing the sample code from the above article. If you are unsure how to do looping for this please let me know and I'll make small sample snippet.

    You may also use the portal to set a static private IP. Navigate to the VM, click on Network settings blade, next on right half click on the Network interface / IP configuration, next click on the IP configuration (ipconfig1 in below sample) you want to change. A flyout screen will open on right where you can change it from Dynamic to Static and enter the address you want.

    qna static private ip

    Please click Accept Answer and upvote if the above was useful.

    Thanks.

    -TP


  2. Andreas Baumgarten 118.9K Reputation points MVP
    2024-02-15T18:57:58.7066667+00:00

    Hi @Varma ,

    maybe this example is helpful to set a private IP address to static on an Azure VM via PowerShell:

    Change private IP address to static


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten

    0 comments No comments

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.