Ok , exporting values of custom attributes , in other words our custom annotations for vms.
When running with specific -Attribute parameter :
When running without the -Attribute parameter it will grab all annotations for the specific target type (VirtualMachine,ResourcePool, Folder,VMHost,Cluster,Datacenter).
I have not implemented the ‘global’ target type here. Might be the case that i will do it later on.
The idea behind is to save the report to a file , and then with the import function , apply to vms that were imported to the new VC.
[sourcecode language=”powershell”]
Function Export-CustomAttributes
{
<#
.SYNOPSIS
Returns VM report with custom attributes for vms that have currently custom attributes applied.
.DESCRIPTION
Returns VM report with custom attributes for vms that have currently custom attributes applied.
Can be ran with the -Attribute parameter to specify which exactly attributes should be recorded.
Cmdlet expects that we are already connected to a specified virtual center system. The sourceVC
parameter is needed to distinguish between VC if you are connected to more than 1 VC. It also
used as the identificator of VC connection if you are connected to a single VC instance.
.PARAMETER sourceVC
Specify the Virtual Center from which it will download the report.
.PARAMETER TargeType
Specify for which kind of attributes this list will be generated: VirtualMachine,ResourcePool,
Folder,VMHost,Cluster,Datacenter. Global is not included in this version.
.PARAMETER Attribute
Specify for which attributes this list will be generated, you can check which one you have by
running get-customattributes cmdlet. Attributes inside the specific TargetType only.
.EXAMPLE
PS C:\> $CA = Export-CustomAttributes -sourceVC ‘VC004.domain.biz’ -targetType ‘virtualmachine’ -Attribute ‘ApplicationOwner’,’Function’
Will get 2 custom attribute for all virtualmachine that have currently customattributes
.EXAMPLE
PS C:\> $CA = Export-CustomAttributes -sourceVC ‘VC004.domain.biz’ -targetType ‘virtualmachine’
Will get all custom attributes from virtualmachine type for all virtualmachine that have currently customattributes
.NOTES
NAME: Export-CustomAttributes
AUTHOR: Grzegorz Kulikowski
NOT WORKING ? #powercli @ irc.freenode.net
.LINK
http://psvmware.wordpress.com
#>
param(
[Parameter(Mandatory=$true)][string]$sourceVC,
[Parameter(Mandatory=$true)][VMware.VimAutomation.ViCore.Types.V1.AnnotationManagement.CustomAttributeTargetType]$targetType,
[string[]]$Attribute
)
#▅▇█▓▒░Don’t forget to implement the global option as per :http://pubs.vmware.com/vsphere-60/index.jsp#com.vmware.wssdk.apiref.doc/vim.CustomFieldsManager.FieldDef.html
#▅▇█▓▒░Right now the $targetType can’t be null, so we can’t take ‘globals’ , globals are the ones which are empty.
$SourceCustomAttr = @{}
if ($Attribute)
{
Get-CustomAttribute -Server $sourceVC -TargetType $targetType -Name $Attribute | % { $SourceCustomAttr+=@{$_.Key=$_.Name}}
} else
{
#This is custom to the environment, depending on which plugins you were installing to your VC, you might have different special attributes, which might not be needed on the other side.
Get-CustomAttribute -Server $sourceVC -TargetType $targetType | ? {$_.Name -notmatch "PnC|vrm|FA.|Xd"} | % { $SourceCustomAttr+=@{$_.Key=$_.Name}}
}
#get only those that have custom attributes
$SourceVms = Get-View -ViewType virtualmachine -Property ‘CustomValue’,’Name’ -Server $sourceVC | ?{$_.customvalue}
$ReportSourceVms = $SourceVms | Select Moref,Name,CustomValue
Foreach ($VM in $ReportSourceVms)
{
foreach ($CustomAttribute in $SourceCustomAttr.GetEnumerator())
{
Add-Member -InputObject $VM -MemberType NoteProperty -Name $CustomAttribute.Value -Value ($VM.CustomValue|?{$_.Key -eq $CustomAttribute.Name}).value
}
}
return $ReportSourceVms | Select-Object * -ExcludeProperty CustomValue
}
[/sourcecode]