Skip to content

Instantly share code, notes, and snippets.

@DamianFlynn
Created February 2, 2014 15:22
Show Gist options
  • Select an option

  • Save DamianFlynn/8769867 to your computer and use it in GitHub Desktop.

Select an option

Save DamianFlynn/8769867 to your computer and use it in GitHub Desktop.
# Import Time
# First import the metadata in question from the exported xml file
$exportedquery = Import-Clixml "exportedproperties.xml"
# Now we have the same query as set above during export time in our hands in a variable called exportedquery
# let's do some setting of values
# first, get all the VMs that currently exist in the system
$currentVMs = get-vm | ? {$_.HostName -like "bil-vm-opsc1*"} | 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 matched the VM based on name and host. we can't do anything else from here on
$vmname = $oldVM.Name
$hostname = $oldVM.Hostname
$oldvmid = $oldVM.VMID
$curvmid = $curVM.VMID
$oldID = $oldVM.ID
$curID = $curVM.ID
"Matched VM $vmname on $hostname."
# now set the properties
# User Role Reference for later
$vmmuserrole = get-vmmuserrole | where {$_.ID -eq $oldVM.UserRoleID}
# Cost Center Format
if ($oldVM.CostCenter -eq $null)
{
$costcenter = ""
} Else {
$costcenter = $oldVM.CostCenter
}
# Description Format
if ($oldVM.Description -eq $null)
{
$description = ""
} else {
$description = $oldVM.Description
}
# Now, the work - Check and set the Cloud First, as any Owner will be wiped in this operation
if ($oldVM.Cloud -eq $null)
{
$cloud = ""
} else {
$cloud = Get-SCCloud -id $oldVM.Cloud.ID
$resultVM = set-vm -VM $curVM -Cloud $cloud
}
# Now, assuming we have a Cloud dealt with, we can move on with the rest of the metadata
if ($curVM.Owner -eq "")
{
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 -QuotaPoint $oldVM.QuotaPoint
} else {
$ownersid = $oldVM.Owner
"Changing Onwer of VM $vmname to $ownersid"
$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 -QuotaPoint $oldVM.QuotaPoint
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment