VMware Cloud Community
sandi82
Contributor
Contributor

Need help for deleted snapshot report in html

Hello,

I need help with for  PowerCLI script to get list of deleted Snapshots report for older then 3 days 

script below but problem is receive blank report but snap shot deleted successfully  

 

Connect-VIServer -Server x.x.x.x-Credential $MyCredential


#This is where the styling for the HTML report is done

$styles = @"

<style>

body { font-family: 'Helvetica Neue', Helvetica, Arial;
font-size: 14px;
line-height: 20px;
font-weight: 400;
color: black;
}
table{
margin: 0 0 40px 0;
width: 100%;
box-shadow: 0 1px 3px rgba(0,0,0,0.2);
display: table;
border-collapse: collapse;
border: 1px solid black;
text-align: left;
}
th {
font-weight: 900;
color: #99ff66;
background: black;
}
td {
border: 0px;
border-bottom: 1px solid black
}
.20 {
width: 20%;
}
.40 {
width: 40%;
}
</style>
"@


#VM Server List

$vmlist = Get-Content C:\Scripts\AUTOSNAP\vmlist.txt
$des = Get-Content C:\Scripts\AUTOSNAP\des.txt
$dc = Get-Datacenter
$VCNAME = $Global:DefaultVIServers

$report = $report = "<!DOCTYPE html><html><head>$Styles<title>VMWare Snapshot Report</title></head><body>"
$report += "<h1>Delete Snapshot Report for $dc $date</h1><table><th>VM Name</th><th>Description</th><th>Date Created</th><th>Deleted By</th><p>Vcenter IP $VCNAME</p><p>Creted By IT TEAM </p>"
$snap = Get-VM $VMlist | Get-Snapshot | Select-Object Description, Created, VM, SizeMB, SizeGB

foreach ($snap in Get-VM $vmlist | Get-Snapshot | Where{$_.Created -lt (Get-Date).AddDays(-3)} | Where{$_.Description -eq 'DAILYSNAP_AUTO_DELETE_IN_3_DAYS'} ) {

$snapevent = Get-VIEvent -Entity $Snap.VM -Types Info -Finish $Snap.Created -MaxSamples 1 | Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'
}
if ($snapevent -ne $null)
{
$sVM = $Snap.VM
$sDesc = $Snap.Description
$sDate = $Snap.Created
$sUser = $snapevent.UserName

if ($sUser -eq "Domain\Username")
{
$report += "<tr style='background-color:MediumSeaGreen; color: MediumSeaGreen;'><td>$sVM</td><td>$sDesc</td><td>$sDate</td><td>$sUser</td></tr>"
} else {
$report += "<tr style='background-color:blue; color: white;'><td>$sVM</td><td>$sDesc</td><td>$sDate</td><td>$sUser</td></tr>"
}
} else {

$sVM = $Snap.VM
$sDesc = $Snap.Description
$sDate = $Snap.Created
$sUser = $snapevent.UserName

if ($sDesc -eq "Please do not delete this snapshot. It is being used by Veeam Backup.")
{
$report += "<tr style='background-color:MediumSeaGreen; color: white;'><td>$sVM</td><td>$sDesc</td><td>$sDate</td><td>$sUser</td></tr>"
} else {
$report += "<tr><td>$sVM</td><td>$sDesc</td><td>$sDate</td><td>$sUser</td></tr>"
}
}
}
$report += "</table>"

foreach($VM in Get-VM $VMlist) {


Get-VM -Name $vmlist | Get-Snapshot | Where-Object{$_.Created -lt (Get-Date).AddDays(-3)} | Where-Object{$_.Description -eq 'DAILYSNAP_AUTO_DELETE_IN_3_DAYS'} | Remove-Snapshot -Confirm:$false

}

Disconnect-VIServer "*" -force -Confirm:$False

$report += "</body></html>"
$report | Out-File "C:\Scripts\AUTOSNAP\Dally-SNAP-Report1.html"

 

Please help

 

0 Kudos
3 Replies
LucD
Leadership
Leadership

There are a couple of issues with these lines

$snap = Get-VM $VMlist | Get-Snapshot | Select-Object Description, Created, VM, SizeMB, SizeGB

foreach ($snap in Get-VM $vmlist | Get-Snapshot | Where{$_.Created -lt (Get-Date).AddDays(-3)} | Where{$_.Description -eq 'DAILYSNAP_AUTO_DELETE_IN_3_DAYS'} ) {

$snapevent = Get-VIEvent -Entity $Snap.VM -Types Info -Finish $Snap.Created -MaxSamples 1 | Where-Object {$_.FullFormattedMessage -imatch 'Task: Create virtual machine snapshot'
}

 

You overwrite the $snap variable in the foreach loop, so what is the point of the first line?
You only get 1 event (-MaxSamples 1) from that VM and so you hope that this the Task event for the creation of the snapshot.
It is highly unlikely that this is the case, hence $snapevent will be empty.
And that might explain why there is nothing in the report.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
sandi82
Contributor
Contributor

Hello Lucd,

Thanks for your help and support 

understand the wrong line I am remove first line of 

$snap = Get-VM $VMlist | Get-Snapshot | Select-Object Description, Created, VM, SizeMB, SizeGB

and second can you help me how to get deleted snap report I am not found any entry in on 

Get-VIEvent

 

Thanks

 

0 Kudos
LucD
Leadership
Leadership

Since you copied that script from Solved: Re: Add user to Snapshot report - VMware Technology Network VMTN you should perhaps ask the user that published that script for help.

Or you could have a look at the script I posted in Solved: Re: Script to determine who created a snapshot - VMware Technology Network VMTN


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos