Sashkin - Fotolia

Tip

Manage VMM library servers with PowerShell

Using PowerShell commands can save you time when searching for all the shares on a VMM library server, exporting and importing VMM library resources, and checking for VMM templates.

While it's relatively easy to perform operational tasks using System Center Virtual Machine Manager, PowerShell can offer greater flexibility for repetitive and time-consuming tasks. Luckily, there are several PowerShell commands that ease Virtual Machine Manager (VMM) library server and resource management.

When installing a VMM library server, first make sure to add a VMM library share, which will host resources such as hard disk files, ISO files, scripts and drivers that VMM will use to deploy new VMs.

If you need to search for Windows shares that are capable of becoming VMM library shares, execute the Find-SCLibraryShare PowerShell cmdlet, as shown below:

Find-SCLibraryShare -ComputerName "SCServer1.TechTarget.com"

Executing this command will list all the Windows shares you can add to a VMM library share. If you would like to list all the shares that have already been added to a library server, execute the following command:

Find-SCLibraryShare -LibraryServer "SCServer1.TechTarget.com"

Export and import VMM library resources

You might want to export library resources, such as hard disk files or ISO files, to a location for manual distribution. In that case, use the Export-LibraryPhysicalResource PowerShell cmdlet, as shown below:

$ThisResource = Get-SCVirtualHardDisk -Name "MyISOFile.ISO"

Export-SCLibraryPhysicalResource -Resource $ThisResource -Path "C:\ISOFiles\" -OverwriteExistingFiles

Note that the Get-SCVirtualHardDisk PowerShell cmdlet gets the reference for the resource you specify, and then the second command actually exports the resource to the location you specify. The -OverwriteExistingFiles parameter ensures you don't receive an error message or prompt if the export command finds a similar resource in the location you specify.

You can also specify a network location to store exported resources, as shown below:

$ThisResource = Get-SCVirtualHardDisk -Name "MyVHDXFile.VHDX"

Export-SCLibraryPhysicalResource -Resource $ThisResource -Path "\\FileServer1\ShareNameHere" -OverwriteExistingFiles

To import a specific resource to a VMM library server, execute the following PowerShell command:

Import-SCLibraryPhysicalResource -SourcePath "C:\ResourceDIR\MyVHDXFile.VHDX" -SharePath \\LibraryServer1\MSSCVMMLibrary\ThisLibraryResourceShare" OverwriteExistingFiles

This command gets the MyVHDXFile.VHDX resource from the C:\ResourceDIR folder and then imports it to a library server in ThisLibraryResourceShare.

Find resources in a VMM library server

There are times when you might want to search for resources in a VMM library server. For example, you might want to check how many ISO files exist in the library share.

Similarly, you might want to check if a specific hard disk file or an ISO file exists in the library share. Below are a few PowerShell commands that use various VMM cmdlets to find a resource in the library share.

To find a specific resource, such as a hard disk file in a library server, execute the following command:

Get-SCVirtualHardDisk | Where { $_.Name -eq "ThisVHDX.VHDX" -and $_.LibraryServer.Name -eq "LibraryServer1.TechTarget.com" }

To find hard disks that match with a given string, use the -like switch, as shown in the command below:

Get-SCVirtualHardDisk | Where { $_.Name -like "Windows*" -and $_.LibraryServer.Name -eq "LibraryServer1.TechTarget.com" }

To list all the virtual hard disk files associated with a VMM template, execute the following command:

Get-SCVMTemplate | Where { $_.Name -eq "ThisTemplateName"} | Get-SCVirtualHardDisk

List VMM templates

Apart from storing hard disk files, ISO files and drivers, library servers also store VMM and service templates. If you want to get a list of the VMM templates stored in a library server, execute the PowerShell command below:

Get-SCVMTemplate -All -VMServer "ThisVMServer.TechTarget.com"

To export the list in a CSV file, use Export-CSV at the end of the command:

Get-SCVMTemplate -All -VMServer "ThisVMServer.TechTarget.com" | Export-CSV C:\Temp\AllSCVMTemplates.CSV

If you are looking for a specific VMM template in a library server, execute the following command:

Get-SCVMTemplate -VMServer "ThisVMServer.TechTarget.com" | Where { $_.Name -eq "Windows Server 2016 Template" }

To list all the VMM templates that match a given string, execute the PowerShell command below:

Get-SCVMTemplate -VMServer "ThisVMServer.TechTarget.com" | Where { $_.Name -like "Windows 10*" }

Check for dependent resources in VMM library servers

In addition to storing independent objects, a library server also stores dependent objects. For example, a virtual hard disk that is associated with a VM template is called a dependent resource.

To list or see if a particular object has any dependency, execute the Get-SCDepedentLibraryResource PowerShell cmdlet, as shown in the command below:

$ThisVHD = Get-SCVirtualHardDisk | Where { $_.Name -eq "Windows2016" }

Get-SCDepedentLibraryResource -LibraryResource $ThisVHD

The first command gets the VHD file named Windows2016 from a library server and stores it in the $ThisVHD variable. The second command shows whether the VHD file is associated with a VM template or VM.

Be sure to check for dependency before removing a resource stored in a library server.

Dig Deeper on IT systems management and monitoring