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

PowerShell – Azure – Create UNIX VM

Posted on April 8, 2016 By David Kittell

If you have a need to create a few VMs it can get a bit tedious to build them in one of the various portals.

Set the variables then let this script run. At some point I will create a function around this but for now the script works as is.

clear

# Log into Azure Account
Login-AzureRmAccount

# List all Azure Subscriptions
Get-AzureSubscription | Format-Table subscriptionname, subscriptionid

# Variables - Start
$subscr="<Scription name from Get-AzureSubscription>"
$staccount="<Storage Account Name>"
$vmname="<VM Name/Hostname>"
$vmsize="<Azure Role Size>" # Get-AzureRoleSize | Format-Table -AutoSize InstanceSize, RoleSizeLabel, Cores, MemoryInMb
$vmip = "<IP for the VM>"
$svcname="<Service Name for the VM>"
$linuxUser = "<User to be created>"
$certPath = "<SSH Cert Path ie. C:\cygwin\home\dkittell\VM.pem"> 
# To generate the key:  openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout VM.key -out VM.pem
$location = "<Azure Region>" # Get-AzureLocation | select Name
$availset="<Available Set>"
$family="<OS Family Name>" # Get-AzureVMImage | sort PublishedDate -Descending | select -ExpandProperty ImageFamily -Unique
$vnetname="<Network Name>"
$subnetname = "<Sub-Network Name>"
# Variables - Stop

Select-AzureSubscription -SubscriptionName $subscr –Current
Set-AzureSubscription -SubscriptionName $subscr -CurrentStorageAccountName $staccount

# Service Creation
if (!(Test-AzureName -Service $svcname))
{  
    Write-Host "Creating Service $svcname in $location location "
    New-AzureService -ServiceName $svcname -Location $location
}
else
{
    Write-Host "Service $svcname already exists"
}

Write-Host "Creating Server certificate"
#Get-AzureCertificate -ServiceName "devVMStorage" | Remove-AzureCertificate 
$cert = Get-PfxCertificate -FilePath $certPath
Add-AzureCertificate -CertToDeploy $certPath -ServiceName $svcname
$sshKey = New-AzureSSHKey -PublicKey -Fingerprint $cert.Thumbprint -Path "/home/$linuxUser/.ssh/authorized_keys"

# Image Configuration
Write-Host "Selecting $family image"
$image=Get-AzureVMImage | where { $_.ImageFamily -eq $family } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1

# Network Configuration
Write-Host "Configuring $vmname networking"
$vm1 = New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image -AvailabilitySetName $availset -MediaLocation "https://$staccount.blob.core.windows.net/vhds/$vmname.vhd"
$vm1 | Add-AzureProvisioningConfig -Linux -NoSSHPassword -LinuxUser $linuxUser -SSHPublicKeys $sshKey
$vm1 | Set-AzureSubnet -SubnetNames $subnetname
$vm1 | Set-AzureStaticVNetIP -IPAddress $vmip

Write-Host "Setting $staccount storage account"
Set-AzureStorageAccount -StorageAccountName $staccount

Write-Host "Creating $vmname VM within $svcname service "
New-AzureVM –ServiceName $svcname -VMs $vm1 -VNetName $vnetname
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 CentOS Code PowerShell Red Hat Ubuntu UNIX azureCreate Azure VMSSHPublicKeysUNIX VM

Post navigation

Previous post
Next post

Related Posts

WordPress – List Categories

Posted on February 11, 2016February 12, 2016

SELECT wpt.term_id, wpt.NAME, (SELECT name from wp_terms WHERE term_id = wptt.parent) AS parent FROM wp_terms wpt LEFT JOIN wp_term_taxonomy wptt ON wpt.term_id = wptt.term_id; 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…

Read More

Visual Studio Oracle Tools

Posted on February 28, 2013October 26, 2015

When working with Visual Studio and Oracle you will need to download Oracle’s Visual Studio Tools. Code Samples: http://www.oracle.com/technetwork/articles/dotnet/cook-dotnet-101788.html If I thought I could legally host the file I would. Official Page: http://www.oracle.com/technetwork/topics/dotnet/utilsoft-086879.html If the link fails to work do a search on http://www.oracle.com/ for Visual Studio ODAC 11.2 Release…

Read More

TaskKill App List

Posted on October 15, 2013October 26, 2015

taskkill /f /im pdfpro7hook.exe taskkill /f /im searchprotocolhost.exe taskkill /f /im sgnmaster.exe taskkill /f /im apmsgfwd.exe taskkill /f /im apntex.exe taskkill /f /im apoint.exe taskkill /f /im jusched.exe taskkill /f /im openvpn-gui.exe taskkill /f /im shstat.exe taskkill /f /im udaterui.exe taskkill /f /im hidfind.exe taskkill /f /im hkcmd.exe taskkill /f…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

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
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes