Share via


Deploy a VM with Azure PowerShell Preview 1.0 Cmdlets

 

There are tons of examples out there and to be honest this is for as much my reference to go back and grab it when I need it.

so here is a simple script nothing fancy whatsoever but functional Smile

First part lets collect the name you want to assign to the Virtual Machine you are building in Azure! and import the AzureRM module. I am specifically calling AzureRM as I am importing all azure resource modules. However I could just call the ones I need

$vmname = "<insert-your-VM-Name>"
import-module AzureRM

These next two lines are for me specifically, I have multiple subscriptions and want to deploy to a specific one so I am just getting all subscriptions available and then referencing the one of interesting. Interestingly enough you need to specify the tenantId and subscriptionID to select the subscription of interest now…

$subs = Get-AzureRmSubscription -All
Select-AzureRmSubscription -TenantId $subs[4].TenantId -SubscriptionId $subs[4].SubscriptionId

Next I select the storage account, in the subscription I am working with there is just one… in your environment you might want to filter out

$storacct = Get-AzureRmStorageAccount

Next we construct the name for the VHD and access the public blob endpoint URI so we can construct a full path to the VHD for later use

$diskname = $vmname + "_OSDisk"
$vhduri = $storacct.PrimaryEndpoints.Blob.OriginalString + "vhds/${diskname}.vhd"

Next we specify out the resource group name we want to assign our VM to, I had previous created one I wanted to use. and then we also specify the location because it seems ever CmdLet wants it now Smile

$rg = "SCDEMO"
$loc = "North Europe"

Next we select the image we are interested in, and you can vary this as you need

$image = get-azurermvmimage -Location $loc -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter" -Version 4.0.20150916

Now we grab the Vnet and Subnet I previously created as I generally have these in place and don’t really want to create one every time I create a Virtual machine

$vNet = Get-AzureRmVirtualNetwork
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vNet

Now I create new nic with construct the nic name from the VMName, honestly you can assign what ever name you want to a nic as long as its unique to the resource group

$nic = New-AzureRmNetworkInterface -Name "${vmname}_nic1" -Subnet $subnet -Location $loc -ResourceGroupName $rg

We need to capture some credential information, I chose to do it this way as I will loop this code up into a function and want the same username and password for everything but you can prompt using get-credentials if you want

$secpassword = convertto-securestring "LS1setup!" -asplaintext -force
$username = "msadmin"
$creds = New-Object System.Management.Automation.PSCredential($username, $secpassword)

Now here is the part that in every example I see you pipe everything along and more times than not I see people struggle with copying the pipe and getting to work, so I decided to do it this way… First I create the VMConfig variable to store all my settings in and as you can see I constantly reference it in each line.

In this first line I create the variable as previously said, assign the VM Name and specify the size

$vmconfig = New-AzureRmVMConfig  -VMName $vmname -VMSize "Standard_A2"

Next we configure some items around the Operating system, like the local admin credentials and the computername which is the same as the VM Name in my case but you can choose to have it different.

Set-AzureRmVMOperatingSystem -Windows -VM $vmconfig -ProvisionVMAgent -EnableAutoUpdate -Credential $creds -ComputerName $vmname

Next we specify the source image to “clone” off as such…

Set-AzureRmVMSourceImage -VM $vmconfig -PublisherName $image.PublisherName -Offer $image.Offer -Skus $image.Skus -Version $image.Version

As NICS are a separate object now we need to assign our previously created NIC to the actual VM

Add-AzureRmVMNetworkInterface -VM $vmconfig -Id $nic.Id

Now we tell the config we need to build a disk with a specific name and URI and give it the options as required

Set-AzureRmVMOSDisk -VM $vmconfig -Name $diskname -VhdUri $vhduri -Caching ReadWrite -CreateOption fromImage

Last but not least I deploy VM with all of the configuration options

New-AzureRMVM -ResourceGroupName $rg -Location $loc -VM $vmconfig

As I said not pretty but it works Smile

Comments

  • Anonymous
    December 09, 2015
    Hi Jhon, thx for this article
    how can i use my own image vhd using these commands ?? $image = get-azurermvmimage
  • Anonymous
    January 25, 2016
    The comment has been removed