Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy VM Annotations from one vCenter server to another
function Copy-VCenterAnnotations()
{
<#
.SYNOPSIS
This function copes all custom attributes and annotations from the VMs on one VCenter server to another.
.PARAMETER SourceVCenter
Specifies the source vCenter server.
.PARAMETER DestinationVCenter
Specifies the destination vCenter server.
.EXAMPLE
Copy-VCenterAnnotations -SourceVCenter oldvcenter.domain.com -DestinationVCenter newvcenter.domain.com
Created new Custom Attribute Application:.
Created new Custom Attribute Environment.
Created new Custom Attribute Responsible Group.
Searching for VMs with annotations assigned to them... 7 found.
Assigning annotations to VM vm-161bffae-aa21-42a6-97bb-752c5395c3df...
Annotation created_at = 2018-04-18T14:23:17Z
Annotation deployment = service-instance_0618a194-b2ab-4dfd-b15e-5345373846fe
Annotation director = p-bosh
Annotation id = ec434247-b3a9-47e7-a657-be6fb7b5e725
Annotation index = 0
Annotation instance_group = master
Annotation job = master
Annotation name = master/ec434247-b3a9-47e7-a657-be6fb7b5e725
Assigning annotations to VM feae0dbe-75d1-4ccf-bb89-dcc152810e3b...
Annotation created_at = 2018-04-18T14:23:17Z
Annotation deployment = service-instance_0618a194-b2ab-4dfd-b15e-5345373846fe
Annotation director = p-bosh
Annotation id = a31301b2-6145-49ce-81ec-2f6386adf6d4
Annotation index = 1
Annotation instance_group = worker
Annotation job = worker
Annotation name = worker/a31301b2-6145-49ce-81ec-2f6386adf6d4
#>
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[string] $SourceVCenter,
[Parameter(Mandatory)]
[string] $DestinationVCenter
)
Import-Module VMware.VimAutomation.Core -ErrorAction Stop
$customAttributeExceptions = `
@(
"com.vmware.vsan.clustermembers",
"com.vmware.vsan.clusterstate",
"com.vmware.vsan.diskconversionstate",
"com.vmware.vsan.witnesshoststate"
)
$srcCon = Connect-ViServer $SourceVCenter
$dstCon = Connect-ViServer $DestinationVCenter
$srcCustomAttribute = Get-CustomAttribute -Server $srcCon | Where-Object { $_.Name -inotin $customAttributeExceptions}
$dstCustomAttribute = Get-CustomAttribute -Server $dstCon
$dstVms = Get-Vm -Server $dstCon
if ( $srcCustomAttribute )
{
foreach ( $ca in $srcCustomAttribute )
{
if ( !($dstCustomAttribute | Where-Object { $_.Name -ieq $ca.Name -and $_.TargetType -ieq $ca.TargetType }) )
{
if ( $PSCmdlet.ShouldProcess($dstCon.Name, "Create custom attribute $($ca.Name) on target type $($ca.TargetType)") )
{
$newCa = New-CustomAttribute -Server $dstCon -TargetType $ca.TargetType -Name $ca.Name
Write-Host ("Created new Custom Attribute $($newCa.Name).") -ForegroundColor Green
}
}
}
$dstCustomAttribute = Get-CustomAttribute -Server $dstCon
Write-Host ("Searching for VMs with annotations assigned to them... ") -NoNewline
$srcVm = Get-Vm -Server $srcCon | Where-Object { $_.CustomFields.Values | Where-Object { $_ } }
if ( $srcVm )
{
Write-Host ("$($srcVm.Count) found.") -ForegroundColor Green
foreach ( $vm in $srcVm )
{
$srcAnnotation = $vm | Get-Annotation
$dstVm = $dstVms | Where-Object { $_.Name -ieq $vm.Name }
if ( $dstVM )
{
Write-Host ("Assigning annotations to VM $($dstVm.name)...")
$dstVmAnnotations = $dstVm | Get-Annotation
foreach ( $ann in $srcAnnotation )
{
if ( !($dstVmAnnotations | Where-Object { $_.Name -ieq $ann.Name -and $_.Value -ieq $ann.Value }) )
{
if ( $PSCmdlet.ShouldProcess($dstVm.name, "Add annoation $($ann.Name) = $($ann.Value)") )
{
$attrib = $dstCustomAttribute | Where-Object { $_.TargetType -ieq "VirtualMachine" -and $_.Name -ieq $ann.Name }
if ( $attrib )
{
$newAnn = $dstVm | Set-Annotation -Value $ann.Value -CustomAttribute $attrib
Write-Host (" Annotation $($newAnn.Name) = $($newAnn.Value)") -ForegroundColor Green
}
else
{
throw ("Unable to find a custom attribute on $($dstCon.Name) named $($ann.Name)!")
}
}
}
}
}
else
{
Write-Warning ("Annotated VM $($vm.Name) not found on destination vCenter server $($dstCon.Name)!")
}
}
}
else
{
Write-Host ("none found.") -ForegroundColor Yellow
Write-Warning ("Unable to find any VMs with annotations assigned!")
}
}
else
{
Write-Warning ("Unable to find any custom attributes on VCenter $($srcCon.name)!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment