VMware Cloud Community
charfisr
Contributor
Contributor
Jump to solution

Retrieving Multiple Annotations in Script

We have previously had a script in place for some time that pulls our inventory plus a single annotation field. We run extracts weekly for sharing the state of our datacenter, but now we are adding additional annotation fields. As much as I've looked online I can't seem to properly add multiple annotation fields so that they show up as columns in the extracted CSV.

Previously we only looked to get the VM-Owner annotation now we are looking for things like Application, LastConfirmed, etc etc.

Here is our previous script,

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer -Server XXXXXX -User XXXXXXX -Password XXXXXX

$targetCA = 'VM-Owner'

$report = @()

foreach($vm in Get-VM){

    $row = [ordered]@{

        VM = $vm.Name

Hostname = $vm.Guest.Hostname

IPAddress = $vm.guest.IPAddress[0]

CPU = $vm.NumCpu

RAM = ($vm.MemoryGB * 1024)

OperatingSystem = $vm.Guest.OSFullName

PowerState = $vm.PowerState

   

    }

    Get-Annotation -Entity $vm | where{$targetCA -contains $_.Name} | %{

        $row.Add($_.Name,$_.Value)

    }

    $report += New-Object PSOBject -Property $row

  

}

$report | Export-Csv 'c:\temp\scripts\export\vSphere_Export.csv' -NoTypeInformation -UseCulture

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.
Specify all the Custom Fields you want to include in the $targetCA variable.

$targetCA = 'VM-Owner','AnotherField','YetAnotherField'

$report = @()

foreach($vm in Get-VM){

    $row = [ordered]@{

        VM = $vm.Name

        Hostname = $vm.Guest.Hostname

        IPAddress = $vm.guest.IPAddress[0]

        CPU = $vm.NumCpu

        RAM = ($vm.MemoryGB * 1024)

        OperatingSystem = $vm.Guest.OSFullName

        PowerState = $vm.PowerState

    }

    $targetCA | %{

        $row.Add($_,(Get-Annotation -Entity $vm -CustomAttribute $_ | select -ExpandProperty Value))

    }

    $report += New-Object PSOBject -Property $row

}

$report | Export-Csv 'c:\temp\scripts\export\vSphere_Export.csv' -NoTypeInformation -UseCulture


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this.
Specify all the Custom Fields you want to include in the $targetCA variable.

$targetCA = 'VM-Owner','AnotherField','YetAnotherField'

$report = @()

foreach($vm in Get-VM){

    $row = [ordered]@{

        VM = $vm.Name

        Hostname = $vm.Guest.Hostname

        IPAddress = $vm.guest.IPAddress[0]

        CPU = $vm.NumCpu

        RAM = ($vm.MemoryGB * 1024)

        OperatingSystem = $vm.Guest.OSFullName

        PowerState = $vm.PowerState

    }

    $targetCA | %{

        $row.Add($_,(Get-Annotation -Entity $vm -CustomAttribute $_ | select -ExpandProperty Value))

    }

    $report += New-Object PSOBject -Property $row

}

$report | Export-Csv 'c:\temp\scripts\export\vSphere_Export.csv' -NoTypeInformation -UseCulture


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

Reply
0 Kudos
charfisr
Contributor
Contributor
Jump to solution

Thank you that worked like a charm

Reply
0 Kudos