Skip to content

Checking VM Snapshot Age via PowerCLI

Herkese Selamlar,

Bildiğiniz gibi Snapshotlar sanal sunucuların performanslarını olumsuz yönde etkileyebilirler.

Özellikle IO intensive sunucularda, snapshot uzun süre kalırsa sonrasında performans sorunları ve delta diskin silinmesi sonrası consolidation ihtiyaçları vs gerekebilir.

Vcenter alarmları üzerinde maalesef sadece snapshot size’ı kontrol edilebiliyor. Burada tabiiki ek trigger ile sunucunun toplam disk kapasitesine göre bir ikinci koşul koyulabilir fakat tam aradığımız sonucu vermeyebilir.

Çok şükür PowerCLI var, bu sayede kontrollerimizi yapıp, işimizi görecek scriptler çalıştırabiliyoruz. Aşağıdaki scripti alıp kendinize göre editler yada geliştirirseniz, günlük olarak sistemden raporlarınızı alabilirsiniz.

PowerShell script to check for VM snapshots and send Email report
Forked by sahinguneri@gmail.com
Reference: https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-Scripting-SnapShot-Create-Date-is-older-than-3-days/m-p/1400098
#Connect Inventory
 Connect-VIServer -Server 'vcenter.tncloud.local' -User 'monitoruser@domain.local' -Password '********'
 HTML formatting
 $a = ""</p> <p>$a = $a + "BODY{background-color:white;}"</p> <p>$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"</p> <p>$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}"</p> <p>$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}"</p> <p>$a = $a + ""
 Main section of check
 Write-Host "Checking VMs for for snapshots"
 $date = get-date
 $datefile = get-date -uformat '%m-%d-%Y-%H%M%S'
 $filename = "C:\Windows\Temp\Snapshots_" + $datefile + ".htm"
 Get list of VMs with snapshots
 Note:  It may take some time for the  Get-VM cmdlet to enumerate VMs in larger environments
 $ss = Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-1)}
 if($ss -eq $null ){
 Disconnect-VIServer -Server  * -Confirm:$false Write-Host "There are no aged snapshot , Exit" -ForegroundColor RED
 }else{
Write-Host " There Are Aged Snapshots , Continue  " -ForegroundColor Green
Write-Host "Generating VM snapshot report" $ss | Select-Object vm, name, Created, powerstate, SizeGB | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report </H2>"| Out-File $filename 
Write-Host "  Report Generated " -ForegroundColor Green 
Write-Host "Your snapshot report has been saved to:" $filename 
Write-Host "Sending Email" $SMTPServer = "mailserver.domain.com" $SMTPPort = 25 
$username = "sender@domain.com" 
$to = "receiver@domain.com" 
$subject = "Aged Snapshot Alert" 
$body = "Aged Snapshot Alerts" 
$attachment = new-object Net.Mail.Attachment($filename) 
$message = New-Object System.Net.Mail.MailMessage 
$message.subject = $subject 
$message.body = $body 
$message.to.add($to) 
$message.from = $username 
$message.attachments.add($attachment) 
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); 
#$smtp.EnableSSL = $true 
#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); $smtp.send($message) write-host "Mail Has been Sent" 
Disconnect-VIServer -Server  * -Confirm:$false
 }
Published inPowerCLIVMware

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *