hi everyone, this week i am going to post a new set of PowerShell scripts that will allow you to save the Virtual Machine metadata in VMM. The saved metadata can be applied later on in the event that you add and remove the host from VMM management.

A scenario where this issue comes up is when something goes wrong with your host in VMM and you need to remove it from management and re-add it to VMM (the host can also be a cluster). Typically in a situation like this you will loose all the metadata associated with your virtual machines. Such metadata includes the custom properties, descriptions, tags, owner, cost center, etc. If it is 1 or 2 VMs, its not a big deal to add them back, but when you are talking about a cluster with 200 VMs it is quite an effort. The scripts i am posting today will enable you to save the metadata and then restore them. Be aware that i am calling VMM metadata here only the properties that are VMM specific. Any virtualization properties of a virtual machine like the amount of RAM it is assigned are already dealt with by VMM. The VMM refreshers will refresh this information from the virtualization host into VMM :). So rest assured for that case.

A typical workflow will look like this.

  1. A problem occured and you discover that you need to remove a host from management in VMM
  2. You run the first script called SavingVMMetadata.ps1 (the contents of this script are included at the bottom of this blog post) from a VMM PowerShell window that has an active connection to VMM (to establish an active connection run the get-vmmserver "servername" cmdlet). This script will save the metadata in a file. Since you have the code for it, you can modify the "get-vm" call to target a specific set of VMs if you want the metadata to be saved only for a handful of VMs
  3. Now you have the metadata in a file called exportedproperties.xml. This file will be located on the same folder from where you run the SavingVMMetadata.ps1 PowerShell script. You can see an example exportedproperties.xml file below. It includes all the metadata properties that the script will save for you from VMM.
  4. Remove the host or cluster from VMM
  5. Re-add the host or cluster with VMM
  6. Take a FULL database backup of the VMM database [Just in case; this is a safety net in case something goes wrong]
  7. Now, you are ready to run the second script.
  8. First, you might want to open the XML file and filter it down to just the VMs that you want to restore the metadata for. You can just create a copy of the file and save it using the same name to another location
  9. You can now run the script called RestoringVMMetadata.ps1 (this script is attached to this blog post) from a VMM PowerShell window that has an active connection to VMM. This script will look for the exportedproperties.xml file in the same location where the script is executed from.
  10. Once the script executes it will try to match an exported VM with a current VM in the system. The matching requirements are that the same VM with the same name needs to exist on the same host computer. if that is the case it will apply the metadata from the XML file. Otherwise an error will be shown for that Virtual Machine if we fail to apply the metadata. if the VM can't be found at all on that same host, then the script will finish silently without an error being shown. You will figure that out when your metadata is not appllied :)
  11. If all goes well in the script, you will see an output like this and no PowerShell errors from VMM. Make sure that any user roles are created ahead of time if your metadata includes the userroleid of a VM.

<<

We matched the VM mytestvm1 with exported properties to an existing VM on host HAMUNAPTRA.contoso.com. Current VM has VMM ID 4ea45174-4dc8-4c18-82ef-9e65f7cf552c and host-based ID B82609B7-B3FE-422A-9F8D-4456911CE0FE. Exporte
d (old) VM has VMM ID 0d6d4736-12c5-44af-a77e-3d5d02207835 and host-based ID B82609B7-B3FE-422A-9F8D-4456911CE0FE

 >>

12. Voila, you are now done :). Good luck.

Below is a sample VM from an exported XML metadata file. You can see here the properties we export as part of this script.

<<

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>Selected.Microsoft.SystemCenter.VirtualMachineManager.VM</T>
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="HostName">HAMUNAPTRA.contoso.com</S>
      <S N="Name">mytestvm1</S>
      <S N="VMId">B82609B7-B3FE-422A-9F8D-4456911CE0FE</S>
      <G N="ID">0d6d4736-12c5-44af-a77e-3d5d02207835</G>
      <S N="Custom1">custom1</S>
      <S N="Custom2">custom2</S>
      <S N="Custom3">custom3</S>
      <S N="Custom4">custom4</S>
      <S N="Custom5">custom5</S>
      <S N="Custom6">custom6</S>
      <S N="Custom7">custom7</S>
      <S N="Custom8">custom8</S>
      <S N="Custom9">custom9</S>
      <S N="Custom10">custom1000</S>
      <S N="CostCenter">19997374509</S>
      <G N="UserRoleID">ad564691-85eb-40a5-9d5f-166c919d645d</G>
      <S N="Tag">vmuniquetag</S>
      <S N="Owner">contoso\enduserforVMM1</S>
      <S N="OwnerSid">SIDVALUEGOESHERE</S>
      <S N="Description">this is a test description</S>
    </MS>
  </Obj>
</Objs>

>>

 SavingVMMetadata.ps1 --> copy the contents below into the PowerShell script file "SavingVMMetadata.ps1"

<<

# Export Time
# This PowerShell script will export all metadata from the selected VMs.
# To export metadata only for some specific VMs and not all VMs in the system, simply change the get-vm call below to a more targeted PowerShell script
# that will enable you to only save the metadata for the VMs you want to.
# metadata is saved on the same folder where you run this script under the name exportedproperties.xml
get-vm | select hostname, name, vmID, ID, @{Name='Custom1';Expression={$_.CustomProperties[0]}}, @{Name='Custom2';Expression={$_.CustomProperties[1]}},@{Name='Custom3';Expression={$_.CustomProperties[2]}},@{Name='Custom4';Expression={$_.CustomProperties[3]}},@{Name='Custom5';Expression={$_.CustomProperties[4]}},@{Name='Custom6';Expression={$_.CustomProperties[5]}},@{Name='Custom7';Expression={$_.CustomProperties[6]}},@{Name='Custom8';Expression={$_.CustomProperties[7]}},@{Name='Custom9';Expression={$_.CustomProperties[8]}},@{Name='Custom10';Expression={$_.CustomProperties[9]}}, CostCenter, UserRoleID, Tag, Owner, OwnerSid, Description | export-clixml "exportedproperties.xml"

>>