Thank you for your help on this, I got this almost perfect, but I am trying to find a way to only report snapshots 5 days older but I am not sure how do this via Get-View This is what I got so far:
#############################
# Variables #
#############################
$date = Get-Date -format "yyyy-MMM-d"
$datetime = Get-Date
$filelocation = "\var\www\Snapshots\snapshot-$date.htm"
#############################
# Content #
#############################
$timeDiff = 2
$report = Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn';'Snapshot'=".+"} |
Select -First 50 Name,
@{N='SnapShot';E={
function Get-Snap{
param([PSObject]$snap)
$snap
if($snap.ChildSnapshotList){
$snap.ChildSnapshotList | %{
Get-Snap -Snap $_
}
}
}
$script:snaps = $_.Snapshot.RootSnapshotList | %{
Get-Snap -Snap $_
}
($script:snaps | sort-Object -property Name).Name -join '|'}},
@{N='SnapShot Created';E={($script:snaps | sort-Object -property Name).CreateTime -join '|'}},
@{N="Days Old";E={
$now = Get-Date
($script:snaps | %{(New-TimeSpan -End $now -Start $_.CreateTime).Days}) -join '|'}},
@{N='Created By';E={
$startEvents = $script:snaps.CreateTime
$start = $startEvents | Sort-Object | Select -First 1
(Get-VIEvent -Entity $_.Name -Start $start.AddSeconds(- $timeDiff) -MaxSamples ([int]::MaxValue) |
Where-Object {$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'VirtualMachine.createSnapshot'} |
ForEach-Object -Process {
$event = $_
$startEvents | ForEach-Object -Process {
if([math]::Abs((New-TimeSpan -Start $event.CreatedTime -End $_.ToLocalTime()).TotalSeconds) -lt $timeDiff){
$event
}
}
} | Select-Object -ExpandProperty UserName) -join '|'}}
#############################
# Add Text to the HTML file #
#############################
$report | ConvertTo-Html –title "VMware Snapshot Check " –body "<H1>VMware Snapshot Check</H1>" -head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File $filelocation
ConvertTo-Html –title "VMware Snapshot Check " –body "<H4>Date and time</H4>",$datetime -head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File -Append $filelocation
ConvertTo-Html –title "VMware Snapshot Check " –body "<H4>VM Count</H4>",$report.Count -head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File -Append $filelocation