Use PowerShell to change subnet and IP of ARM VM

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

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish