How to replicate VMM templates from one Library server to another Library Server programmatically?

Many customers ask how they can replicate VMM templates to other Library servers (within a single VMM instance). A template is really a collection of properties stored in the database and pointers to VHDs stored on the Library server (exposed through a share path). Customers with multiple datacenters or regional locations will most probably want local Library servers in every major hub to reduce WAN traffic and avoid paying latency penalty when deploying new VMs from template.

Natively, VMM allows you to copy (clone) an existing template . By default the new template will remain on the same Library server and even though you can change the virtual hard disk through the UI, for very large environments this manual effort is error prone. So the question now, how can a user replicate VHDs and VMM templates to secondary Library Servers programmatically?

 

NOTE: The information below only applies to Library servers managed by a single VMM instance. Replication of templates between independent VMM instances is not addressed here.

 

1. You can accomplish this in 2 steps. The first involves using some replication technology (not offered throughVMM)… for example robocopy or DFSR. Cheng Wei, a fellow PM on the VMM team, published a blog post detailing the ins and out of VMM and DFSR (https://blogs.technet.com/chengw/archive/2008/08/26/dfs-on-vmm-library.aspx). The second step is to clone and modify VMM templates using VMM's Powershell CLI.

2. For this example I will assume you have a Library server in NY as the primary and a Library Server in Seattle as secondary.

3. I will use "WindowsServer2003SP2withIIS.vhd" as the virtual hard disk for the template.

4. Copy the VHD to the secondary Library server and refresh the Library share so it can index the file and bring it under management

5. Start by creating your template on the New York primary Library server. Provide all the hardware and guest os profile information necessary, including the virtual disk (i.e. WindowsServer2003SP2withIIS.vhd) stored in the Library. Name the template "W2K3SourceTemplate01"

6. Create a copy/clone of "W2K3SourceTemplate01", leaving all setting default, and name it "W2K3SeattleTemplate02." This step is really fast, keep in mind that the clone is simply creating a new database record for the template with pointers to the VHD.

7. Get the template object "W2K3SeattleTemplate02"

8. Then get the virtual disk drive for the template

9. Remove the virtual disk drive. At this point if you go to the VMM UI and open the properties for the template, the disk should no longer be present

10. Back in Powershell, get the virtual hard disk object, "WindowsServer2003SP2withIIS.vhd", from the Seattle Library Server

11. Create a new virtual disk drive using this VHD and attach it to the Template "W2K3SeattleTemplate02"

12. After the Library servers refresh, the presence of "W2K3SeattleTemplate02" will switch from the primary Library Server to the secondary.

 

Here is the powershell script I used to go this:

 

PS C:\> $os = Get-OperatingSystem | where {$_.Name -eq "Windows Server 2003 Standard Edition (32-bit x86)"}

 

PS C:\> $vhdprimary = Get-VirtualHardDisk | where {$_.Name -eq "WindowsServer2003SP2withIIS.vhd" -and $_.SharePath -eq "\\NYCVMMLibrary.contoso.com\MSSCVMMLibrary\WindowsServer2003SP2withIIS.vhd"}

 

PS C:\> $templateprimary = New-Template –Name “W2K3SourceTemplate01” –NoCustomization –OperatingSystem $os –VirtualHardDisk $vhdprimary

 

PS C:\> $templatesecondary = New-Template -Template $templateprimary -Name W2K3SeattleTemplate02

  

PS C:\> $vddsecondary = @($templatesecondary.VirtualDiskDrives)

 

PS C:\> Remove-VirtualDiskDrive -VirtualDiskDrive $vddsecondary[0]

 

PS C:\> $vhdsecondary = Get-VirtualHardDisk | where {$_.Name -eq "WindowsServer2003SP2withIIS.vhd" -and $_.SharePath -eq "\\SEAVMMLibrary.contoso.com\VHDs\WindowsServer2003SP2withIIS.vhd"}

 

PS C:\> New-VirtualDiskDrive -VirtualHardDisk $vhdsecondary -Template $templatesecondary -IDE -BUS 0 -LUN 0