VMware Cloud Community
halr9000
Commander
Commander

Why no Get-CustomField cmdlet?

@VMware,

I see New, Set, and Remove, but no Get. Was this intentional?

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Tags (1)
Reply
0 Kudos
12 Replies
halr9000
Commander
Commander

while on the topic...

The customfield cmdlets could use an ID parameter, and ability to pass entity (name) as a string.

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
halr9000
Commander
Commander

Fixed. Smiley Wink

I'd still like to see that cmdlet, VMware!

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
alanrenouf
VMware Employee
VMware Employee

Couldnt Agree more, my first instinct when seeing the new set and remove cmdlets was to try and use the get-customfield command.

Perhaps for the next beta ?!

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos
admin
Immortal
Immortal

Indeed currently there is no Get-CustomField cmdlet.

However each VI object has CustomFileds property, through which you can get all custom fields for a particular object

Here is an example:

$v = get-vm MyVM

$v | fc

class VirtualMachineImpl

{

PowerState = PoweredOn

Description =

Guest =

class VMGuestImpl

{

OSFullName = Microsoft Windows Server 2003, Enterprise Edition (32-bit)

............

CustomFields =

[

class 0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

{

Key = CustomField1

Value = 1

}

class 0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

{

Key = CustomField2

Value = 2

}

...

]

Id = VirtualMachine-vm-3122

Name = VC40_1

}

Reply
0 Kudos
houghtp
Contributor
Contributor

Hal,

i'm trying to use your get-customefield cmdlet to work but having no joy.

i've saved the script and then if i try to do get-vm | get-customfield.ps1 -Name backup*

i get:

The term 'get-customfield.ps1' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again

(i am in the directory this script is saved.)

so i added it to the toolkit extensions. as a cmdlet which i load in my powershell profile

  1. TITLE: Get-CustomField.ps1

  2. AUTHOR: Hal Rottenberg <hal@halr9000.com>

  3. PURPOSE: Retrieve custom field data from any VMware Infrastructure "managed entity"

  4. (i.e. VM, VMHost, Cluster, etc.).

  5. 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.

  1. INPUT: By pipeline only, any managed entity.

  2. OUTPUT: A custom object containing the fields Name, Value, InputObject, and Type

  3. EXAMPLE:

  4. PS> Get-Inventory | Get-CustomField.PS1 -name Service*

  5. Name Value InputObject Type

  6. ---- ----- ----------- ----

  7. ServiceLevel Gold 192.168.0.51 VMHostImpl

Cmdlet Get-customefield{

param (

$Include # Search by this custom field name (aka key)

)

Process {

  1. If the object on the pipeline does not have a CustomFields property, ignore it

if ( !$_.CustomFields ) { continue }

  1. Save this for later inclusion into the output custom object.

$InputObject = $_

  1. An entity can be one of several types, it may be useful to know which one.

$Type = $InputObject.GetType().Name

$_.CustomFields | ForEach-Object {

  1. If Name was provided, filter out the non-matches

if ( ( $Name -and ( $_.Key -like $Name )) -or !$Name ) {

  1. 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

}

}

}

}

now when i pass a value to it via the pipline , powershell plus recognises it as a cmdlet via autocomplete drop down list, but i get an error:

PS C:\Documents and Settings\camph009> Get-VM | Get-customefield -Name backup*

Get-customefield : A parameter cannot be found that matches parameter name 'Name'.

At line:1 char:32

+ Get-VM | Get-customefield -Name <<<< backup*

where am i going wrong?

TIA.

Reply
0 Kudos
halr9000
Commander
Commander

The reason it didn't work the first time is because you can't execute a script in the current working directory without prepending it with ".\". This is a security-related feature (of arguable usefulness) to prevent you from accidentally executing hostile code because your CWD had changed without your notice.






[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
houghtp
Contributor
Contributor

I've tried that by filtering for a customefield we have called Application, but I get a list of all customfields for all VM's

i've attached the ouput

Reply
0 Kudos
LarsOeschey
Contributor
Contributor

I'm trying to use this as a function (to prevent having to deploy several script files later), but can't get it to work

param (
	$Include # Search by this custom field name (aka key)
)

function Get-CustomField($customname)
{
	# 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
		}
	}
}

Connect-VIServer 10.10.10.10 -User vadmin -Password vadmin
$vms = Get-VM | select name
Foreach ($vm in $vms) {
	Get-CustomField Intervall
	}

Reply
0 Kudos
LucD
Leadership
Leadership

If you are trying to use Hal's function as a filter you could do it like this

filter Get-CustomField($name)
{
	# 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
		}
	}
}

Connect-VIServer 10.10.10.10 -User vadmin -Password vadmin
$vms = Get-VM
foreach ($vm in $vms) {
	$vm | Get-CustomField Intervall
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LarsOeschey
Contributor
Contributor

hm, with that I get all Customfields...

Edit: looks like it's $Name thats missing... how do I get that into the filter?

Reply
0 Kudos
LucD
Leadership
Leadership

You can specify the name of a custom attribute on the 2nd to last line to get a specific custom attribute.

filter Get-CustomField($name)
{
	# 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
		}
	}
}

Connect-VIServer 10.10.10.10 -User vadmin -Password vadmin
$vms = Get-VM
foreach ($vm in $vms) {
	$vm | Get-CustomField MyCustomField
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
admin
Immortal
Immortal

Everyone, please use the Get-Annotation and Set-Annotation cmdlets instead of this.

=====

Carter Shanklin

Read the PowerCLI Blog
[Follow me on Twitter|http://twitter.com/cshanklin]

Reply
0 Kudos