Automation

 View Only
  • 1.  power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Jun 23, 2022 12:38 PM

    I need to get a report in email when the size of the snapshot of a particular VM  is > 3TB and  if it is less than the 3TB size then the script  has to simply exit and send no email. I have a script below but need to modify it according to the requirement. Can some one please advise and help.

    I can task schedule this on daily basis to run and send the report. Thanks

    # PowerShell script to check for VM snapshot size more than 3TB and send Email report
     
    add-pssnapin VMware.VimAutomation.Core
     
    Connect-VIServer -Server <ip-or-host> -User <username> -Password <password>
     
    # HTML formatting
     
    $a = "<style>"
     
    $a = $a + "BODY{background-color:white;}"
     
    $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
     
    $a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}"
     
    $a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}"
     
    $a = $a + "</style>"
     
    # Main section of check
     
    Write-Host "Checking VM for snapshots"
     
    $date = get-date
     
    $datefile = get-date -uformat '%m-%d-%Y-%H%M%S'
     
    $filename = "c:\dir\Snapshots_" + $datefile + ".htm"
     
    # Get VM with snapshots
     
    # Note:  It may take some time for the  Get-VM cmdlet to enumerate VMs in larger environments
     
    $ss = Get-VM  Vm Name | Get-Snapshot
     
    Write-Host "   Complete " -ForegroundColor Green
     
    Write-Host "Generating VM snapshot Size report"
     
    #$ss | Select-Object VM, Name, Created, SizeGB | ConvertTo-HTML -head $a -body "<H2>VM Snapshot size Report</H2>"| Out-File $filename
     
    $ss | Select-Object VM, Name, Created, SizeGB | ConvertTo-HTML -head $a -body "<H2>VM Snapshot size Report</H2>"| Out-File $filename
     
    Write-Host "   Complete " -ForegroundColor Green
     
    Write-Host "Your snapshot report has been saved to:" $filename
     
    $SMTPServer = <smtp_server>
     
    $SMTPPort = 587
     
    $Username = "email@email.com"
     
    #Define the receiver of the report
     
    $to = "recipient.email@email.com"
     
    $subject = "VM Snapshot Size Report"
     
    $body = " VM Snapshot Size Report"
     
    $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 Sent"


  • 2.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Jun 23, 2022 01:38 PM

    The post where you got this script also has an answer on how to use the Where-clause.
    Instead of the creation date you would have to test on the SizeGB property.



  • 3.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Jun 28, 2022 11:54 AM

    I tried but I am not able to figure out the logic or math to get the 3TB size specification. Can some one please help on the logic. Thanks



  • 4.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Jun 28, 2022 11:57 AM

    What did you place in the Where clause?



  • 5.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Jun 28, 2022 12:18 PM

    here is the logic, not sure if this is correct?

    Where {$_.SizeGB -lt (Get-Disk).size > 3000GB/1GB}



  • 6.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Jun 28, 2022 12:21 PM

    Can you try with

    Where {$_.SizeGB -gt (3 * 1024)}

    You were looking for snapshots greater than 3TB, correct?



  • 7.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Jun 28, 2022 12:25 PM

    Yes correct LucD. I am looking for snapshots greater than 3TB. I will try this and will update you the status. Thanks



  • 8.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Nov 23, 2022 04:17 PM

    what if you wanted to include the contents of that attachment that was created in the body of the email itself?

     



  • 9.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Nov 23, 2022 05:13 PM

    Read the content of that file, organise it (don't forget newlines) and assign that to the Body property



  • 10.  RE: power shell script to check a VM snapshot size when it is more than "x"TB and send an email.

    Posted Nov 23, 2022 07:11 PM

    I tweaked it slightly to send the contents in the email body rather than an attachment.

    # PowerShell script to check for VM snapshots older than 1w and send Email report
    
    Connect-VIServer -Server vcsa.example.com -User "exampleuser" -Password "examplepassword"
    
    $SMTPServer = "smtp.example.com"
    $SMTPPort = 25
    $SMTPFrom = "vcenter@example.com"
    $SMTPTo = "distro@example.com"
    
    # HTML FORMATTING
    $a = "<style>"
    $a = $a + "BODY{background-color:white;}"
    $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
    $a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}"
    $a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}"
    $a = $a + "</style>"
    
    # GET LIST OF VMs WITH SNAPSHOTS
    Write-Host "Checking VMs for for snapshots"
    $date = get-date
    $datefile = get-date -uformat '%m-%d-%Y-%H%M%S'
    #$ss = Get-vm | Get-Snapshot
    $ss = Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-7)}
    Write-Host "   Complete " -ForegroundColor Green
    Write-Host "Generating VM snapshot report"
    $ssreport = $ss | Select-Object vm, name, Created, SizeGB, powerstate | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report </H2> <p>Snapshots over 1w old</p>"
    Write-Host "   Complete " -ForegroundColor Green
    
    # CREATE THE EMAIL
    $subject = "VM Snapshot Report " + $datefile
    $body = $ssreport
    $message = New-Object System.Net.Mail.MailMessage
    $message.subject = $subject
    $message.IsBodyHTML = $true
    $message.body = $body
    $message.to.add($SMTPTo)
    $message.from = $SMTPFrom
    
    # SEND THE EMAIL
    Write-Host "Sending Email"
    $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
    $smtp.send($message)
    Write-Host "   Complete " -ForegroundColor Green