Hi
Trying to modifiy a script to email snapshots say older than 15 days.
Example code is Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-30)} | select Name, Created
need to write this into the script but cant get it right? Any advice would be great please.
@"
===============================================================================
Title: Get-VmwareSnaphots.ps1
Description: List snapshots on all VMWARE ESX/ESXi servers as well as VM's managed by Virtual Center.
Requirements: Windows Powershell and the VI Toolkit
Usage: .\Get-VmwareSnaphots.ps1
Author: Modded
===============================================================================
"@
#Global Functions
#This function generates a nice HTML output that uses CSS for style formatting.
function Generate-Report {
Write-Output "<html><head><title></title><style type=""text/css"">.Error {color:#FF0000;font-weight: bold;}.Title .Normal {}</style></head><body><table><trTitle""><td colspan=""5"">VMware Snaphot Report</td></tr><tr class="Title"><td>VM Name </td><td>Snapshot Name </td><td>Date Created </td><td>Description </td><td>Host </td></tr>"
Foreach ($snapshot in $report){
Write-Output "<td>$($snapshot.vm)</td><td>$($snapshot.name)</td><td>$($snapshot.created)</td><td>$($snapshot.description)</td><td>$($snapshot.host)</td></tr> "
}
Write-Output "</table></body></html>"
}
#Login details for standalone ESXi servers
$username = 'root'
$password = 'password' #Change to the root password you set for you ESXi server
#List of servers including Virtual Center Server. The account this script will run as will need at least Read-Only access to Cirtual Center
$ServerList = "esx01" #Chance to DNS Names/IP addresses of your ESXi servers or Virtual Center Server
#Initialise Array
$Report = @()
#Get snapshots from all servers
foreach ($server in $serverlist){
Check is server is a Virtual Center Server and connect with current user
if ($server -eq "VCServer"){Connect-VIServer $server}
Use specific login details for the rest of servers in $serverlist
else {Connect-VIServer $server -user $username -password $password}
example Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-30)} | select Name, Created*
get-vm | get-snapshot | %{
$Snap = {} | Select VM,Name,Created,Description,Host
$Snap.VM = $_.vm.name
$Snap.Name = $_.name
$Snap.Created = $_.created -lt (Get-Date).AddDays(-15)
$Snap.Description = $_.description
$Snap.Host = $_.vm.host.name
$Report += $Snap
}
}
Generate the report and email it as a HTML body of an email
Generate-Report > "VmwareSnapshots.html"
IF ($Report -ne ""){
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "mail.local.com" #Change to a SMTP server in your environment
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "nobody@site2.com" #Change to email address you want emails to be coming from
$MailMessage.To.add("nobody@site.com") #Change to email address you would like to receive emails.
$MailMessage.IsBodyHtml = 1
$MailMessage.Subject = "Vmware Snapshots"
$MailMessage.Body = Generate-Report
$SmtpClient.Send($MailMessage)}