VMware Cloud Community
SuperVirtual
Contributor
Contributor
Jump to solution

VM Snapshot Deletion

Hi, I am after a script which deletes the snapshots which are x number of days old and excludes those which starts with a certain letter

any help will be appreciated 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There was an error in my original script, I updated the code above.

To send an email, you could do

$numberOfDays = 7

$excludeName = "^Exclude"

$report = Get-VM | Get-Snapshot |

    where {$_.created -lt (Get-Date).adddays(- $numberOfDays) -and $_.Name -notmatch $excludeName} | %{

        $snap = $_

        Remove-Snapshot -Snapshot $snap -Confirm:$false -RunAsync > $null

        $snap | Select @{N='VM';E={$_.VM.Name}},Name,Created

    }

Send-MailMessage -From me@domain.com -To me@domain.com -Subject 'Snapshots removed' `

    -BodyAsHtml -Body ($report | ConvertTo-HTML | Out-String) `

    -SmtpServer smtp.domain.com


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$numberOfDays = 7

$excludeName = "^Exclude"

Get-VM | Get-Snapshot |

where {$_.created -lt (Get-Date).adddays(- $numberOfDays) -and $_.Name -notmatch $excludeName} |

Remove-Snapshot -Confirm:$false -RunAsync


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

Reply
0 Kudos
SuperVirtual
Contributor
Contributor
Jump to solution

Thanks a lot LucD 🙂

just one more thing, I want to exclude any snapshot which starts with word save, will this code do the job if I just replace put $excludeName = "^save" ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct.

The -match operator expects a RegEx expression on the right side.

The ^ in the expression indicates that this string shall be matched at the start of the name


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

Reply
0 Kudos
SuperVirtual
Contributor
Contributor
Jump to solution

Sweet and thanks for the quick tutorial ,appreciate that

I will let you know how it goes

Reply
0 Kudos
SuperVirtual
Contributor
Contributor
Jump to solution

One more query I have , can I use the script to connect to multiple vcentre servers with connect-viserver command ?

I also want to add the VM's whose snapshot were purged in a log file (last 7 days will do)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, you can connect to multiple vCenters.

Just make sure that in your PowerCLI configuration the DefaultVIServerMode parameter is set to Multiple (Set-PowerCLIConfiguration).

To get a report, you could do

$numberOfDays = 7

$excludeName = "^Exclude"

$report = Get-VM | Get-Snapshot |

    where {$_.created -lt (Get-Date).adddays(- $numberOfDays) -and $_.Name -notmatch $excludeName} | %{

        $snap = $_

        Remove-Snapshot -Snapshot $snap -Confirm:$false -RunAsync > $null

        $snap | Select @{N='VM';E={$_.VM.Name}},Name,Created

    }

$report | Export-Csv report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
SuperVirtual
Contributor
Contributor
Jump to solution

The script ran fine without issues but the repot.csv does not show the VMname anywhere , any ideas ?

I actually want to grab the VMname and send it on email as a daily report stating that the snapshots for these VM's have been purged successfully

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was an error in my original script, I updated the code above.

To send an email, you could do

$numberOfDays = 7

$excludeName = "^Exclude"

$report = Get-VM | Get-Snapshot |

    where {$_.created -lt (Get-Date).adddays(- $numberOfDays) -and $_.Name -notmatch $excludeName} | %{

        $snap = $_

        Remove-Snapshot -Snapshot $snap -Confirm:$false -RunAsync > $null

        $snap | Select @{N='VM';E={$_.VM.Name}},Name,Created

    }

Send-MailMessage -From me@domain.com -To me@domain.com -Subject 'Snapshots removed' `

    -BodyAsHtml -Body ($report | ConvertTo-HTML | Out-String) `

    -SmtpServer smtp.domain.com


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

Reply
0 Kudos