Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

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

Posted on December 19, 2017 By David Kittell

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.

Related

Azure Azure CLI Azure CLI Functions Code UNIX UNIX Shell Scripts Windows

Post navigation

Previous post
Next post

Related Posts

Get User Details Function

Posted on August 6, 2013October 26, 2015

— Get User’s First Name, Last Name SELECT dbo.GetUserDetails(120, 1) — Get User’s Last Name, First Name SELECT dbo.GetUserDetails(120, 4) — Get User’s Username SELECT dbo.GetUserDetails(120, 2) — Get User’s Email Address SELECT dbo.GetUserDetails(120, 3) IF OBJECT_ID(N’dbo.GetUserDetails’) IS NOT NULL DROP FUNCTION dbo.GetUserDetails GO CREATE FUNCTION dbo.GetUserDetails ( @userid INT…

Read More

Regex Match

Posted on August 6, 2013October 26, 2015

IF OBJECT_ID(N’dbo.RegexMatch’) IS NOT NULL DROP FUNCTION dbo.RegexMatch GO CREATE FUNCTION dbo.RegexMatch ( @pattern VARCHAR(2000) ,@matchstring VARCHAR(MAX) –Varchar(8000) got SQL Server 2000 ) RETURNS INT /* The RegexMatch returns True or False, indicating if the regular expression matches (part of) the string. (It returns null if there is an error)….

Read More

PowerShell – Check DNS For Skype For Business

Posted on May 21, 2018

## CallTower PowerShell DNS Checker, This script checks all that you have all your DNS entries in place and displays the final route for DNS lookups. ## ## Usage .\Check-SRVRecords.ps1 -domain calltower.com ## ## Original Script Obtained From: https://www.uc.solutions/Skype_for_Business/Skype4B_Set_Up/How_to_verify_or_set_DNS_records_for_Skype_for_Business ## param($domain = (Get-WmiObject win32_computersystem).Domain) $cnameresult = Resolve-DnsName sip.$domain -Server 8.8.8.8…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes