Hello @all.
In my vCenter I've created a custom field "email". The value for this field is set to the e-mail address of the operator a VM belongs to.
I like to retrieve these values in a perl script. But I only found a method to set values for custom fields. For example:
$CustomFieldsManager -> SetField(entity => $_, key=> $emailKey, value => "foo@bar.com");
What do I have to do to retrieve the value for the key $emailKey? Could somebody provide a quick and dirty code-snippet, please?
Best Regards
Trondesson
sure, in this discussion https://communities.vmware.com/message/1512863#1512863 you can see how it's done
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";
}
}
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?
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 🙂
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?
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.
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.
no, 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', ],
);
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";
}
}
You 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
For gods sake, what a stupid typo. :smileyblush:
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.
sure, in this discussion https://communities.vmware.com/message/1512863#1512863 you can see how it's done
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.