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
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.