Audit VMware VM’s for Creator and write to vCenter VM notes with PowerShell and PowerCLI 4.1.1

I will start off by saying that this was not my idea to create this awesome script. The real hero behind this is Alan Renouf who first published the original script here. 

For anyone working with VMware and PowerShell, Alan’s blog is a “must bookmark” as it contains some great information regarding using PowerCLI, including this great script for reporting your virtual infrastructure state.

What I have done is added some error handling to the script and added a few PowerCLI 4.1.1 changes to it as well as removing the Quest AD cmdlets for translating the usernames.

The jist of what all of this can do can be found by checking out Alan’s blog and viewing all of his posting. The only thing I will cover is that I found that I could not use the Set-CustomField cmdlet as it had changed in 4.1.1. So instead, I used the Set-Annotation cmdlet to perform the same task. Also because the default max for the Get-VIEvent is for 100 logs and our infrastructure had been around for a while, I went with a more “wilder” number to make sure I got everything. Obviously, this hinders performance and should be changed after the initial run to speed things up.

In the end, what we have here is an amazing way to have some accountability over who created what VM and when it was created. What I have done at work is created a scheduled job that runs nightly to make any updates as required. This could even be expanded to send out a nightly email of new VM’s which were created during the day.

 

#Credit to Alan Renouf for original script
#http://www.virtu-al.net/2010/02/23/who-created-that-vm/

Begin {
    If (-NOT (Get-CustomAttribute "CreatedBy" -ea silentlycontinue)) {
        Write-Host "Creating 'CreatedBy' attribute"
        New-CustomAttribute -Name "CreatedBy" -TargetType VirtualMachine
        }
    If (-NOT (Get-CustomAttribute "CreatedOn" -ea silentlycontinue)) {
        Write-Host "Creating 'CreatedOn' attribute"
        New-CustomAttribute -Name "CreatedOn" -TargetType VirtualMachine
        }
    Try {        
        $vms = Get-VM
        }
    Catch {
        Write-Warning "$($Error[0])"
        Break
        }
    }#End Begin
Process {
    ForEach ($vm in $vms) {
        If (-NOT $vm.CustomFields['CreatedBy']) {
            Write-Host "Looking for creator of $($vm.name)"
            Try {
                $event = $vm | Get-VIEvent -MaxSamples 500000 -Types Info | Where {
                    $_.GetType().Name -eq "VmBeingDeployedEvent" -OR $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent"`
                     -or $_.Gettype().Name -eq "VmClonedEvent"
                    }#End Where
                If (($event | Measure-Object).Count -eq 0) {
                    $username = "Unknown"
                    $created = "Unknown"
                    }#End If
                Else {
                    If ([system.string]::IsNullOrEmpty($event.username)) {
                        $username = "Unknown"
                        }#End If
                    Else {
                        $username = $event.username
                        }#End Else
                    $created = $event.CreatedTime
                    }#End Else
                Write-Host "Updating $($vm.name) attributes"
                $VM | Set-Annotation -CustomAttribute "CreatedBy" -Value $username | Out-Null
                $VM | Set-Annotation -CustomAttribute "CreatedOn" -Value $created | Out-Null
                }
            Catch {
                Write-Warning "$($Error[0])"
                Return
                }
            }#End If
        }#End ForEach
    }#End Process

About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
This entry was posted in PowerCLI, powershell, scripts, VMWare and tagged , , . Bookmark the permalink.

2 Responses to Audit VMware VM’s for Creator and write to vCenter VM notes with PowerShell and PowerCLI 4.1.1

  1. Guest says:

    Thank you, this added the right properties I was missing and works great with 4.1.1 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s