Deploying VM in Azure with Powershell Menu Script!
I am currently sitting on an azure course and I don't like using console I have to be honest. So I created this script to be able to deploy a VM in Azure.
Basically you need to set 2 variables which are the workdir and the azuresettings
workdir is just a temp working directory and azuresettings points to your azure settings file.
Btw I assume you have already established connectivity with Certs and Downloaded your settings file :)
The script will walk you through selecting all the different options available and then provision a VM.
Have a go! :) here is the script… again I am just trying to provide a framework… there is many ways to improve this script for your own needs with lots of error checking etc… :)
Usual disclaimers applies :)
<#
.SYNOPSIS
Deploys VM's In Azure using a simple menu based system
.DESCRIPTION
This will query the options available to your subscription and allow you to select and configure as you need to deploy a VM.
.PARAMETER SettingsFile
.EXAMPLE
.\DeployAzureVM.ps1 -SettingsFile AzureSettings.PublishSettings
#>
[CmdletBinding()]
#Validating the file exists before continuing into script
param([ValidateScript({test-path $_})][string]$SettingsFile)
$Erroractionpreference = "SilentlyContinue"
$azuresettings = $settingsfile
$error.clear()
#Global Variables
#Please Set This According to your environment
$workdir = "c:\temp"
function GetAzureSubScription()
{
#Get Azure Subscription & Set Subscription and storage account
write-host "`n`nMenu: `tAzure Subscription"
$azuresubs = get-azuresubscription
$i=0
foreach ($azsub in $azuresubs)
{
write-host $i "`t" $azsub.SubscriptionName "`t" $azsub.SubscriptionId
$i++
}
$azuresubans = Read-host "Please Enter The Subscription you wish to use"
$azuresub = $azuresubs[$azuresubans]
return $azuresub
}
function GetAzureStorage()
{
#Get Azure Storage Account
write-host "`n`nMenu: `tStorage Account"
$asacts = get-azurestorageaccount
$i=0
foreach ($asact in $asacts)
{
write-host $i "`t" $asact.StorageAccountName "`t" $asact.Location
$i++
}
$asactans = Read-host "Please Enter The Storage Account you wish to use"
$azurestorageacct = $asacts[$asactans]
return $azurestorageacct
}
function GetAzureCloudService()
{
#Select Cloud Service
write-host "`n`nMenu: `tCloud Service"
$azureservice = get-azureservice
$i=0
foreach($service in $azureservice)
{
write-host $i "`t" $service.label "`t" $service.AffinityGroup
}
write-host 99 "`t If you want to create a new cloud service please select 99"
$azureserviceans = read-host "Please Select the Azure Service"
if ($azureserviceans -eq 99)
{
$azureservice = read-host "Please Enter The Name of the New Azure Cloud Service You Want To Create"
}
else
{
$azureservice = ($azureservice[$azureserviceans]).label
}
return $azureservice
}
function GetFreePort()
{
$phemoralportstartrange = 50000
$phemoralportendrange = 63000
#Get Free Port for Endpoint
$portlist = (get-azurevm -ServiceName $azureservice |get-azureendpoint ).port
do
{
$port = get-random -Maximum $phemoralportendrange -Minimum $phemoralportstartrange
$portexists = $portlist |where {$_ -eq $port}
}while ($portexists.count -ne 0)
return $port
}
function GetAzureNetConf($workdir)
{
#Gets NetworkConfiguration
write-host "`n`nMenu: `tSelect Subnet"
[xml]$xmldata = (Get-AzureVNetConfig).XMLConfiguration
$xmldata |Export-Clixml -path ($workdir + "\vnetconf.xml")
$Subnets = $xmldata.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.VirtualNetworkSite.Subnets.subnet
$SIte = ($xmldata.NetworkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.VirtualNetworkSite).name
$i=0
foreach($snet in $subnets)
{
write-host $i "`t" $Snet.Name "`t" $Snet.AddressPrefix
$i++
}
$subnetans = read-host "Please Enter The subnet number you wish to use"
$subnettouse = ($subnets[$subnetans]).name
#Gets Affinity Group
write-host "`n`nMenu: `tAffinity Group"
$ags = get-azureaffinitygroup
$i=0
foreach ($ag in $ags)
{
write-host $i "`t" $ag.name "`t" $ag.Location
$i++
}
$agsans = read-host "Please Enter the affinity group you wish to use"
$affinitygroup = $ags[$agsans]
return $affinitygroup
}
function GetImageType()
{
#Get Azure VM Image
write-host "`n`nMenu: `t OS Image"
write-host "Do you wish to deploy a Windows Image or a Linux Image"
[array]$imagetype = "Linux"
$imagetype += "Windows"
$i=0
foreach ($imaget in $imagetype)
{
write-host $i "`t" $imaget
$i++
}
$imageosans = read-host "Please Select the windows image type to continue"
$imageos = $imagetype[$imageosans]
return $imageos
}
function GetAzureImages($imageos)
{
$Categories = (Get-AzureVMImage |where {$_.OS -eq $imageos} |Group Category).Name
write-host "Please Choose the category you wish to search within"
$i=0
Foreach ($cat in $categories)
{
write-host $i "`t" $cat
$i++
}
$catans = read-host "Select the category"
$cattosearch = $Categories[$catans]
$images = Get-AzureVMIMage |where {$_.OS -eq $imageos} |Where {$_.Category -eq $cattosearch}
$i=0
foreach ($image in $images)
{
write-host $i "`t" $image.Label
$I++
}
$imageans = read-host "Please Select Your Image"
$imagetouse = $images[$imageans]
return $imagetouse
}
function GetVMSize()
{
#Set VM Size
write-host "`n`nMenu: `tVM Size"
[array]$vmsize = "ExtraSmall"
$vmsize += "Small"
$vmsize += "Medium"
$vmsize += "Large"
$vmsize += "ExtraLarge"
$vmsize += "A6"
$Vmsize += "A7"
$i=0
Foreach ($vms in $vmsize)
{
write-host $i "`t" $vms
$i++
}
$vmsizeans = read-host "Choose The VM Size"
$vmsi = $vmsize[$vmsizeans]
return $vmsi
}
function GetVMName()
{
write-host "`n`nMenu: `tDeployment Info"
write-host "You will be asked for the VM name continously until a unique one is found in windows azure"
do
{
$vmname = read-host "Please Enter the VM Name"
}while ((test-azurename -service $vmname) -eq $true)
return $vmname
}
########## MAIN SCRIPT BODY #############################################
#Import Settings File
write-verbose -Message "Importing Azure Settings...."
Import-AzurePublishSettingsFile $azuresettings
cls
#Get Azure Supscription and Set Active One
$azuresubscription = GetAzureSubScription
Select-AzureSubscription -SubscriptionName $azuresubscription.SubscriptionName
#Get Azure Storage Accounts and Bind it to the Current Subscription
$azurestoracct = GetAzureStorage
Set-AzureSubscription -SubscriptionName $azuresubscription.SubscriptionName -CurrentStorageAccount $azurestoracct.StorageAccountName
#Get All Available Cloud Services and Choose
$azureservice = GetAzureCloudService
#Find free phemoral port
$port = GetFreePort
#Find the Correct Afinity Group
$affinitygroup = GetAzureNetConf $workdir
#Find the Location
$location = $affinitygroup.location
#Select The Correct Image Type Windows / Linux to search properly
$imageos = GetImageType
#Select the correct images
$imagetouse = GetAzureImages $imageos
#Select the correct VM Size
$vmsi = GetVMSize
#Get the VMName
$vmname = GetVMName
$vmusername = read-host "Please Enter The Administrator Username (not Admin or Administrator or for Linux not root)"
$vmpassword = read-host "Please Enter the Password you wish to use"
cls
write-host "All Information Gathered..."
write-host "Deploying VM... Please Wait...."
if ($imageos -eq "Linux")
{
$epname = $vmname + "ep_SSH"
$vmconfig = New-AzureVMConfig -ImageName $imagetouse.ImageName -InstanceSize $vmsi -name $vmname
Add-AzureProvisioningConfig -Linux -LinuxUser $vmusername -Password $vmpassword -vm $vmconfig|out-null
Set-AzureSubnet -SubnetNames $subnettouse -vm $vmconfig |out-null
Add-AzureEndpoint -Name $epname -Protocol tcp -localport 22 -PublicPort $port -vm $vmconfig|out-null
New-AzureVM -ServiceName $azureservice -vms $vmconfig -VNetName $site
}
else
{
$epname = $vmname + "ep_RDP"
$vmconfig = New-AzureVMConfig -ImageName $imagetouse.ImageName -InstanceSize $vmsi -name $vmname
Add-AzureProvisioningConfig -Windows -AdminUsername $vmusername -Password $vmpassword -vm $vmconfig |out-null
Set-AzureSubnet -SubnetNames $subnettouse -vm $vmconfig |out-null
Add-AzureEndpoint -Name $epname -Protocol tcp -localport 3389 -PublicPort $port -vm $vmconfig |out-null
New-AzureVM -servicename $azureservice -vms $vmconfig -VNetName $site
}
if ($error -ne $null)
{
write-host "There where errors! They Might Be Nothing but you have to check :)" -ForegroundColor Yellow -BackgroundColor Black
write-host "The Last Error recorded is `n" $error[0]
}
write-host "Done!"
Comments
- Anonymous
December 18, 2013
In a previous post I gave some steps to troubleshoot connecting to your Azure VM via Powershell. You