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)}
Since you didn't say where you had a problem, I can give you this minimal script that sends the report (not as HTML) in the body of the email.
$report = Get-VM | Get-Snapshot | where { $_.Created -lt (Get-Date).AddDays(-15)} | select VM, Name, Created $emailFrom = <from-email-addr> $emailTo = <to-email-addr> $subject = "Snapshots older then 15 days" $body = $report | Out-String $smtpServer = <smtp-server> $smtp = new-object Net.Mail.SmtpClient($smtpServer) $smtp.Send($emailFrom, $emailTo, $subject, $body)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for your response
Issue is need to the top bold portion to the lower bold portion but this doesnt work? Returns columu Date Created is @{Name=; Created=} hence the code I added is not correct.
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
$Snap.Created = { $_.Created -lt (Get-Date).AddDays(-30)} | select Name, Created
$Snap.Description = $_.description
$Snap.Host = $_.vm.host.name
$Report += $Snap
}
}
This should do the trick.
Try this one, if you get errors include the error messages in you reply.
@" =============================================================================== 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 {background: #4A8AC6;color: #FFFFFF;text-align:center;font-weight: bold;}.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 = "esx1","esx2" #Change 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 "myVC"){Connect-VIServer $server} # Use specific login details for the rest of servers in $serverlist else {Connect-VIServer $server -user $username -password $password} Get-VM | Get-Snapshot | where { $_.Created -lt (Get-Date).AddDays(-15)} | %{ $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 } } # 1. 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 = "smtp.server" #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@site2.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)}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
99% there....(thanks)
Only issue is the Date Created returns True rather than a creation date?
VMware Snaphot Report
VM Name Snapshot Name Date Created Description Host
SVRHOMGMT Created by vReplicator (do NOT modify or delete!) True Application: vReplicator; Version: 3.0.0 (build 13184); Time: 16/12/2009 12:34:05 a.m.; Machine: SVRDC; User: SYSTEM; esx01
I see, didn't check that part of the code.
Change this line
$Snap.Created = $_.created -lt (Get-Date).AddDays(-15)
by this line
$Snap.Created = $_.created
and you should see the actual date of the snapshot.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Fantastic it now works well.
Reson for this is to ensure we have no old snapshots from vReplicator running.
I'm having a problem searching isolated esxi hosts for snapshots in this script. I can scan any hosts that are connected to the vcenter, but I have a couple of esxi hosts that are not managed by vcenter. The root credentials are the same in both enviroments, but when I list the isolated host in this script, it only authenicates to the vcenter, which just duplicates the same results. Any suggestions?
EX:
CL 06232010 SRV-EMPIRE12V before bes install 06172010 06/16/2010 13:11:37 requested Luke Bragg jpofitesx02.empiredistrict.com
CL 06232010 SRV-EMPIRE12V BES install complete 06/16/2010 21:45:48 After BES 5.0.1 maintenance release 3 install jpofitesx02.empiredistrict.com
SRV-EMPIRE19V 05182010 Prior to Roger's tool upgrades 05/18/2010 15:49:58 jpofitesx02.empiredistrict.com
SRV-EMPIRE19V 06142010 Roger requested to save work to this point 06/15/2010 19:06:48 Scott also requested this jpofitesx02.empiredistrict.com
EDE-90003 06042010 on network, before psoft install 06/04/2010 16:02:07 jpofitesx02.empiredistrict.com
SRV-EMPIRE12V Post MS updates, Luke will start BES migration 06/24/2010 09:56:17 06/24/2010 jpofitesx05.empiredistrict.com
SRV-EMPIRE17V Fresh server Handing over to Kristi 04232010 04/23/2010 16:14:44 jpofitesx02.empiredistrict.com
SRV-EMPIRE74V Base Install 06/04/2010 15:57:17 jpofitesx02.empiredistrict.com
CL 06232010 SRV-EMPIRE12V before bes install 06172010 06/16/2010 13:11:37 requested Luke Bragg jpofitesx02.empiredistrict.com
CL 06232010 SRV-EMPIRE12V BES install complete 06/16/2010 21:45:48 After BES 5.0.1 maintenance release 3 install jpofitesx02.empiredistrict.com
SRV-EMPIRE19V 05182010 Prior to Roger's tool upgrades 05/18/2010 15:49:58 jpofitesx02.empiredistrict.com
SRV-EMPIRE19V 06142010 Roger requested to save work to this point 06/15/2010 19:06:48 Scott also requested this jpofitesx02.empiredistrict.com
EDE-90003 06042010 on network, before psoft install 06/04/2010 16:02:07 jpofitesx02.empiredistrict.com
SRV-EMPIRE12V Post MS updates, Luke will start BES migration 06/24/2010 09:56:17 06/24/2010 jpofitesx05.empiredistrict.com
SRV-EMPIRE17V Fresh server Handing over to Kristi 04232010 04/23/2010 16:14:44 jpofitesx02.empiredistrict.com
SRV-EMPIRE74V Base Install 06/04/2010 15:57:17 jpofitesx02.empiredistrict.com