Use PowerShell to change subnet and IP of ARM VMUse PowerShell to change subnet and IP of ARM VM

Change networking configuration for an ARM VM with PowerShell.

John Savill

August 12, 2016

1 Min Read
Use PowerShell to change subnet and IP of ARM VM

Q. How can I change the subnet and IP address of an Azure RM VM using PowerShell?

A. To change the subnet an Azure RM VM is in and to set a static IP use the following PowerShell. Change the variables to match your environment.

$RGname = 'RGVM01' #VM and NIC RG$VNetRG = 'RGNet01' #Virt Net RG$VMName = 'VM01' #VM Name$NICName = 'VM01100' #NIC Name$VNetName = 'VNet01' #Virt Net name$TarSubnetName = 'Subnet10' #Target subnet name$VM = Get-AzureRmVM -Name $VMName -ResourceGroupName $RGname$VNET = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $VNetRG$TarSubnet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $VNET -Name $TarSubnetName$NIC = Get-AzureRmNetworkInterface -Name $NICName -ResourceGroupName $RGname$NIC.IpConfigurations[0].Subnet.Id = $TarSubnet.IdSet-AzureRmNetworkInterface -NetworkInterface $NIC#Once the subnet has been set and that applied can apply the static IP address$NIC = Get-AzureRmNetworkInterface -Name $NICName -ResourceGroupName $RGname$NIC.IpConfigurations[0].PrivateIpAddress = ‘10.1.1.20'$NIC.IpConfigurations[0].PrivateIPAllocationMethod = 'Static'#$NIC.DnsSettings.DnsServers = '10.1.1.10'Set-AzureRmNetworkInterface -NetworkInterface $NIC

 

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like