Coffee Break – Windows PowerShell and creating a Hyper-V disk

After covering some basic management operations for Microsoft Dynamics NAV, we thought it might be the time to look under the hood of the Dynamics NAV cloud provisioning script. The script in its entirety is fairly large and complex. But stripped away from all the wrappings and bundling, it can be broken down to small, fairly simple units.

Coffee Break 9 – Provisioning (Creating Hyper-V disks)

Provisioning is the PowerShell scripts on the Dynamics NAV product DVD in the folder WindowsPowerShellScripts\Cloud\. It’s a set of scripts that are designed to take the parameters you specify, then provide a whole new Dynamics NAV machine (or two) in Microsoft Azure, with Dynamics NAV running on it/them. In short, the script creates a vhd with NAV DVD and a database copied from locations you provide through parameters. Next, that VHD is uploaded to Azure Storage (also specified through parameters). Then, a VM with SQL image you chose is created and the VHD is mounted on to that VM, the contents of VHD (NAVDVD and database) are copied to the VM and finally the script deploys NAV on the VM.

You might ask yourself why, given that today you only need to provide a few parameters to run the script, would you want to break it up into bits? Well, first of all, it will give clarity of what happens when provisioning and why, which makes this process a bit less of a black box. That again can help troubleshooting the script (should you run into any issues on your environment), tailor the script to your needs (it is, after all, made for one standard scenario), give you the chance to select and use the parts of the script that you might find useful for your daily management on your Dynamics NAV solution, whether physical or virtual environments, on-premises, or in the cloud.

The entire script consists of quite a few cmdlets and we have tried to break it up and group it into smaller logical units that we will present over several coming posts.

So get your coffee mugs filled up and let’s start.

The first part of the provisioning script creates a VHD with a Dynamics NAV DVD, and uploads that to your azure storage (also created, if the one with the name provided in parameters list doesn’t already exist).
To create a VHD, we simply invoke diskpart.exe. This utility is used to enable storage configuration from a script, a remote session, or another command prompt. Diskpart enhances the Disk Administrator graphical user interface (GUI).

For more details of how it is used in our cmdlets, see scripts provided in following path: NAVDVD\WindowsPowerShellScripts\Cloud\NAVAdministration\Windows\Vhd”.
However, to make it a bit more interesting and give you an alternative in your every-day operations, we thought that we could provide you with an alternative for this part of the process, using another utility: Hyper-V management module. PowerShell module for Hyper-V management is available on Windows 2008 and 2012 servers, by simply adding a hyper-v role to your server. All you need to do to use it (after adding the Hyper-V role) is remember to run your PowerShell console As Administrator, and enable execution policy (RemoteSigned or Unrestricted).

 

To create a VHD, we’ll perform the following steps: 

1.  Create the disk.

For this New-VHD commandlet is used (part of Hyper-V powershell module). Provide the path to the disk, disk size provided in GBs (calculate the size with what you plan on placing on your disk in mind: NAV DVD/ customer backup database/template database/ your solution/third-party tools, ….) and disk type.
Make sure it’s VHD and not VHDX (if you have a VHDX, convert it with the Convert-VHD Windows PowerShell cmdlet) and make sure the size is 1023GB or less (keep in mind the upload time and network bandwith).  The VHD does not have to be fixed type but will be converted to fixed type by the Add-AzureVhd PowerShell cmdlet when it’s copied into Azure, so might as well be created as fixed.
New-VHD –path “c:\temp\mydisc.vhd” –sizebytes 3gb –fixed

This should take a few seconds for a disk of 3 gbs to complete. Once complete, mount your new vhd:

Mount-VHD –path “c:\temp\mydisc.vhd”

To see the properties of this new disk, including disk number:

Get-VHD “c:\temp\mydisc.vhd”

 

2.  Initialize the disk.

Use the Initialize-Disk commandlet to initialize the raw disk, specify MBR Partition Style.

To initialize the disk you need the disknumber returned by running Get-VHD (see step 1). Then use Initialize-disk cmdlet and specify partition style MBR.
Initialize-Disk -Number 3 -PartitionStyle MBR
Your disk is now initialized.

 

3.  Partition the disk.

Use the New-Partition function, choose a drive letter, or let OS assign a new drive letter to the drive, or assign one yourself using –DriveLetter parameter. Specify the largest partition size that the disk will support (max size here):
New-Partition –DiskNumber 3 -AssignDriveLetter -UseMaximumSize
Or specify the drive letter:
New-Partition –DiskNumber 3 -DriveLetter “M” –UseMaximumSize
Once this has run, you will be automatically prompted with following:
 

You can select format disk here, but as we want a complete PS scenario, we will also do this bit using cmdlet, so select cancel here.

 

4.  Finally format the volume and label it.

If you didn’t make a note of the drive letter, or didn’t specify it, then first get the drive letter from the initialized disk:

Get-Partition -DiskNumber 3

Then format it :

Format-Volume –driveletter “M” -FileSystem NTFS -NewFileSystemLabel “MyStuff”

You’ll be prompted to confirm the action, and after this – the drive is formatted and ready for use.

 

Last but not least, let’s tie all of the above into a single command, and create and format the disk in one go. This time we will create a drive N labeled “My New Stuff”:

New-vhd –path “c:\temp\NewData.vhd” –sizebytes 3gb –fixed|Mount-vhd -passthru|get-disk –number {$_.DiskNumber}|Initialize-Disk -PartitionStyle MBR -PassThru |New-Partition –DriveLetter “N” -UseMaximumSize |Format-Volume -FileSystem NTFS -NewFileSystemLabel “My New Stuff”

Once the drive is ready, copy the files you want to the drive. To copy folder (with its subfolders, preserving the structure):

Copy-Item –path “C:\temp\40262\NAV.8.0.40262.W1.DVD” –Destination “N:\” –recurse
Or copy a zip file containing everything
Copy-item –path “C:\temp\40262\NAV.8.0.40262.W1.DVD.zip” –Destination “N:\”

And lastly, dismount your vhd:
Dismount-vhd –path “c:\temp\NewData.vhd”
And the disk is now ready to be uploaded to Azure storage (we’ll write about that in the next coffee break). Or copy it to another installation or site and mount it to any VM – this disk will work equally well in the cloud or on premises.

 

Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA

Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA

Bas Graaf, Senior Software Engineer Dynamics NAV