Get-CustomField.ps1 Script
# TITLE: Get-CustomField.ps1
# AUTHOR: Hal Rottenberg <hal@halr9000.com>
# PURPOSE: Retrieve custom field data from any VMware Infrastructure "managed entity"
# (i.e. VM, VMHost, Cluster, etc.).
# USAGE: Use a Get cmdlet such as Get-VM or Get-Inventory and pipe to this script.
# Optionally, specify the Name parameter to modify items retrieved. Wildcards
# are permitted.
# INPUT: By pipeline only, any managed entity.
# OUTPUT: A custom object containing the fields Name, Value, InputObject, and Type
# EXAMPLE:
# PS> Get-Inventory | Get-CustomField.PS1 -name Service*
#
# Name Value InputObject Type
# ---- ----- ----------- ----
# ServiceLevel Gold 192.168.0.51 VMHostImpl
param (
$Include # Search by this custom field name (aka key)
)
Process {
# If the object on the pipeline does not have a CustomFields property, ignore it
if ( !$_.CustomFields ) { continue }
# Save this for later inclusion into the output custom object.
$InputObject = $_
# An entity can be one of several types, it may be useful to know which one.
$Type = $InputObject.GetType().Name
$_.CustomFields | ForEach-Object {
# If Name was provided, filter out the non-matches
if ( ( $Name -and ( $_.Key -like $Name )) -or !$Name ) {
# Build custom object to hold output.
$Process = New-Object PSObject
$Process | Add-Member -MemberType NoteProperty -Name Name -Value $_.Key -PassThru |
Add-Member -MemberType NoteProperty -Name Value $_.Value -PassThru |
Add-Member -MemberType NoteProperty -Name InputObject -Value $InputObject -PassThru |
Add-Member -MemberType NoteProperty -Name Type -Value $Type
Write-Output $Process
}
}
}
Comments