VMware Cloud Community
patrick_hagen
Contributor
Contributor
Jump to solution

List all Vm's from a specific customer

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do

Get-VM | where{$_.CustomFields['Customer Environment'] -eq 'test'}


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

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
patrick_hagen
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do

Get-VM | where{$_.CustomFields['Customer Environment'] -eq 'test'}


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

Reply
0 Kudos