bernz
Enthusiast
Enthusiast

Get / Set VM Annotation & Custom Attribute

Hi all

Would like to seek your expertise on exporting and importing Virtual Machines annotation & all custom attribute that is located in specific Folder and then create a CSV/XLS report.

Sample:

Location = "Offsite DC"

Foldername = "BUILDS"

2 Replies
RvdNieuwendijk
Leadership
Leadership

You can the following PowerCLI script to export the annotations and all custom attributes of the virtual machines in datacenter Offsite DC and folder BUILDS to a file named VMannotations.csv:

Get-Datacenter -Name "Offsite DC" | Get-Folder -Name "BUILDS" | 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

The following PowerCLI script can be  used to import the annotations from the VMannotations.csv file:

Import-Csv -Path VMAnnotations.csv | Where-Object {$_.Value} | ForEach-Object {

  Get-VM $_.VM | Set-Annotation -CustomAttribute $_.Name -Value $_.Value

}

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

I'll give a try ... thanks.

0 Kudos