Hi Team,

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Keshav Prasad Keshav Prasad 1 month ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #51085
    Profile photo of Keshav Prasad
    Keshav Prasad
    Participant
     # PowerShell script to check for VM snapshots and send Email report
    
    add-pssnapin VMware.VimAutomation.Core
    Connect-VIServer -Server 'vcenter' -User 'domain\user' -Password 'password'
    
    # HTML formatting
    $a = ""
    $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 + ""
    
    # 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 = "x:\dir\Snaps_" + $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
    Write-Host "   Complete" -ForegroundColor Green
    Write-Host "Generating VM snapshot report"
    #$ss | Select-Object vm, name, description, powerstate | ConvertTo-HTML -head $a -body "VM Snapshot Report"| Out-File $filename
    $ss | Select-Object vm, name, description | ConvertTo-HTML -head $a -body "VM Snapshot Report"| Out-File $filename
    Write-Host "   Complete" -ForegroundColor Green
    Write-Host "Your snapshot report has been saved to:" $filename
    
    # Create mail message
    $server = "smtp.domain.com"
    $port = 25
    $to      = "[email protected]"
    $from    = "[email protected]"
    $subject = "VM Snapshot Report"
    $body = Get-Content $filename
    
    $message = New-Object system.net.mail.MailMessage $from, $to, $subject, $body
    
    # Create SMTP client
    $client = New-Object system.Net.Mail.SmtpClient $server, $port
    # Credentials are necessary if the server requires the client # to authenticate before it will send e-mail on the client's behalf.
    $client.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials
    
    # Try to send the message
    
    try {
       
    # Convert body to HTML
        $message.IsBodyHTML = $true
    # Uncomment these lines if you want to attach the html file to the email message
    #   $attachment = new-object Net.Mail.Attachment($filename)
    #   $message.attachments.add($attachment)
       
    # Send message
        $client.Send($message)
        "Message sent successfully"
    
    }
    
    #Catch error
    
    catch {
    
        "Exception caught in CreateTestMessage1(): "
    
    } 
    #51089
    Profile photo of Keshav Prasad
    Keshav Prasad
    Participant

    Hi Team,

    I need to edit above script to highlight older the 90Days snapshots.
    can you help me to add the code in above script.

    Thanks in Advance
    Keshava Prasad

    #51096
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    I think you only need to change line 22 from

    $ss = Get-vm | Get-Snapshot
    

    to

    $ss = Get-vm | Get-Snapshot | Where-Object { $_.Created -lt (Get-Date).AddDays(-90) }
    

    for the expected result.

    I hope above works for you because I can't test it due to a lack of access to a VMware environment at the moment.

    Cheers
    Daniel

    #51102
    Profile photo of Keshav Prasad
    Keshav Prasad
    Participant

    Thank You Krebs, I will check and update you.

    #51104
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    No problem. Please call me Daniel instead 🙂

    #51142
    Profile photo of Edmond Yee
    Edmond Yee
    Participant

    Daniel is correct. That should be all that needs to be modified.

    #51187
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Here is the script refactored to a bit more simple:

    Add-PSSnapin VMware.VimAutomation.Core
    Connect-VIServer -Server 'vcenter' -User 'domain\user' -Password 'password'
    
    $css = @"
    
        BODY { 
            background-color:white;
        }
    
        TABLE {
            border-width: 1px;
            border-style: solid;
            border-color: black;
            border-collapse: collapse;
        }
        
        TH {
            border-width: 1px;
            padding: 5px;
            border-style: solid;
            border-color: black;
            foreground-color: black;
            background-color: LightBlue;
        }
    
        TD {
            border-width: 1px;
            padding: 5px;
            border-style: solid;
            border-color: black;
            foreground-color: black;
            background-color: white;
        }
    
    "@
    
    
    $htmlBody = Get-VM | 
          Get-Snapshot |
          Where-Object { $_.Created -lt (Get-Date).AddDays(-90) } |
          Select-Object vm, name, description | 
          ConvertTo-HTML -head $css -body "VM Snapshot Report"
    
    
    $mailParams = @{
        To = "[email protected]"
        From = "[email protected]"
        Subject = "VM Snapshot Report"
        Body = $htmlBody
        BodyAsHtml = $true
        SmtpServer = "smtp.domain.com"
        Port = 25
    }
    
    Send-MailMessage @mailParams
    

    Edit: Forum is eating some of the HTML. There needs to be STYLE tags in the CSS and H1 tags around the -Body statement

    • This reply was modified 1 month, 1 week ago by Profile photo of Rob Simmers Rob Simmers.
    #51418
    Profile photo of Keshav Prasad
    Keshav Prasad
    Participant

    Thank You Daniel, Edmond, Rob.
    one more, I need to receive html report as attachment in mail.

    #51420
    Profile photo of Keshav Prasad
    Keshav Prasad
    Participant

    Got It All Working Fine.

    Thank You For Your Help.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.