VMware Cloud Community
RKB79
Contributor
Contributor

Retrieving the values of Custom Attributes

Hi Guys

How can I retrieve the values of custom attributes for a VM?

below command gives me the name and target type but not the values

PS C:\> Get-CustomAttribute

Key Name TargetType
--- ---- ----------
1001 Backup Status VirtualMachine
2 Hardened VirtualMachine
101 Last Backup VirtualMachine
601 SRM-com.vmware.vc...

Can you please help me to get these custom attributes values??

 

I thank you in advance for your help.

Regards

Raj

0 Kudos
4 Replies
LucD
Leadership
Leadership

Use the Get-Annotation cmdlet.

$caName = 'MyCA'

$ca = Get-CustomAttribute -Name $caName
Get-VM | Get-Annotation -CustomAttribute $ca


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

0 Kudos
RKB79
Contributor
Contributor

Hi LucD,

Thanks for pointing me into the right direction.. I have done this:

$Values = read-host 'Enter Attribute'

$VM = read-host 'Enter Virtual Machine'

foreach ($value in  (read-host 'Enter Attribute') ){Get-VM (read-host 'Enter Virtual Machine') | Get-Annotation -CustomAttribute (Get-CustomAttribute -Name $value)}

When I run this it ask for Attribute value and VM name like below

Enter Attribute: Hardened

Enter Virtual Machine: VM Name

AnnotatedEntity Name                 Value

--------------- ----                 -----

VM Name  Hardened             Profile 2 Applied 02/06/2021

 

But when I enter multiple attributes it don't like it.

Is there anyway I can use this for all the attributes I need rather than doing one by one? Hardened, Backup Status, Last Backup, SRM-com.vmware.vcDr:::protected

Thanks in advance for your help!!!
Raj

0 Kudos
RKB79
Contributor
Contributor

little more modification..

$VM = 'VM Name'
foreach ($value in @('Hardened','Backup Status','Last Backup','SRM-com.vmware.vcDr:::protected') ){Get-VM $VM | Get-Annotation -CustomAttribute (Get-CustomAttribute -Name $value)}

 

AnnotatedEntity Name                 Value

--------------- ----                 -----

VM Name Hardened             Profile 2 Applied 02/06/2021

VM Name Backup Status        Backup Job ID [116255351]  Client: [vCentre-In-...

VM Name Last Backup          8/4/2021 11:22:03 PM

VM Name SRM-com.vmware.vc... vm-1418908

 

Any advise on this and how can I more improve this will be greatly appriciated.

thanks,

0 Kudos
LucD
Leadership
Leadership

You could do something like this

$vmName = Read-Host 'Enter Virtual Machine'
$caNames = Read-Host 'Enter Attribute (comma separated'

Get-VM -Name $vmName |
Get-Annotation -CustomAttribute (Get-CustomAttribute -Name $caNames.Split(',')) |
Select-Object @{N='VM';E={$_.AnnotatedEntity.Name}},Name,Value
 


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

0 Kudos