VMware Cloud Community
Manolax
Contributor
Contributor
Jump to solution

Powershell Script - Annotations to csv

Hello everybody!

I need a liitle powershell script, which is doing the following things.

Get all VM's and when "CustomAttribute" is set to 1, write the Values like CPU, RAM, .... into a csv-File.

I tried it like this:

$VMExport = Get-VM | Get-Annotation -CustomAttribute Export | Where-Object {$_.Value -eq 1 }

But now I have only the Value 1 in the Variable "$VMExport" and cannot use it for further things.

Can anybody help me with that?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The CustomFields property is a hash table, so you can access the values by using the Key as index.

You will have to use a calculated property for that

$VMExport = Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | 
Select Name,ProvisionedSpaceGB,MemoryMB,NumCpu,Description,
   @{N
="Custom Field";E={$_.CustomFields["CustomFieldName"]}}

This retrieves the value of the Custom Field called "CustomFieldName" and shows it under the "Custom Field" property.


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Use the Get-Annotation cmdlet inside the where-clause.

Like this

$VMExport = Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | Select Name

Update the Select cmdlet at the end to include the properties of the VM that you want


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

Manolax
Contributor
Contributor
Jump to solution

Thanks for your help!

It was very helpful, but now I'm stuck again.

I don't know how to get all the attributes which I need to have in the csv-file

I need the following things:

Name,Provisioned Space,Memory Size,CPU Count,Notes, and some CustomAttributes
But in the select cmdlet I can only use the properties of the "Get-Annotation" cmdlet.
Thanks in advance for your help!
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You get VirtualMachineImpl objects back with my line.

Try

$VMExport = Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | 
Select Name,ProvisionedSpaceGB,MemoryMB,NumCpu,Description

It should produce most of the data you want.

If you want to see what is returned or what properties are available, do a Get-Member

Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | 
Get-Member


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

0 Kudos
Manolax
Contributor
Contributor
Jump to solution

Yes, you're right.

But how do I get my CustomAttributes within this line?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The CustomFields property is a hash table, so you can access the values by using the Key as index.

You will have to use a calculated property for that

$VMExport = Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | 
Select Name,ProvisionedSpaceGB,MemoryMB,NumCpu,Description,
   @{N
="Custom Field";E={$_.CustomFields["CustomFieldName"]}}

This retrieves the value of the Custom Field called "CustomFieldName" and shows it under the "Custom Field" property.


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

0 Kudos