Clone-CustomAttributes using PowerCLI


There is nothing to explain i guess in this function. Just copies the definitions of custom attributes between 2 virtual center servers. It expects that you are connected to both virtual centers.

Function Clone-CustomAttributes 
{
<#
    .SYNOPSIS
        Copies custrom attributes used for annotations , just the definitions.
  
    .DESCRIPTION
        Copies custrom attributes used for annotations , just the definitions.
  
    .PARAMETER  sourceVC
        Specify the Virtual Center from which it will download the custom attributes.

    .PARAMETER  destinationVC
        Specify the Virtual Center to which it will apply the custom attribues.
          
    .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.

    .EXAMPLE
        PS C:\> Clone-CustomAttributes -sourceVC 'VC01.biz' -destinationVC 'vc02.biz' -TargetType 'VirtualMachine'
        Will create the custom attribute definitions for type VirtualMachine which belong to VC01 on VC02.

    .NOTES
        NAME:  Export-CustomAttributes
          
        AUTHOR: Grzegorz Kulikowski
          
        NOT WORKING ? #powercli @ irc.freenode.net 
          
    .LINK
  
https://psvmware.wordpress.com
  
#>
param(
[Parameter(Mandatory=$true)]$sourceVC, 
[Parameter(Mandatory=$true)]$destinationVC, 
[Parameter(Mandatory=$true)][VMware.VimAutomation.ViCore.Types.V1.AnnotationManagement.CustomAttributeTargetType]$targetType

) 
    $SourceCustomAttr = @{}
    #I have special filter not to much some names, as i do not wan't them on my new VC Instance.
    Get-CustomAttribute -Server $sourceVC -TargetType $targetType  | ? {$_.Name -notmatch "PnC|vrm|FA.|Xd"} | % { $SourceCustomAttr+=@{$_.Key=$_.Name}}
    Foreach ($CustomAttribute in $SourceCustomAttr.GetEnumerator()) 
    {
        New-CustomAttribute -Name $CustomAttribute.Value -TargetType $targetType -Server $destinationVC
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s