Powershell removal of snapshots

Checking for snapshots, using powershell. 

connect-viserver <vCenter_ServerName>
get-vm | get-snapshot | select VM, PowerState, Name, Description, SizeMB, Created

--it produces this report--


VM          : VMName
PowerState  : PoweredOff
Name        : Post SQL Install
Description :
SizeMB      : 0.03
Created     : 6/23/2011 10:09:16 AM

Next, the removal process 

get-vm | get-snapshot | where {$_.SizeMB -gt 1024} | Remove-Snapshot -confirm:$false

This removes snapshots larger than 1 GB in size without confirming each removal.  Now to put it all together along with an email stating the snapshots that were removed.


Thanks to this link
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/5f7a9d63-6a37-4d98-b710-d76fde920a37/

connect-viserver <vCenter_Server>

get-vm | get-snapshot | where {$_.SizeMB -gt 1024} | select VM, PowerState, Name, Description, SizeMB, Created | export-csv c:\output\Snapshots.csv

$EmailFrom = "<user@domain.local>"
$EmailTo = "<user@domain.local>"
$EmailSubject = "<email subject>"

$SMTPServer = "smtphost.domain.local"
$SMTPAuthUsername = "username"
$SMTPAuthPassword = "password"
 
$emailattachment = "c:\output\snapshots.csv"

function send_email {

$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody

$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
$mailmessage.Attachments.Add($attachment)

#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword")
$SMTPClient.Send($mailmessage)
} get-vm | get-snapshot | where {$_.SizeMB -gt 1024} | Remove-Snapshot -confirm:$false



Comments