The snapshot report script has been working great, until i noticed it does not handle VMs that have multiple snapshots very well and the "Days Old" result displays a "System.Object []" error. I tried to rewrite it a bit so the logic outputs the correct number of days for VMs with multiple snapshots but at my wits end. Any advice?
thanks
example of error:
Snapshot Name | Description | Created By | Virtual Machine | Created On | SizeMB | Days Old |
20130621_upgrade | upgrade | User1 | Test1 | 6/21/2013 3:55:03 PM | 5440 | 7 |
20130624_patch | patch | User1 | Test2 | 6/24/2013 11:43:40 AM | 120 |
|
Pre-Project | testing | User2 | Test2 | 6/25/2013 12:15:25 PM | 5152 |
|
|
|
|
|
|
|
|
current script that works great otherwise (edited out the sensitive bits):
Add-PSSnapin VMware.VimAutomation.Core Connect-VIServer #,# $OldSnapshotList = Get-VM | where {$_.name -notlike "*wvm*"} | get-snapshot IF ($OldSnapshotList -ne $null) { $Report = @() $SnapUser = "" FOREACH ($Snapshot in $OldSnapshotList) { $TaskMgr = Get-View TaskManager $TaskNumber = 100 $Filter = New-Object VMware.Vim.TaskFilterSpec $Filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime $Filter.Time.beginTime = ((($Snapshot.Created).AddSeconds(-60)).ToUniversalTime()) $Filter.Time.timeType = "startedTime" $Filter.Time.EndTime = ((($Snapshot.Created).AddSeconds(60)).ToUniversalTime()) $Filter.State = "success" $Filter.Entity = New-Object VMware.Vim.TaskFilterSpecByEntity $Filter.Entity.recursion = "self" $Filter.Entity.entity = (Get-Vm -Name $Snapshot.VM.Name).Extensiondata.MoRef $TaskCollector = Get-View ($TaskMgr.CreateCollectorForTasks($Filter)) $TaskCollector.RewindCollector | Out-Null $Tasks = $TaskCollector.ReadNextTasks($TaskNumber) FOREACH ($Task in $Tasks) { $GuestName = $Snapshot.VM $Daysold = Get-VM $GuestName | Get-Snapshot | Select @{N="DaysOld"; e={((Get-Date) - $_.Created).Days}} $Task = $Task | where {$_.DescriptionId -eq "VirtualMachine.createSnapshot" -and $_.State -eq "success" -and $_.EntityName -eq $GuestName} IF ($Task -ne $null) { $SnapUser = $Task } $TaskCollector.ReadNextTasks($TaskNumber) } $SplitSnapUser = $SnapUser.Reason.Username -split('\\') | select -last 1 #Create a custom object for reporting $objReport = New-Object System.Object $objReport | Add-Member -Type NoteProperty -Name "Snapshot Name" -Value $Snapshot.Name $objReport | Add-Member -Type NoteProperty -Name "Description" -Value $Snapshot.Description $objReport | Add-Member -Type NoteProperty -Name "Created By" -Value $SplitSnapUser $objReport | Add-Member -Type NoteProperty -Name "Virtual Machine" -Value $Snapshot.VM $objReport | Add-Member -Type NoteProperty -Name "Created On" -Value $Snapshot.Created $objReport | Add-Member -Type NoteProperty -Name "SizeMB" -Value ([System.Math]::Round($Snapshot.SizeMB)) $objReport | Add-Member -Type NoteProperty -Name "Days Old" -Value $DaysOld.DaysOld $Report += $objReport $TaskCollector.DestroyCollector() } } $head = "<style> TABLE{border-width:1px; border-color: black;font-size:12px; font-family:courier; border-style: solid; border-collapse: collapse;} TH{border-width:1px; border-style: solid; padding: 0px; border-color: black; background-color: #006699; color: white;} TD{border-width:1px; border-style: solid; padding: 7px; border-color: black; background-color:#424242; color: white;} </style>" $mailreport = $report | sort "Created On" | ConvertTo-Html -Head $head | Out-String Send-MailMessage -to #,#,# -from # -subject "vCenter Snapshot Report" -body $mailreport -BodyAsHtml -smtpServer # Disconnect-VIServer # -Confirm:$false Disconnect-VIServer # -Confirm:$false