Updating Vcenter Annotations or Notes with PowerCLI

Vcenter allows you to add notes/comments into the Annotations field per individual VMs. This is very handy if you manage a large environment in which you need to descriptions about the VMs.

In this guide we’re trying to accomplish the following:
Add the following information in this Annotation field:

VM Owner: Requestor

Purpose of System: Purpose

Date Created: SystemDateCreation

Here’s a screenshot of my environment:

(Note: initially I exported the list of VM’s and uploaded them to a SharePoint site in which other system owners could update the Requestor and Purpose fields with accurate information about those systems).

Once the changes were made, I saved this list as Original.csv and created a script to compare that list to AD in order to get the Creation Date (This was one of the VMware Administrator requirements since Vmware purges the VMware creation value after some time)

The Original.csv file contains the following fields: VMName, Requestor, Purpose

NOTE: you need to install Vmware Vsphere POwerCLI 5.5 (or later) to be able to run the powerscript below; otherwise, it won’t work!

Additionally ensure that you have permission to update the Vmware information, otherwise this will fail.

On to the script!

#What does this script do?

# 1. YOU have to manually Export the list of VMs from Vsphere and save it as originallist.csv
# 2. Add the following columns to your spreadsheet and update them them appropriately : Requestor	Purpose
# 3. Run this powershell script, it will Compare each hostname to Active Directory, and if the system is found, it will- 
# get the WhenCreated attribute and append it to the new Newlycreatedlist.csv with a new field called "SystemCreation"

#change the originallist.csv to a qualified path example: c:\users\username\originalist.csv  (unless you are working in current folder)

Import-Csv originalist.csv |

ForEach-Object {

 $DateCreated = ForEach-Object {Get-ADComputer -identity $_.VMName -properties * |select whenCreated}

Add-Member -MemberType NoteProperty -Name SystemCreation -Value $DateCreated -PassThru }|
Export-CSV Newlycreatedlist.csv -notype

#This next portion will remove unwanted characters that were automatically appended by the AD attributes "@[WhenCreated]

$test = import-csv Newlycreatedlist.csv
$test | ForEach-Object {
$_.SystemCreation = $_.SystemCreation.replace("@{whenCreated=","")
$_.SystemCreation = $_.SystemCreation.replace("}","")
$test | Export-Csv Newlycreatedlist.csv -NoType

# This last section establishes the connectsion to Vcenter and updates the Notes field with the information in the csv file
Connect-VIServer yourvcenter.domain.com

$CSV = Import-Csv "Newlycreatedlist.csv"|select VMName,Requestor,Purpose,SystemCreation, @{n='Note';e={"Requestor: " + $_.Requestor  + "    Purpose: " + $_.Purpose + "     DateCreated:" + $_.SystemCreation}}
$CSV | %{Set-VM $_.VMName -Notes $_.Note -Confirm:$False}

Once you run this script, you should be able to see the following info:

When viewing the list of VMs in your environment, you should be able to see this info in the Notes field. It makes it easier to keep information related to the VM here.

That is all!


0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments