Skip to main content
Dynamics 365

Coffee Break – Using Windows PowerShell to provision virtual machines

The  previous coffee break was about creating a new Virtual Hard Disk (vhd). This time we will create the rest of the machine (VM) in Microsoft Azure.

Coffee Break 10 – Provisioning (Creating a Virtual Machine (VM) in Azure)

A central part of provisioning a Microsoft Dynamics NAV deployment is of course to provision the actual machine (well the virtual actual one) that the system is running on. The provisioning script on the Microsoft Dynamics NAV product media has two options – both in the “WindowsPowerShellScripts\Cloud\HowTo” folder:

  • Example-1VM.ps1 – Provision one VM that runs both SQL Server and Microsoft Dynamics NAV
  • Example-2VM.ps1 – Provision two machines, one for SQL Server and one for Microsoft Dynamics NAV

In this post we will keep it simple and just provision one machine. In later posts we will see how to install Microsoft Dynamics NAV on it. This post assumes that you have an Azure account. If you don’t then you can request a free trial here http://azure.microsoft.com/en-gb/pricing/free-trial/ .

Choose an image

In the Azure Management Portal (https://manage.windowsazure.com ), go to New -> Compute -> Virtual Machine -> From Gallery, then pick one, and follow the rest of the steps to create a new VM.

To do this with Windows PowerShell, first connect your PowerShell prompt to your Azure subscription:

Add-AzureAccount

Use Get-AzureVMImage to get a list of available images:

Get-AzureVMImageselect ImageFamily | Group-Object ImageFamily | Format-TableAutoSize

 Browse through that list and choose any image you like. For this example let’s pick one that already has SQL Server installed:

$imageFamily = “SQL Server 2014 RTM Standard on Windows Server 2012 R2”

The image library on Azure is very much alive and keeps changing as machines get updated. This will choose the latest version of the image family you chose:

$imageName = Get-AzureVMImage | where { $_.ImageFamily -eq $imageFamily } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1

 

The VM you make will live in a Service. The Service name must be unique world-wide. So pick a Service name:

$ServiceName = “mytestserviceabcdef

Test-AzureNameService $ServiceName 

Test-AzureName returns True if someone somewhere in the world already picked the name you specified. You must update ServiceName until Test-AzureName returns False.

Next, decide on the name of your VM. This only has to be unique for yourself:

$Name = “NAVPC

 

Then we are ready for the main event!

Creating a new VM

In Windows PowerShell: 

New-AzureQuickVMImageName $imageNameServiceName $ServiceNameName $NameWindowsLocationWest Europe” –AdminUsername vmadmin -PasswordHelloWorld2” –WaitForBoot

Notice the last parameter here –WaitForBoot. New-AzureQuickVM will provision and boot a new VM. If we specify –WaitForBoot, then the command will not complete until the new VM is booted (or failed), which can take 5-10-15 minutes or more or less. If you want the command to return sooner, then run it without this parameter. In that case, go to “Virtual Machines” in your Azure portal, and refresh to see status of your new VM while it is provisioning. Or watch status from PowerShell:

Get-AzureVMServiceName $ServiceNameName $Name

Or to have a loop that checks and updates every 5 seconds, look in the provisioning tool in NAVRemoteAdministration\Azure\Wait-VMReadyState.ps1. With this method, if a user is waiting for the script to finish then at least they know that it is processing.

If the script fails the first (few) times, then second time you run it you may get error that Service already exists. In this case, go to “Cloud Services” in your Azure portal, and clean up (delete the service that was created). Or do it via PowerShell:

Remove-AzureService $ServiceName

 

After this, you should have your new VM ready and you can connect to it from the Azure portal. Or if you find it hard to leave your favourite environment (ISE), connect to your new machine directly from PowerShell:

Get-AzureRemoteDesktopFileServiceName $ServiceName Name $NameLocalPath c:\Temp\remote.rdp

c:\Temp\remote.rdp

  

 

Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA

Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA

Bas Graaf, Senior Software Engineer Dynamics NAV