Azure – CLI – Build Windows/UNIX VM Function (With or without public IP)

In the functions below the VM name is used in all of the pieces to build the VM to simplify documentation.

Important Items:

  1. As I use a Firewall appliance in Azure I do not set Network Security Groups (NSG).
  2. I have a predefined virtual network (VN) and predefined network subnet configuration (NSC)
  3. UnixAdmin user is created with a SSH key instead of password so saving the key will be needed
  4. WindowsAdmin user is created with a password that you will want to change before you run the function or after your first login
  5. Windows VM has an option for additional data disks, this could be added to the UNIX VM function as well, the size in GB is not configurable currently via the function so you may want to change the values or add it to the function
  6. Some variables could be created to replace items I’ve predefined
Build_UNIX_VM ()
{
  # echo "Resource Group set at $1"
  # echo "Region set at $2"
  # echo "Image set at $3"
  # echo "Size set at $4"
  # echo "VMName set at $5"
  # echo "Public IP set at $6"

  vmName="$5"
  region="$2"
  regionNS=$(tr -d ' ' <<< "$region")
  resourceGroup=$(echo $1)-$(echo $regionNS)
  # echo $region
  # echo $regionNS

  if [ "$6" -eq 1 ] ; then
    echo "Configuring $vmName in $region with a public IP"
    # Create a public IP address.
    az network public-ip create --resource-group "$resourceGroup" --name $(echo $vmName)$(echo $regionNS)PublicIP --location "$region"
    # Create a virtual network card and associate with public IP address
    az network nic create --resource-group "$resourceGroup" --name $(echo $vmName)$(echo $regionNS)Nic --location "$region" --vnet-name VN-$(echo $regionNS) --subnet NSC-$(echo $regionNS)-Reserved-VM --public-ip-address $(echo $vmName)$(echo $regionNS)PublicIP
  else
    echo "Configuring $vmName in $region"
    # Create a virtual network card
    az network nic create --resource-group "$resourceGroup" --name $(echo $vmName)$(echo $regionNS)Nic --location "$region" --vnet-name VN-$(echo $regionNS) --subnet NSC-$(echo $regionNS)-Reserved-VM
  fi

  # Create virtual machine with defined nic
  az vm create --resource-group "$resourceGroup" --name $(echo $vmName) --location "$region" --nics $(echo $vmName)$(echo $regionNS)Nic --image $3 --admin-username UnixAdmin --generate-ssh-keys --size $4 --storage-sku Standard_LRS

  az vm stop --resource-group "$resourceGroup" --name $(echo $vmName)
  az vm deallocate --resource-group "$resourceGroup" --name $(echo $vmName)
}
Build_Windows_VM ()
{
  echo "Resource Group set at $1"
  echo "Region set at $2"
  echo "Image set at $3"
  echo "Size set at $4"
  echo "VMName set at $5"
  echo "Public IP set at $6"
  echo "Data Disk GB size set at $7"
  echo "Additional Data Disks set at $8"

  vmName="$5"
  region="$2"
  regionNS=$(tr -d ' ' <<< "$region")
  resourceGroup=$(echo $1)-$(echo $regionNS)
  #echo $region
  #echo $regionNS

  if [ "$6" -eq 1 ] ; then
    echo "Configuring $vmName in $region with a public IP"
    # Create a public IP address.
    az network public-ip create --resource-group "$resourceGroup" --name $(echo $vmName)$(echo $regionNS)PublicIP --location "$region"
    # Create a virtual network card and associate with public IP
    az network nic create --resource-group "$resourceGroup" --name $(echo $vmName)$(echo $regionNS)Nic --location "$region" --vnet-name VN-$(echo $regionNS) --subnet NSC-$(echo $regionNS)-Reserved-VM --public-ip-address $(echo $vmName)$(echo $regionNS)PublicIP

  else
    echo "Configuring $vmName in $region"
    # Create a virtual network card
    az network nic create --resource-group "$resourceGroup" --name $(echo $vmName)$(echo $regionNS)Nic --location "$region" --vnet-name VN-$(echo $regionNS) --subnet NSC-$(echo $regionNS)-Reserved-VM
  fi

  # Create virtual machine with defined nic
  az vm create --resource-group "$resourceGroup" --name $(echo $vmName) --location "$region" --nics $(echo $vmName)$(echo $regionNS)Nic --image $3 --admin-username WindowsAdmin --admin-password SuperSecretPassword --size $4 --storage-sku Standard_LRS
  az vm disk attach --vm-name $(echo $vmName) --resource-group "$resourceGroup" --disk $(echo $vmName)DataDisk01 --size $4 --size-gb $7 --sku Standard_LRS --new

  if [ "$8" -eq 1 ] ; then
    az vm disk attach --vm-name $(echo $vmName) --resource-group "$resourceGroup" --disk $(echo $vmName)DataDisk02 --size $4 --size-gb 50 --sku Premium_LRS --new
    az vm disk attach --vm-name $(echo $vmName) --resource-group "$resourceGroup" --disk $(echo $vmName)DataDisk03 --size $4 --size-gb 50 --sku Premium_LRS --new
    az vm disk attach --vm-name $(echo $vmName) --resource-group "$resourceGroup" --disk $(echo $vmName)DataDisk04 --size $4 --size-gb 50 --sku Premium_LRS --new
  fi

  az vm stop --resource-group "$resourceGroup" --name $(echo $vmName)
  az vm deallocate --resource-group "$resourceGroup" --name $(echo $vmName)
}
# Create CentOS UNIX VM in East US region with public IP
Build_UNIX_VM "RG-SomethingCool" "East US" "OpenLogic:CentOS:7.3:latest" "Standard_DS2_v2" "CentOSVM01" 1

# Create Red Hat UNIX VM in West US region without public IP
Build_UNIX_VM "RG-SomethingCool" "West US" "RedHat:RHEL:7.3:latest" "Standard_DS2_v2" "RHELOSVM01" 0

# Create Windows Server in East US region with public IP no additional data disks
Build_Windows_VM "RG-SomethingCool" "East US" "win2016datacenter" "Standard_DS2_v2" "WindowsOS01" 1 "64" 0

# Create Windows Server in East US region with public IP and additional data disks
Build_Windows_VM "RG-SomethingCool" "East US" "win2016datacenter" "Standard_DS2_v2" "WindowsOS01" 1 "64" 1

# Create Windows Server in West US region without public IP no additional data disks
Build_Windows_VM "RG-SomethingCool" "West US" "win2016datacenter" "Standard_DS2_v2" "WindowsOS01" 0 "64" 0
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.