VMware Cloud Community
jslarouche
Enthusiast
Enthusiast
Jump to solution

PowerCLI - VM+Datastore+Annotations script help?

Can someone help me in getting this script to pull the annotations field of the VM's please?

Get-Datacenter | Get-Datastore | Foreach-Object {
    $ds = $_.Name
    $_ | Get-VM | Select-Object Name,@{n='DataStore';e={$ds}}
} | Export-Csv C:\VMList.csv -NoTypeInformation -UseCulture
Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

A VM can have multiple annotations. I am not sure if you want to combine that in the same output as the datastores. The following script lists all the names and values of the VM's annotations:

Get-VM | ForEach-Object {
  $VM = $_
  $VM | Get-Annotation |
    ForEach-Object {
    $Report = "" | Select-Object VM,Name,Value
    $Report.VM = $VM.Name
    $Report.Name = $_.Name
    $Report.Value = $_.Value
    $Report
  }
} | Export-Csv -Path VMAnnotations.csv -NoTypeInformation -UseCulture

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
8 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

A VM can have multiple annotations. I am not sure if you want to combine that in the same output as the datastores. The following script lists all the names and values of the VM's annotations:

Get-VM | ForEach-Object {
  $VM = $_
  $VM | Get-Annotation |
    ForEach-Object {
    $Report = "" | Select-Object VM,Name,Value
    $Report.VM = $VM.Name
    $Report.Name = $_.Name
    $Report.Value = $_.Value
    $Report
  }
} | Export-Csv -Path VMAnnotations.csv -NoTypeInformation -UseCulture

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
jslarouche
Enthusiast
Enthusiast
Jump to solution

That works thanks. 

Reply
0 Kudos
Sanjuro
Contributor
Contributor
Jump to solution

I tried this script.. Got no errors, but it only put an empty .csv on my desktop.

Any ideas?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you connected to a vCenter or an ESXi server ?

You do have custom attributes on your VMs ?


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

Reply
0 Kudos
Sanjuro
Contributor
Contributor
Jump to solution

LucD,

I'm connected to vCenter and some but not all vm's have data in the Annotations field.

Reply
0 Kudos
Sanjuro
Contributor
Contributor
Jump to solution

By "Custom Attributes" I'm assuming you mean data in the Annotations field of the VM. Am I correct in this assumption?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try it like this

Get-VM | Get-Annotation | 
Select @{N="VM";E={$_.AnnotatedEntity.Name}},Name,Value |
Export-Csv -Path VMAnnotations.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
Sanjuro
Contributor
Contributor
Jump to solution

This is what I'm running from the ISE. (Ignore my Set-PowerCLIConfiguration. I plan to use this script for multiple vCenter Servers..) But it still only places and empty .csv in the -path no errors are thrown.

Add-PSSnapin VMware.VimAutomation.Core -erroraction SilentlyContinue

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false

connect-viserver 'vCenter' -WarningAction SilentlyContinue | out-null Get-VM | Get-Annotation | Select @{N="VM";E={$_.AnnotatedEntity.Name}},Name,Value | Export-Csv -Path $env:userprofile\desktop\VMAnnotations.csv -NoTypeInformation -UseCulture

Set-PowerCLIConfiguration -DefaultVIServerMode Single -Confirm:$false

Sorry I don't know how to paste the code properly in the forum...

Reply
0 Kudos