Hello,
We would like to get a list of VM's that have a specific name in the customer environment customfields.
So let's say list only the VM's for which CustomField "Customer Environment" is "x".
Example:
$vmlijst[0].customfields.item( "Customer Environment") gives the correct name.
but $vmlijst.customfields.item( "Customer Environment") doesn't work.
$vmlijst.customfields.item( "Customer Environment")
Exception getting "Item": "Cannot convert argument "index", with value: "Customer Environment", for "get_Item" to type "System.Int32": "Cannot convert value "Customer Environment" to type "System.Int32"
. Error: "Input string was not in a correct format."""
At line:1 char:1
+ $vmlijst.customfields.item( "Customer Environment")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], GetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenGetting
How can we accomplish this?
Thanks in advance
Kind Regards,
Patrick Hagen
You could do
Get-VM | where{$_.CustomFields['Customer Environment'] -eq 'test'}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The problem is that your $vmLijst is an array, in that case the Index is interpreted as an index, an integer - hence the error message, into that array.
You can only use the name (string) of the CA as an index for an individual VM.
You could do
$vmlijst.Foreach{$_.CustomFields.Item('Customer Environment')}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
hi Luc,
And how do I get all the VM's from customer "x" then?
For example:
I have 2000 VM's and only want a list of VM's from the customer "test"
With the code you provide I get a list of all the customer environments. Bu what I need is only the VM's that have a Customer Environment with the name "test"
Hope that this makes things clear
Kind Regards,
Patrick
You could do
Get-VM | where{$_.CustomFields['Customer Environment'] -eq 'test'}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
