none
IF Vmware doesn't have snapshot. Send email "No snapshots found" RRS feed

  • Question

  • Hello,

    Could you please help me modify the script below? The script below will check snapshots in Vsphere and will send a report in an email. I wanted to edit the script below like if it doesn't find any snapshots it will send an email with the body/contents that says "No snapshots found". 

    Add-PSSnapin VMware.VimAutomation.Core

    Connect-VIServer -Server 'vcenter' -User 'login' -Password 'password'

    $Snapshots = Get-VM | Get-Snapshot | select Description,Created,VM,Name,SizeMB,SizeGB

    function Get-SnapshotSize ($Snapshot)
    {
        if ($snapshot.SizeGB -ge "1")
        {
            $Snapshotsize = [string]([math]::Round($snapshot.SizeGB,3)) + " GB"
            }
            else {
            $Snapshotsize = [string]([math]::Round($snapshot.SizeMB,3)) + " MB"
            }
       Return $Snapshotsize
    }

    function set-SnapshotDate ($snapshot)
    {
        $greenValue = (get-date).AddDays(-7)
        $RedValue = (get-date).AddDays(-14)
        
        if ($snapshot.created -gt $greenValue)
            {
                $backgroundcolor = "green"
            }
        elseif ($snapshot.Created -lt $greenValue -and $snapshot.Created -gt $RedValue)
            {
                $backgroundcolor = "yellow"
            }
        else 
            {
            $backgroundcolor = "red"
            }
        return $backgroundcolor
    }

    function Format-HTMLBody ($body)
    {
        $newbody = @()
        foreach ($line in $body)
        {
            ## Remove the Format Header
            if ($line -like "*<th>Format</th>*")
                {
                    $line = $line -replace '<th>Format</th>',''
                }
            ## Format all the Red rows
            if ($line -like "*<td>red</td>*")
                {
                    $line = $line -replace '<td>red</td>','' 
                    $line = $line -replace '<tr>','<tr style="background-color:Tomato;">'
                }
            ## Formating all the Yellow Rows
            elseif ($line -like "*<td>yellow</td>*")
                {
                    $line = $line -replace '<td>yellow</td>','' 
                    $line = $line -replace '<tr>','<tr style="background-color:Orange;">'
                }
            ## Formating all the Green Rows
            elseif ($line -like "*<td>green</td>*")
                {
                    $line = $line -replace '<td>green</td>','' 
                    $line = $line -replace '<tr>','<tr style="background-color:MediumSeaGreen;">'
                }
            ## Building the new HTML file
                $newbody += $line
        }
        return $newbody
    }

    $date = (get-date -Format d/M/yyyy)
    $header =@"
     <Title>Snapshot Report - $date</Title>
    <style>
    body {   font-family: 'Helvetica Neue', Helvetica, Arial;
             font-size: 14px;
             line-height: 20px;
             font-weight: 400;
             color: black;
        }
    table{
      margin: auto;
      width: 100%;
      box-shadow: 0 1px 3px rgba(0,0,0,0.2);
      display: table;
      border-collapse: collapse;
      border: 1px solid black;
      text-align: center;

    }
    th {
        font-weight: 900;
        color: #ffffff;
        background: black;
        text-align: center;
        vertical-align: middle;
        border-style: ridge;
       }
    td {
        border-bottom: 1px solid black
        }
    </style>
    "@

    $PreContent = "<H1> VM Snapshots Report for " + $date + ".</H1>"
    $Footer = "<h3> Please check and investigate accordingly </h3>"
    $html = $Snapshots | select VM,Name,@{Label="Size";Expression={Get-SnapshotSize($_)}},Created,@{Label="Format";Expression={Get-SnapshotDateStyle($_)}}| sort Created -Descending | ConvertTo-Html -Head $header -PreContent $PreContent

    $Report = Format-HTMLBody ($html)


    $MailParam = @{ 
     To = "recepient" 
     From = "sender"
     SmtpServer = "smtpserver"
     Subject = "Daily Snapshot Report"
     body = ([string]$Report) 

    }

    Send-MailMessage @MailParam -BodyAsHtml
    Disconnect-VIServer -Confirm:$false

    Thursday, August 15, 2019 5:27 AM

All replies