VMware Cloud Community
itzvinuu
Contributor
Contributor

Get custom annotation in csv format

Hi,

I am trying to fetch custom annotation details using following powershel command and the sample o/p is shown below.

Get-VM | `
  ForEach-Object {
    $VM = $_.Name
    $_ | Get-Annotation -Customattribute "Domain","HDD" | export-csv C:\test.csv

AnnotatedEntityIdAnnotatedEntityNameValueUid
VirtualMachine-vm-66038VM1Domainmydomain.com/VIServer=@162.168.1.1:443/VirtualMachine=VirtualMachine-vm-66038/AnnotationValue=Domain/
VirtualMachine-vm-66038VM1HDD76/VIServer=@10.168.1.1:443/VirtualMachine=VirtualMachine-vm-66038/AnnotationValue=HDD/

Requirement:-

My requirement is to get these details in a format shown below

AnnotatedEntityDomainHdd
VMmydomain.com76

Is this possible to do? if so,could you please help me to get the result in this format.

Thanks in advance

----------

Vinod

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

Sure, use the Select cmdlet to extract what you want.

Something like this

Get-VM | Select Name,
    @{N="Domain";E={(Get-Annotation -Entity $_ -CustomAttribute "Domain").Value}},
    @{N="HDD";E={(Get-Annotation -Entity $_ -CustomAttribute "Hdd").Value}} | `
Export-Csv "C:\Test.csv" -NoTypeInformation -UseCulture


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

itzvinuu
Contributor
Contributor

Thanks a lot LucD,

This is what i was looking for ...

one more query,  i have two annotattions "Start date" and "End date" can i get no of months in the next column in thesame  csv out put.

---------

Vinod

Reply
0 Kudos
LucD
Leadership
Leadership

Try it like this.

I used the CustomFields property instead of the individual calls to the Get-Annotation cmdlet.

Get-VM | Select Name,
    @{N="Domain";E={$_.CustomFields["Domain"]}},
    @{N="HDD";E={$_.CustomFields["Hdd"]}},
        @{N="Months";E={
            [int](
            ([datetime]$_.CustomFields["EndDate"] - `
            [
datetime]$_.CustomFields["StartDate"]).Days/31)}} | `
Export-Csv "C:\test.csv" -NoTypeInformation -UseCulture


A lot depends on the format you used to store the Start- and End-date.

When the 2 dates are read, the scrip uses the substraction to calculate the difference.

Note that I had to divide the number of days by 31 to get the months.


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

Reply
0 Kudos