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
    }
}

Export-CustomAttributes using PowerCLI

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.

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
  
https://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
} 

Get-ESXiMemoryDimms

I had to get some info about host memory dimms layout, so i wrote this one.
It is utilizing CIM_PhysicalMemory. Not sure if i haven’t written it before, but i couldn’t find it on my disk so…
Here it is.

Function Get-ESXiMemoryDimms {
<#
    .SYNOPSIS
        Returns memory dimms configuration for esxi
  
    .DESCRIPTION
        This function utilizes wsman / cim in order to download configuration of memory dims in ESXi.
  
    .PARAMETER  esxi
        Specify esxi host for which you want to get the report
          
    .PARAMETER  rootpw
        Password for user root inside esxi
 
    .EXAMPLE
        PS C:\> Get-ESXiMemoryDimms -Esxi 'Esxi048.domain.local' -rootpw 'password.123' | ft *
        BankLabel               Manufacturer Description SizeGB MemoryType MemoryDef
        ---------               ------------ ----------- ------ ---------- ---------
        P0_Node0_Channel0_Dimm0 Samsung      P1-DIMMA1       16 24         DDR3
        P0_Node0_Channel0_Dimm1 Samsung      P1-DIMMA2       16 24         DDR3
        P0_Node0_Channel1_Dimm0 Samsung      P1-DIMMB1       16 24         DDR3
        P0_Node0_Channel1_Dimm1 Samsung      P1-DIMMB2       16 24         DDR3
        P0_Node0_Channel2_Dimm0 Samsung      P1-DIMMC1       16 24         DDR3
        P1_Node1_Channel2_Dimm0 Samsung      P2-DIMMG1       16 24         DDR3
        P1_Node1_Channel2_Dimm1 Samsung      P2-DIMMG2       16 24         DDR3
        P1_Node1_Channel3_Dimm0 Samsung      P2-DIMMH1       16 24         DDR3
        P1_Node1_Channel3_Dimm1 Samsung      P2-DIMMH2       16 24         DDR3
                                Winbond                  ...625 11         Flash
         

    .NOTES
        NAME:  Get-ESXiMemoryDimms
          
        AUTHOR: Grzegorz Kulikowski
          
        NOT WORKING ? #powercli @ irc.freenode.net 
          
    .LINK
  
https://psvmware.wordpress.com
  
#>
param([String]$Esxi,[string]$rootpw)

#MemoryTypes from: https://msdn.microsoft.com/en-us/library/aa394347(v=vs.85).aspx
$MemHash = @{
'0'='Unknown';
'1'='Other';
'2'='DRAM';
'3'='Synchronous DRAM';
'4'='Cache DRAM';
'5'='EDO';
'6'='EDRAM';
'7'='VRAM';
'8'='SRAM';
'9'='RAM';
'10'='ROM';
'11'='Flash';
'12'='EEPROM';
'13'='FEPROM';
'14'='EPROM';
'15'='CDRAM';
'16'='3DRAM';
'17'='SDRAM';
'18'='SGRAM';
'19'='RDRAM';
'20'='DDR';
'21'='DDR2';
'22'='DDR2 FB-DIMM';
'23'='DDR2 FB-DIMM';
'24'='DDR3';
'25'='FBD2'
}
$pwd = ConvertTo-SecureString $rootpw -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential (“root”, $pwd)
$CIMOpt = New-CimSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck -Encoding Utf8 –UseSsl
$Session = New-CimSession -Authentication Basic -Credential $cred -ComputerName $esxi -port 443 -SessionOption $CIMOpt
Get-CimInstance -CimSession $Session CIM_PhysicalMemory | select BankLabel, Manufacturer, Description , @{n='SizeGB';e={$_.Capacity/1GB}}, MemoryType, @{n='MemoryDef';e={$MemHash[$_.MemoryType]}}
}