PowerShell – Azure – Create UNIX VM

| | | | | |

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.