Remove Custom Attribute script from Virtual Machines annotation

PowerCLI

In Today post I would like to show you quick method of replacing annotations which quite a lot of people use to store details about Virtual Machines. As usual this short script was created to automate things rather than do it all manually.

In my company we use annotations to store information about Virtual Machines. I was quite often asked about VMs I deployed three Years ago - like I remember it now 🙂 So to remove information about myself I used following script.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
###
# Purpose        : Replace custom attribute in Virtual Machines
# Created        : 24.06.2015
# Author         : Wojciech Marusiak
# Pre-requisites : none
###
Add-PSSnapin VMware.VimAutomation.Core
If ($globale:DefaultVIServers) {
    Disconnect-VIServer -Server $global:DefaultVIServers -Force
    }
    
$vCenter = Read-Host "Please provide name or IP address of the vCenter Server"
$credentials = get-credential
connect-viserver -server $vCenter -Credential $credentials

$vms = Get-VM | where {(get-annotation -Entity $_ -CustomAttribute Creator).Value -eq "searched phrase"} 
foreach($vm in $vms){
Set-Annotation -Entity $vm -CustomAttribute "Creator" -Value " "
}

Disconnect-VIServer "*" -Confirm:$False

In my case I am replacing custom attribute Creator with space. If you want to replace another custom attribute you first need to find it.

To show which custom attributes you have run following oneliner.

1
get-vm -name vmname | Get-Annotation

Replace custom attribute - 1

Afterwards simply change in line 16 value for -CustomAttribute and in line 18 the one in quotation mark.

Replace custom attribute - 2

All rights reserved © Wojciech Marusiak
Last updated on 24 Jun, 2015 14:02 ETC
Built with Hugo
Theme Stack designed by Jimmy