VMware Cloud Community
Leffingwell
Contributor
Contributor

Script picking up wrong snapshots

Hey All,

Been working on a script to delete snaps automatically each night that have aged over 2 weeks.  Here is my script logic.. I had thought it was solid but I just got a report this morning that the log shows it deleted a snap that was just made a day ago.. Thanks in advance for any insight !

$SnapAge = (Get-Date).AddDays(-14)

<omitted>

Get-Cluster -Name *oduction* | Get-VM |
    Get-Snapshot |
    %{
        Write-Output "I've entered the Get-Snapshot loop"
        if ($_.Created.ToLocalTime() -lt $SnapAge)
        {
            $_ | Select @{ n="VM Name"; e={ $_.VM.Name}}, @{ n="Snap Name"; e={ $_.Name}}, @{ n="Created On"; e={ $_.Created.ToLocalTime()}}, SizeMB
            $LogOutput += "Removed snap from: " + $_.VM.Name + ", named: " + $_.Name
            $AmountReclaimed += $_.SizeMB
            Remove-Snapshot $_ -Confirm:$False
        }
    }

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

Are you sure that the $_.Created.LocalTime() actually was 1 day ago ?

And that $_.Created was not days off ?

It could be a regional settings thing (like US and European date formats)


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

Reply
0 Kudos
Leffingwell
Contributor
Contributor

Hey LucD,

Thanks for the quick reply, I've decided to add those two values into the log it emails out, and removed the deletion element from the script.  I'll observe over the next week or so and depending on the results will post here!  So stay tuned Smiley Happy

Kindest Regards,

ALAN

Reply
0 Kudos
Leffingwell
Contributor
Contributor

Hey All,

So it's been a few weeks - we verified that the script was picking up snapshots that were new, and showing them with inaccurate dates in the log.

We had one instance where it showed a snapshot that was over a year old, but it picked it up randomly one day even though the script had run every night for a week... Turns out it was a new snapshot, and so we're positive now that the mechanic the script is using to get the date and judge whether or not it's 2 weeks old or not is unreliable.

Timezone wise, I'm not sure where to look for differences?? The comparison date is based on the local machine? So I'd be theoretically looking for differences between target vSphere servers and the machine running the script? Only question about that is: assuming a few hours to even a days time difference, it wouldn't be that dramatic if we were a day behind deleting a two week old snapshot.  I do know that no machines in our environment are more than their timezones differences apart.

Looking forward to continue the quest to solve this most pesky of scripts!

Kindest Regards,

Alan

Message was edited by: fixed to be more relevant, and less repetitive

Reply
0 Kudos
LucD
Leadership
Leadership

I agree with your conclusion, a TZ issue would only explain a difference of maximum 23 hours.

Could it be that the date format in the timestamp is the cause ?

For some 1/2/2013 is February 1st, for others it is January 2nd.


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

Reply
0 Kudos
Leffingwell
Contributor
Contributor

It's a good point - I will check this the next time the script logs any snap.  I've disabled the deletion part of it, so now rather than just going off of my colleagues word, I'll be able to verify and test the assessment end of the script.  I'll report my findings as I uncover them.  There's no reason that logic shouldn't work.

Thanks as always for the fast feedback LucD - stay tuned !

ALAN

Reply
0 Kudos