VMware Cloud Community
Stampede
Contributor
Contributor
Jump to solution

Issue Pulling Down Annotations

I have a script I've been patching together to export a full run down on the state of my vCenter server and all it's moving parts. For some reason I can't seem to find how to add the annotations to my script. Every way I've tried results in a blank field.

Here's my script:

$regIPv4 = '(?:(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)\.){3}(?:1\d\d|2[0-5][0-5]|2[0-4]\d|0?[1-9]\d|0?0?\d)'

Get-VM | Select Name, @{N='HostName';E={$_.Guest.HostName}}, @{N="IP addr";E={[string]::Join(',',($_.Guest.IPAddress | where{$_ -match $regIPv4}))}}, NumCpu, MemoryGB, Notes, PowerState | Export-Csv -path "c:\temp\vCenter.csv" -NoTypeInformation

The one annotation script  I got working exports everything in a separate line. So there are like 5 different annotation fields for each VM, and each one gets it's own line. I'm trying to get them all to populate in a single line per VM, and trying to integrate this code to my existing script.

Get-VM | Get-Annotation | Select @{N="VM";E={$_.AnnotatedEntity.Name}},Name,Value | Export-Csv -path "c:\temp\Annotate.csv" -NoTypeInformation -UseCulture

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$targetCA = 'Application','CERT Location','Database','Prod Location'

$report = @()

foreach($vm in Get-VM){

    $row = [ordered]@{

        VM = $vm.Name

    }

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

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

    }

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

}

$report | Export-Csv 'c:\temp\vAnnotations.csv' -NoTypeInformation -UseCulture


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

View solution in original post

9 Replies
LucD
Leadership
Leadership
Jump to solution

It would be easier if you first determine which Custom Attributes you want to include in the report.

You can get these with the Get-CustomAttribute cmdlet (use the TargetType VirtualMachine).

Once you have your selection of custom attributes, you can mention them on the Get-Annotation cmdlet on the CustomAttribute parameter.

I'm not sure I understand how you want the report to look.

Could you perhaps provide a mockup ?


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

0 Kudos
Stampede
Contributor
Contributor
Jump to solution

Hi LucD,

  The report is just a basic CSV that when opened in excel has each VM property on the top row and then each VM populates this on a single line. Here's an example:

Name        IP          CPU    Memory     State

VM1    192.168.0.2     2          4             Off

VM2     192.168.0.3     4          8            On

I want to get each Annotation field in there, as an example I have Application, CERT Location and Database as three separate fields. If I were to add them to the existing document shown above it would look like this:

Name        IP          CPU    Memory     Application     CERT Location     Database     State

VM1    192.168.0.2     2          4               Jumper                 Totowa              SQL           Off

VM2     192.168.0.3     4          8              Jumper2               Brigsfield            SQL          On

Right now when I run a script like:

Get-VM | Get-Annotation | Select @{N="VM";E={$_.AnnotatedEntity.Name}},Name,Value | Export-Csv -path "c:\temp\vAnnotations.csv" -NoTypeInformation

The output comes out:

"VM",                         "Name",          "Value"

"VM01",     "Application",            ""

"VM01",     "CERT Location",      ""

"VM01",     "Database",               ""

"VM01",     "Prod Location",          ""

What should I do to have each item populate a single line? We have a ton of VM's so this method would just make things difficult to read, and result in an incredibly long document.

Thanks,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

$targetCA = 'Application','CERT Location','Database','Prod Location'

$report = @()

foreach($vm in Get-VM){

    $row = [ordered]@{

        VM = $vm.Name

    }

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

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

    }

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

}

$report | Export-Csv 'c:\temp\vAnnotations.csv' -NoTypeInformation -UseCulture


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

Stampede
Contributor
Contributor
Jump to solution

Thanks so much for your help LucD. This is spot on.

Now to combine it all together with my previous script!! Thanks again!

0 Kudos
Stampede
Contributor
Contributor
Jump to solution

Actually I'll ask one more question, since I'm so close to the perfect output.

I keep getting a bad output, "System.Collections.Hashtable", on one of the data points I am looking to add to this spreadsheet. Is there an easier way to pull down the IPv4 address?

$targetCA = 'Application','CERT Location','Database','Prod Location'
$report = @()


foreach($vm in Get-VM){
    $row = [ordered]@{
        VM = $vm.Name
        CPU = $vm.NumCpu
        RAM = $vm.MemoryGB
        Notes = $vm.Notes
        PowerState = $vm.PowerState
        IPAddress = @{N="IP Address";E={@($_.guest.IPAddress[0])}}
    
    }
    Get-Annotation -Entity $vm | where{$targetCA -contains $_.Name} | %{
        $row.Add($_.Name,$_.Value)
    }
    $report += New-Object PSOBject -Property $row
   
}

$report | Export-Csv 'c:\temp\vSphere.csv' -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$targetCA = 'Application','CERT Location','Database','Prod Location' 

$report = @() 

  

foreach($vm in Get-VM) { 

    $row = [ordered]@{ 

        VM = $vm.Name 

        CPU = $vm.NumCpu 

        RAM = $vm.MemoryGB 

        Notes = $vm.Notes 

        PowerState = $vm.PowerState 

        IPAddress = $vm.guest.IPAddress[0]

      

    } 

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

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

    } 

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

     

$report | Export-Csv 'c:\temp\vSphere.csv' -NoTypeInformation -UseCulture 


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

Stampede
Contributor
Contributor
Jump to solution

Wow that was easy Smiley Happy,

Thanks again LucD, your help has really been invaluable.

0 Kudos
corneham
Contributor
Contributor
Jump to solution

Brilliant script LucD. How do you bulk Import contents of vsphere.csv file into a migrated batch of the VM in vSphere 6 ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What would be in the CSV file?


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

0 Kudos