-
1. Re: How to retrieve the value from a custom field
natxoasenjo Apr 29, 2015 6:37 AM (in response to Trondesson)This works for me:
my $vm_view = Vim::find_entity_views( view_type => 'VirtualMachine', filter => { 'name' => name of your vm' }, properties => ['summary.customValue', ], ); for my $value ( @$vm_view ) { my $cv_ref = $value->{'summary.customValue'}; for ( @$cv_ref ) { print "key: ", $_->key, "\tvalue: " , $_->value, "\n"; } }
-
2. Re: How to retrieve the value from a custom field
Trondesson Apr 29, 2015 11:00 PM (in response to natxoasenjo)Good Morning.
I tried to adopt your code for my script but it didn't work.
my $vms = Vim::find_entity_views(view_type => 'VirtualMachine', filter => { 'guest.guestState' => 'running' }, properties => ['summary.customValue',], ); for my $value (@$vms) { my $cv_ref = $value->{'summary.customValue'}; for (@$cv_ref) { print "key: ",$_->key, "\tvalue: ",$_->value, "\n"; } }
On my vCenter I created the custom field "email" and put for one VM the value "foo@bar.com" in. When I run the script I didn't get any output. Could you explain where I'm wrong?
-
3. Re: How to retrieve the value from a custom field
natxoasenjo Apr 29, 2015 11:40 PM (in response to Trondesson)nearly there.
In the for loop you need to skip those vm's without custom fields:
for my $vm ( sort { $a->name cmp $b->name } @$vm_view ) { my $cv_ref = $vm->{'summary.customValue'}; next unless defined ( $vm->{'summary.customValue'} ) ; print $vm->name . "\n"; for ( @$cv_ref ) { print "key: ", $_->key, "\tvalue: ", $_->value, "\n"; } }
o, and you need to retrieve the 'name' property of the vm as well if you copy paste that :-)
-
4. Re: How to retrieve the value from a custom field
Trondesson Apr 30, 2015 12:14 AM (in response to natxoasenjo)Sorry, but I didn't get it, yet.
At first, I believe that I don't have to skip any VM's in the loop. My custom field is global an all VM's on the cluster have this field. But not all VM's have an value for this field.
natxoasenjo schrieb:
o, and you need to retrieve the 'name' property of the vm as well if you copy paste that :-)
From reading the Documentation I guess I have to do something like:
my $entity_type = Opts::get_option('VirtualMachine'); my $entity_views = Vim::find_entity_views(view_type => $entity_type); foreach my $entity_view (@$entity_views) { my $entity_name = $entity_view->name; }
Could you explain to me how to do this, please?
-
5. Re: How to retrieve the value from a custom field
natxoasenjo Apr 30, 2015 12:45 AM (in response to Trondesson)ok, I think I understand where you want to get to. If all vm's have the custom field key not not the value, then you need to skip those without a defined value. Then in the @$cv_ref loop use something like this:
for ( @$cv_ref ) {
next unless defined $_->value;
...
}
But I just checked and all the vm's here have the key and only one has a test value, and my original code works, so in fact skipping the empty values is in my case indifferent. Apparently adding a customfield to one vm adds it to all vm's, just does not fill the value field.
2nd edit: I tried with a global customfield and same thing, it keeps working.
-
6. Re: How to retrieve the value from a custom field
Trondesson Apr 30, 2015 12:45 AM (in response to natxoasenjo)Ok. Now my code looks like this:
my $vms = Vim::find_entity_views(view_type => 'VirtualMachine', properties => ['summary.customValue',], ); for my $vm (@$vms) { my $cv_ref = $vm->{'summery.customValue'}; next unless defined($vm->{'summary.customValue'}); print $vm->name . "\n"; for (@$cv_ref) { next unless defined $_->value; print "key: ",$_->key,"\tvalue: ",$_->value,"\n"; } }
For line 6 I got the following error when I tried to run my script.
Use of uninitialized value in concatenation (.) or string at ./SnapshotReminder.pl
I think I have to use something different than "name".
Thank you for your patience.
-
7. Re: How to retrieve the value from a custom field
natxoasenjo Apr 30, 2015 12:48 AM (in response to Trondesson)1 person found this helpfulno, you need to retrieve the 'name' property of the vm, you just retrieved the 'summary.customValue' . So try this:
my $vm_view = Vim::find_entity_views( view_type => 'VirtualMachine', filter => { 'guest.guestState' => 'running' }, properties => [ 'summary.customValue', 'name', ], );
-
8. Re: How to retrieve the value from a custom field
Trondesson Apr 30, 2015 1:04 AM (in response to natxoasenjo)The following code prints the Name of the VirtualMachine that has an value for the custom field, but it don't print the value as well.
my $vms = Vim::find_entity_views(view_type => 'VirtualMachine', properties => ['summary.customValue','name',], ); for my $vm (@$vms) { my $cv_ref = $vm->{'summery.customValue'}; next unless defined($vm->{'summary.customValue'}); print $vm->name . "\n"; for (@$cv_ref) { next unless defined $_->value; print "key: ",$_->key,"\tvalue: ",$_->value,"\n"; } }
-
9. Re: How to retrieve the value from a custom field
natxoasenjo Apr 30, 2015 1:18 AM (in response to Trondesson)1 person found this helpfulYou have a typo on declaring $cv_ref, you spell summary wrong.
this works for me:
my $vm_view = Vim::find_entity_views( view_type => 'VirtualMachine', filter => { 'guest.guestState' => 'running' }, properties => [ 'summary.customValue', 'name', ], ); for my $vm ( sort { $a->name cmp $b->name } @$vm_view ) { my $cv_ref = $vm->{'summary.customValue'}; next unless defined ( $vm->{'summary.customValue'} ) ; print $vm->name . "\n"; for ( @$cv_ref ) { #next unless defined $_->value; print "key: ", $_->key, "\tvalue: ", $_->value, "\n"; } }
$perl /tmp/kk
vmname
key: 201 value: bladibla
key: 203 value: globalbladibla
-
10. Re: How to retrieve the value from a custom field
Trondesson Apr 30, 2015 1:26 AM (in response to natxoasenjo)For gods sake, what a stupid typo.
Thank you for your help.
Now I get with this code:
my $vms = Vim::find_entity_views(view_type => 'VirtualMachine', properties => ['summary.customValue','name',], ); for my $vm (@$vms) { my $cv_ref = $vm->{'summary.customValue'}; next unless defined($vm->{'summary.customValue'}); print $vm->name . "\n"; for (@$cv_ref) { next unless defined $_->value; print "key: ",$_->key,"\tvalue: ",$_->value,"\n"; } }
This output:
./SnapshotReminder.pl --url https://<fqdn>/sdk/webService Enter username: ********* Enter password: UHRZ-Ubuntu-E01 key: 2006 value: joerg.kastning@uni-bielefeld.de
In line number 5, is 2006 a unique value for the custom field? Is it possible to get the current name of the custom field instead of the id?
Thanks a lot for your help and your patience with me.
-
11. Re: How to retrieve the value from a custom field
natxoasenjo Apr 30, 2015 1:37 AM (in response to Trondesson)sure, in this discussion https://communities.vmware.com/message/1512863#1512863 you can see how it's done
-
12. Re: How to retrieve the value from a custom field
LukaszWarchol Jun 1, 2017 10:59 AM (in response to Trondesson)Hi,
I have similar question. I would like to get information with specific settings for all VMs and here I am able to do it, but I would like also get information about specific Custom Attribute and here I am failing. Could you please help me?
My current part of the code looks like following:
$VMInfo = "" | Select VMName,NICCount,IPAddress,MacAddress,NICType,NetworkName,GuestRunningOS,PowerState,ToolsVersion,ToolsStatus,ToolsRunningStatus,HWLevel,VMHost,CustomValue
$VMInfo.VMName = $vmview.Name
$VMInfo.NICCount = $vmview.Guest.Net.Count
$VMInfo.IPAddress = [String]$getvm.Guest.IPAddress
$VMInfo.MacAddress = [String]$nicmac
$VMInfo.NICType = [String]$nictype
$VMInfo.NetworkName = [String]$nicname
$VMInfo.GuestRunningOS = $vmview.Guest.GuestFullname
$VMInfo.PowerState = $getvm.PowerState
$VMInfo.ToolsVersion = $vmview.Guest.ToolsVersion
$VMInfo.ToolsStatus = $vmview.Guest.ToolsStatus
$VMInfo.ToolsRunningStatus = $vmview.Guest.ToolsRunningStatus
$VMInfo.HWLevel = $vmview.Config.Version
$VMInfo.VMHost = $getvm.VMHost
$VMInfo.CustomValue = $vmview.CustomValue.Value - > this one is not working
In VM Annotation in vCenter I have CustomAttibute named SLA. And entry this field I want to get for all VMs: Please see output from particular VM:
PowerCLI C:\> $vm.AvailableField.Name
App_Category
Building
Crash
Function
Hardening Settings
Operational_Usage
SEC_Info
SLA
Service Contract
Status
StorMagicVSA
TSM_VE
TSM_VE_Policy
TSM_VE_Scope
Veeam-Status
com.vmware.vdp2.is-protected
com.vmware.vdp2.protected-by
com.vmware.vdr.is-protected
com.vmware.vdr.protected-by
vhw-name
vhw-responsible
PowerCLI C:\> $vm.AvailableField.Key
201
101
1001
202
601
501
401
502
203
503
901
801
802
803
903
904
905
906
907
204
205
PowerCLI C:\> $vm.CustomValue.Value
Web/Application Platform Generic
Application Other
internal
Test
Best Effort (DE)
in operation
PowerCLI C:\> $vm.CustomValue.Key
201
202
401
501
502
503
So interesting part is for me Key 502, but not sure how to correlate CustomValue, Key and AvailableFields to get output from SLA field.
I appreciate your help. Thx.