@VMware,
I see New, Set, and Remove, but no Get. Was this intentional?
Hal Rottenberg
Co-Host, PowerScripting Podcast (http://powerscripting.net)
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)
Fixed.
I'd still like to see that cmdlet, VMware!
Hal Rottenberg
Co-Host, PowerScripting Podcast (http://powerscripting.net)
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 ?!
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
}
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
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
Cmdlet Get-customefield{
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
}
}
}
}
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.
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
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 }
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
hm, with that I get all Customfields...
Edit: looks like it's $Name thats missing... how do I get that into the filter?
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
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]