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
} 

Advertisements

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

Searching for VMs with active questions.

for exmaple:
$vms = get-view -viewtype virtualmachine
$vms | ?{$_.runtime.question} | select name

In order to answer
assuming there is 1 question:
($vms | ?{$_.runtime.question}) | %{$_.AnswerVM($_.runtime.question.Id,’0′)}
Where 0 is for Retry because :
PowerCLI C:\> ($vms | ?{$_.runtime.question})[0].runtime.question.Choice.ChoiceInfo

Key : 0
Label : Retry
Summary : Retry

Key : 1
Label : Cancel
Summary : Cancel

So we can answer the question now with:

($vms | ?{$_.runtime.question}) | %{$_.AnswerVM($_.runtime.question.Id,’0′)}

Issues with using the Search option in vsphere client

New-Item -Path HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\KeyExchangeAlgorithms -Name Diffie-Hellman –Force
New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\KeyExchangeAlgorithms\Diffie-Hellman -Value 0x00000200 -PropertyType DWord -Name ‘ClientMinKeyBitLength’

Get-VMHostPnicCDP for check CDP/LLDP information for ESXi hostsystem physical nic.

Today i had to check some data in CDP, hence i wrote this script. I think it’s well documented, so it does not need description from my side. One thing to note, is that i have not checked if it prints out properly lldp information if you are using -lldp switch, i just assumed it’s going to be ok, looking at the vsphere documentation for this lldpinfo object.

<#
	.SYNOPSIS
		Gets CDP/LLDP information for ESXi physical nic.

	.DESCRIPTION
		Gets CDP/LLDP information for ESXi physical nic.

	.PARAMETER  VMHost
		VMHost Can be piped to this function, it can VMHost object from get-vmhost or it get be HostSystme object from Get-View.

	.PARAMETER  pnic
		This is the physical nic adapter name, for example: vmnic0 , it supports array as well: vmnic0,vmnic2,vmnicN.

	.PARAMETER  lldp
		If paramenter -lldp was give, it will output content of lldpinfo from QueryNetworkHint method.

	.EXAMPLE
		PS C:\> Get-VMHostPnicCDP   -pnic vmnic0 -VMHost 'esxi1.local.lan'
		Without passing through pipeline it acceppts 1 vmhost only.

	.EXAMPLE
		PS C:\> Get-VMHostPnicCDP  -VMHost 'esxi1.local.lan'
		When -pnic parameter is skipped, CDP will be retrieved for all pnics in ESXi.

	.EXAMPLE
		PS C:\> 'esxi1.local.lan','esxi2' | Get-VMHostPnicCDP   -pnic vmnic0
		You can pass multiple VMHost names through pipeline

	.EXAMPLE
		PS C:\> get-vmhost -name 'esxi1.local.lan' | Get-VMHostPnicCDP -pnic vmnic1
		You can pass object directly from get-vmhost

	.EXAMPLE
		PS C:\>  get-view -viewtype hostsystem -filter @{'name'='esxi1.local.lan'} | Get-VMHostPnicCDP -pnic vmnic1
		You can pass object directly from get-view

	.NOTES
		NAME: Get-VMHostPnicCDP
		AUTHOR: Grzegorz Kulikowski
		LASTEDIT : 09/07/2015

	.LINK
		https://psvmware.wordpress.com

#>
function Get-VMHostPnicCDP {
	[CmdletBinding()]
	param(
		[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
		[ValidateNotNullOrEmpty()]
		[System.Object]
		$VMHost,
		[Parameter(Position=1)]
		[System.String[]]
		$pnic,
		[switch]$lldp
	)
	begin {
		try
		{
			if (!$global:defaultVIserver) { 'Not connected to VirtualCenter';break }
		}
		catch
		{
		}
	}
	process {
		try {
			switch (($VMHost.GetType()).FullName)
			{
				'VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl'
				{
					$NetworkSystemConfigManager = get-view -id $vmhost.ExtensionData.ConfigManager.NetworkSystem
				}
				'VMware.Vim.HostSystem'
				{
					$NetworkSystemConfigManager = Get-View -id $vmhost.ConfigManager.NetworkSystem
				}
				'System.String'
				{
					$VMHost = Get-View -viewtype HostSystem -Property Name, ConfigManager.NetworkSystem -Filter @{ 'name' = $VMHost }
					$NetworkSystemConfigManager = Get-View -id $vmhost.ConfigManager.NetworkSystem
				}
			}
			if (!$pnic) { [string[]]$pnic = $NetworkSystemConfigManager.NetworkConfig.Pnic | %{ $_.device } }
			foreach ($EsxiPnic in $pnic)
			{
				if ($lldp) { $NetworkSystemConfigManager.QueryNetworkHint($esxipnic) | %{ $_.lldpinfo } | Select-Object *, @{ n = 'Esxi'; e = { $VMHost.name } }, @{ n = 'ESXiPnic'; e = { $EsxiPnic } } }
				else
				{
					$NetworkSystemConfigManager.QueryNetworkHint($esxipnic) | %{ $_.ConnectedSwitchPort } | Select-Object *, @{ n = 'Esxi'; e = { $VMHost.name } }, @{ n = 'ESXiPnic'; e = { $EsxiPnic } }
				}
			}
		}
		catch [VMware.Vim.VimException]
		{
			$_.Exception
			'Maybe the pnic you are reffering to, does not exist ?'
		}
	}
	end {
		try {
		}
		catch {
		}
	}
}

Copy n paste, and enjoy 😉

cdp