Please enable Javascript for better experience...
Fetching list of snapshot older than 5 days and sending it on email
By admin | Jul 2, 2017 | In Articles | Total Views [ 129 ]
(0 Like)
Rate

Fetching list of snapshot older than 5 days and sending it on email

 

# PowerShell script to check for VM snapshots and send Email report

 

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer -Server vcenter.vmwarediary.com' -User admin@vmwarediary.com' -Password 'abc1234'

 

# 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 VMs for snapshots"

$date = get-date

$datefile = get-date -uformat '%m-%d-%Y-%H%M%S'

if(-not (test-path "C:\test")) {

    md "C:\test" | out-null

} else {

    write-host "$dirName already exists!"

}

$filename = "C:\test\vmwarediary_" + $datefile + ".csv"

 

 

# Get list of VMs with snapshots

# Note:  It may take some time for the  Get-VM cmdlet to enumerate VMs in larger environments

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-5)} | Select-Object VM, Name, Created, SizeMB | sort-object SizeMB -descending | export-csv $filename

Disconnect-VIServer -Server * -Confirm:$false

Write-Host "   Complete" -ForegroundColor Green

Write-Host "Generating VM snapshot report"

Write-Host "   Complete" -ForegroundColor Green

Write-Host "Your snapshot report has been saved to:" $filename

 

 

# Create mail message

$file = $filename

$server = "smtp.vmwarediary.com"

$port = 25

$to      = "Jitendra.singh@vmwarediary.com"

$from    = "admin@vmwarediary.com"

$subject = "VM Snapshot Report"

$body = "Hello All <br><br>"

$body += "Greetings for the Day <br><br>"

$body += "Find the attached snapshot reports for ALL Vcenters <br><br>"

$body += "Below are the Scripts error logs <br><br><br<br>"

$body += "Best Regards <br>"

$body += "Jitendra Kumar Singh <br>"

$body += "Jitendra.singh@vmwarediary.com <br><br>"

 

$message = New-Object system.net.mail.MailMessage $from, $to, $subject, $body

$filename | foreach-object{

  $Attachment = New-Object System.Net.Mail.Attachment $_

  $message.Attachments.Add($Attachment)

}

 

# 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($file)

#   $message.attachments.add($attachment)

  

# Send message

    $client.Send($message)

    "Message sent successfully"

 

}

 

#Catch error

 

catch {

 

    "Exception caught in CreateTestMessage1(): "

 

}

Write back to us for any query or errors to be sorted out.

 

Share this

About the Author

admin
admin
Contributer VMwareDiary.com

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment