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
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
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
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" ?
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
Sweet and thanks for the quick tutorial ,appreciate that
I will let you know how it goes
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)
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
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
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