Export (0) Print
Expand All

SaveAndRestoreVMMMetadata

Updated: April 29, 2010

Applies To: Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

Occasionally, issues might occur where you must remove a host or host cluster from management by System Center Virtual Machine Manager (VMM). However, if you use metadata, such as custom properties, descriptions, tags, owner, or cost center, and remove the host or host cluster from management by VMM, you will lose the metadata. For one or two virtual machines, re-adding the metadata can be fairly straightforward; however, for a large number of virtual machines, it can be prohibitive. This situation only applies to VMM-specific properties. Virtualization properties, such as the amount of RAM a virtual machine is assigned, are refreshed from the host to VMM.

The scripts in this topic help you save your metadata before you remove the host or host cluster from management by VMM, and then reapply the metadata after you have re-added your host or host cluster. Use the following steps to implement these scripts in a typical scenario:

  1. In a Windows PowerShell - Virtual Machine Manager command shell, connect to your VMM server by using the Get-VMMServer cmdlet.

  2. After you have copied and saved the SavingVMMMetadata.ps1 script provided later in this topic, run it from the VMM command shell. The script saves your metadata into a file named exportedproperties.xml, and saves the file to the same folder from where you ran the SavingVMMMetadata.ps1 script.

    TipTip
    If you want metadata saved for only a particular set of virtual machines, you can modify the Get-VM cmdlet in the script to get only the virtual machines that you specify.

  3. Remove the host or host cluster from VMM.

  4. Re-add the host or host cluster back to VMM.

  5. Make a full backup of the VMM database.

  6. If you want, you can open the exportedproperties.xml file and filter it to only the virtual machines for which you want to restore the metadata. You can do this by creating a copy of the file and saving it to a different location using the same name.

  7. If your metadata includes the UserRoleID of a virtual machine, ensure that you create the user role before you run the RestoringVMMMetadata.ps1 script.

  8. After you have copied and saved the RestoringVMMMetadata.ps1 script provided later in this topic, run it from a VMM command shell in which you have established a connection to the VMM server. The script looks for the exportedproperties.xml file in the same location from which you run the script.

  9. The script attempts to match an exported virtual machine with a current virtual machine in the system. For them to match, the same virtual machine with the same name has to exist on the same host. If that is the case, it applies the metadata from the XML file. Otherwise, you will receive an error if the script fails to apply the metadata. If the virtual machine cannot be found on the host, the script finishes silently without displaying an error. If the script was successful, and no errors were shown, you receive the following output:

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

The following is an example of a virtual machine from an exported XML metadata file demonstrating the properties that are exported when you run the SavingVMMMetadata.ps1 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>

Disclaimer

Following is the sample SavingVMMMetadata.ps1 script.

 
# Filename:     SavingVMMMetadata.ps1 
# Description:  Exports all metadata from the selected virtual machines. To export
#               metadata for specific virtual machines, change the Get-VM command
#               to retrieve only the virtual machines that you specify. The metadata 
#               is saved to the same folder from where you run this script, and is 
#               saved with 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"

Following is the sample RestoringVMMMetadata.ps1 script.


# Filename:     RestoringVMMMetadata.ps1
# Description:  Restores metadata that was saved using the SavingVMMMetadata.ps1
#               script.

# First, import the metadata from the exported xml file.
$exportedquery = Import-Clixml "exportedproperties.xml"

# Get all of the virtual machines that currently exist.
$currentVMs = get-vm | sort-object Name -descending
foreach ($curVM in $currentVMs)
{
   foreach ($oldVM in $exportedquery)
   {
      if ($curVM.Name -eq $oldVM.Name)
      {
         if ($curVM.Hostname -eq $oldVM.Hostname)
         {
            # We've matched the virtual machine based on name and host. 
            $vmname = $oldVM.Name
            $hostname = $oldVM.Hostname
            $oldvmid = $oldVM.VMID
            $curvmid = $curVM.VMID
            $oldID = $oldVM.ID
            $curID = $curVM.ID
            "We matched the virtual machine $vmname with exported properties to an existing virtual machine on host $hostname. The current virtual machine has the VMM ID $curID and host-based ID $curvmid. The exported (old) virtual machine has the VMM ID $oldID and host-based ID $oldvmid"

            # Set the properties.
            $vmmuserrole = get-vmmuserrole | where {$_.ID -eq $oldVM.UserRoleID}
            if ($oldVM.CostCenter -eq $null)
            { $costcenter = "" } else { $costcenter = $oldVM.CostCenter }
            if ($oldVM.Description -eq $null)
            { $description = "" } else { $description = $oldVM.Description }
            if ($oldVM.OwnerSid -eq "")
            {
               $resultVM = set-vm -VM $curVM -Custom1 $oldVM.Custom1 -Custom2 $oldVM.Custom2 -Custom3 $oldVM.Custom3 -Custom4 $oldVM.Custom4 -Custom5 $oldVM.Custom5 -Custom6 $oldVM.Custom6 -Custom7 $oldVM.Custom7 -Custom8 $oldVM.Custom8 -Custom9 $oldVM.Custom9 -Custom10 $oldVM.Custom10 -CostCenter $costcenter  -Tag $oldVM.Tag -Description $description -UserRole $vmmuserrole
            } 
            else 
            { 
               $ownersid = $oldVM.Owner 
               $resultVM = set-vm -VM $curVM -Custom1 $oldVM.Custom1 -Custom2 $oldVM.Custom2 -Custom3 $oldVM.Custom3 -Custom4 $oldVM.Custom4 -Custom5 $oldVM.Custom5 -Custom6 $oldVM.Custom6 -Custom7 $oldVM.Custom7 -Custom8 $oldVM.Custom8 -Custom9 $oldVM.Custom9 -Custom10 $oldVM.Custom10 -CostCenter $costcenter  -Tag $oldVM.Tag -Owner $ownersid -Description $description -UserRole $vmmuserrole
            }
         }
      }
   }
}

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft