Azure CLI – Convert VM Dynamic IP to Static IP

| | |

If you create your VM with Dynamic IPs DHCP will grab the first available IP but some VMs are best on a static IP. This script below will get the existing IP of the ipconfig and reassign it to the same ipconfig as a static IP.

Dynamic_To_Static ()
{
nic="$4"
ipConfig="$5"
vmName="$3"
region="$2"
regionNS=$(tr -d ' ' <<< "$region")
resourceGroup=$(echo $1)

ConfigIP=$(az network nic ip-config show -g "$resourceGroup" --nic-name $(echo $nic) -n $(echo $ipConfig) --query "{ipConfigurations:privateIpAddress}" -o tsv)
az network nic ip-config update -g "$resourceGroup" --nic-name $(echo $nic) -n $(echo $ipConfig) --private-ip-address $ConfigIP
}
Dynamic_To_Static "RG-$(echo $regionNS)" $region $vmName $(echo $vmName)$(echo $regionNS)Nic "ipconfig1"
datetime=`date +%Y_%m_%d_%H.%M.%S`
echo "$(echo $vmName) Private IPs" > "$(echo $vmName)_$(echo $datetime).txt"
az network nic ip-config list --nic-name  $(echo $vmName)$(echo $regionNS)Nic  -g $resourceGroup -o table >> "$(echo $vmName)_$(echo $datetime).txt"
cat "$(echo $vmName)_$(echo $datetime).txt"
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.