VM Creation Historical Report

I needed something that I could look back upon to determine who created what VM in vCenter and from what template.  This works when dealing w/ just vCenter and not vCloud Director.  It's painfully slow, but it works as a stopgap until I figure out how to query the same information from SQL instead.

Here is an screenshot example of what the PowerCLI script outputs:

Although, I can probably gather a longer historical view through CSV's than vCenter's SQL if those events/tasks are cleaned up after a period of time.  It's meant to be run on a daily basis.

Script found after the break:

The script assumes IIS as your web server, so you'll need to change some things if you have different target web server.  I also assume the existence of a sorttable.js and a css file.  Both of which you can download.

Copy everything below:
#################################################
#Title: VM Creation Historical Report            #
#Filename: Get-VMCreationReport.ps1                #
#Originally written by K. Chris Nakagaki        #
#http://tech.zsoldier.com/                      #
#################################################

Add-PSsnapin VMware.VimAutomation.Core

Connect-VIServer yourvCenterServerName

# Dates (Midnight specified for absolute start date/time for event collection)
$Date = Get-Date -Hour 0 -Minute 0 -Second 0
$FileFormatDate = Get-Date -Format yyyy.MM.dd-hh.mm.ss

#Enter Datastore names, such as local or read-only stores that should be excluded from totals.
$htmpath        = "C:\inetpub\wwwroot\"
$csvpath        = "C:\inetpub\wwwroot\csv\VMCreationHistory\"
$htmname        = "VMCreationHistoryReport.htm"
$csvname         = "$($FileFormatDate)VMsCreated.csv"

#Report Title appears in <title> section and shows in the browser window.
$ReportTitle     = "VM Creation History Report"

#IconPath is the icon that is rendered on supported web browsers tab or address bar
$IconPath        = "/images/mySuperDuperAwesomeCustomIcon.ico"

#AppleIconType, set this to "Apple-touch-icon" if you would like the iOS device to render a glass effect on top of your custom icon.
#Otherwise use "Apple-touch-icon-precomposed" to just use your stock image.  45x45 is recommended, but higher rez can be used.
$AppleIconType     = "apple-touch-icon-precomposed"

#AppleTouchIcon is the image that appears on an iOS and/or Android device when a home screen bookmark is created.
$AppleTouchIcon = "/images/apple-touch-icon.png"


#Stylesheet is the path where your CSS file lies.
$Stylesheet        = "/styles/style.css"

#SortScript is where the sorttable.js script resides.  This allows dynamic sorting of table information within the browser.
$SortScript     = "/scripts/sorttable.js"

#Notes will be added to the footer of the page.
$Notes             = ""

#HTML Code for <Head> Section, simply append additional code
$Head = "<LINK REL='SHORTCUT ICON' HREF='$($IconPath)'><link rel='$($AppleIconType)' href='$($AppleTouchIcon)'/><title>$($ReportTitle)</title><link rel='stylesheet' type='text/css' href='$($Stylesheet)'><script src='$($SortScript)'></script>"

## Script Output Section ##
$VIEvent = Get-VIEvent -maxsamples 500000 -Start $Date.AddDays(-1) -Finish $Date
$OutputCreatedVMs = @($VIEvent | where {$_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmBeingClonedEvent" -or $_.Gettype().Name -eq "VmBeingDeployedEvent"} | Select createdTime, UserName, fullFormattedMessage)
$OutputCreatedVMs | Export-Csv -NoTypeInformation "$($csvpath)$($csvname)"

$OutputCreatedVMs | ConvertTo-Html -Body "<a name='Data'></a><strong>$ReportTitle $date</strong> 
<br><a href=./csv/VMCreationHistory/>(Click here to see historical csv copies)</a>" -Head $Head | 
foreach {$_.replace("&lt;","<").replace("&gt;",">").replace("<tr><th>","<table class='sortable'><thead><tr><th>").replace("</th></tr>","</th></tr></thead><tbody>").replace("</table>","</tbody><tfoot></tfoot></table>")} |
Out-File -Encoding ASCII -FilePath "$($htmpath)$($htmname)"

Disconnect-VIServer yourvCenterServerName -Confirm:$false

Comments

Popular posts from this blog

NSX-T: Release associated invalid node ID from certificate

NSX-T: vCenter and NSX-T Inventory out of Sync (Hosts in vSphere not showing up in NSX-T)

Azure VMware Solution: NSX-T Active/Active T0 Edges...but