PowerCLI Backup vCenter Guest OS VM Customization Specifications

Recently I migrated from an existing vCenter 6.5 installation to a new vCenter 6.7 installation. I was replacing the existing SSO domains and linking with another vCenter in another site and with the previous vCenters being deployed prior to starting at this job I wanted to start from scratch. As someone who backups up and reports as much of the environment as possible I assumed that just disconnecting ESXi hosts from one vCenter and connecting to the new one would have been good enough. Turns out I was wrong.

After the migration to the new vCenter everything seemed good until someone went to deploy a new VM and saw all the VM customization specs were missing. Having never backed up, exporting, or migrating these guest OS specs before I was afraid I needed to manually re-create each and every one of them. Luckily, VMware thought of this and gives us the ability to just click on a guest OS spec and choose “Export” and then we get an XML file that can be imported into the new vCenter. This got me thinking if there was a way to do this via PowerCLI.

As part of my daily VMware report, I have included the following script to automatically back up these VM customization specifications to a location on my server in XML format. As new specs are created they will be automatically picked up by this script so there is no configuration required. Just run and you get the backup. Now let’s get to the script.

This first line is just to define the output location of these backups. Ensure you include the last backslash as we’ll add the file name to this variable and then we’re going to get a list of all the VM guest OS customization specs and save it to the variable “$specs”:

$location = "C:\Scripts\Specs\"
$specs = Get-View CustomizationSpecManager

Now that we have a list of all the OS specs, we’re going to open a ForEach loop do a backup of each one. Because the list of specs sits in $specs.Info instead of each one being listed in just $specs, we need to add the “.Info” in the loop definition.

ForEach ($spec in $specs.Info) {

Then we grab the name of the spec ($spec.Name) to the variable $specName. We set the file name ($outFile) as the location listed in line 1, add the name of the spec we just captured, then add “.xml” to the end of the file.

$specName = $spec.Name
$outFile = $location + $specName + ".xml"

Now that we have the spec name saved and the output location defined, we can actually get all the details of the OS customization spec and perform the export. $output captures the details of the OS customization spec for the specified spec ($specName). We then convert that spec detail to XML format, then export it to the defined file name and location and close the loop.

$output = $specs.GetCustomizationSpec($specName)
$specs.CustomizationSpecItemToXml($output) | Out-File $outFile }

And with that we’re able to automatically backup these guest OS specs in case of a vCenter loss or someone deleting/changing one accidentally. You could also add the date to the file name in case you’re concerned about versioning. Below is the completed script:

$location = "C:\Scripts\Specs\"
$specs = Get-View CustomizationSpecManager
ForEach ($spec in $specs.Info) {
$specName = $spec.Name
$outFile = $location + $specName + ".xml"
$output = $specs.GetCustomizationSpec($specName)
$specs.CustomizationSpecItemToXml($output) | Out-File $outFile }