Hello all,
I have been using the script from this website with great success.
http://vnucleus.com/2011/07/powercli-script-to-alert-on-snapshots-in-vsphere/
What I am trying to figure out, it how to modify the script so it will send out a report even when there are no snapshots on the VMs?
Can someone please let me know what I would need to do?
Thanks
Ray
Some <CR><LF> got lost during the copy/paste it seems.
I corrected the lines above, please try again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
With this test
if($VMsWithSnaps -ne $null){
the part of the script that sends out the email is only executed when there are snapshots present.
If you add an
else {....}
block on that If statement, you can place code in that block that is fired when there are no snapshots.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So does this make sense, i added in the info listed in red.
Connect-VIServer -server $vcenterserver
$date = Get-Date
$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb)
# Make sure the body isn't empty. If it is, skip this part and exit the script.
if($VMsWithSnaps -ne $null)Else{
Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: NO VMware Snapshots found on msf1vvcs01p" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml{
$body = @("
<center><table border=1 width=50% cellspacing=0 cellpadding=8 bgcolor=Black cols=3>
<tr bgcolor=White><td>Virtual Machine</td><td>Snapshot</td><td>Size in MB</td><td>Creation Time</td></tr>")
$i = 0
do {
if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td><td>$($VMsWithSnaps[$i].Created)</td></tr>";$i++}
else {$body += "<tr bgcolor=#EFEFEF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td><td>$($VMsWithSnaps[$i].Created)</td></tr>";$i++}
}
while ($VMsWithSnaps[$i] -ne $null)
$body += "</table></center>"
# Send email alerting recipients about snapshots.
if($attachmentPref){
$VMsWithSnaps | Export-CSV "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"
Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: VMware Snapshots found on msf1vvcs01p" -Body "$body" -Attachments "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv" -SmtpServer "$smtpsrv" -BodyAsHtml
Remove-Item "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"
}
Else{
Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: VMware Snapshots found on msf1vvcs01p" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml
}
}
Disconnect-VIServer -Server $vcenterserver -Confirm:$false
exit
No, I meant like this
Connect-VIServer -server $vcenterserver $date = Get-Date $VMsWithSnaps = @(Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb) # Make sure the body isn't empty. If it is, skip this part and exit the script. if($VMsWithSnaps -ne $null){ $body = @("
<center><table border=1 width=50 % cellspacing=0 cellpadding=8 bgcolor=Black cols=3>
<tr bgcolor=White><td>Virtual Machine</td><td>Snapshot</td><td>Size in MB</td><td>Creation Time</td></tr>") $i = 0 do { if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td><td>$($VMsWithSnaps[$i].Created)</td></tr>";$i++} else {$body += "<tr bgcolor=#EFEFEF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td><td>$($VMsWithSnaps[$i].Created)</td></tr>";$i++} } while ($VMsWithSnaps[$i] -ne $null) $body += "</table></center>" # Send email alerting recipients about snapshots. if($attachmentPref){ $VMsWithSnaps | Export-CSV "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"
Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: VMware Snapshots found on msf1vvcs01p" -Body "$body" -Attachments "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv" -SmtpServer "$smtpsrv" -BodyAsHtml
Remove-Item "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"
} Else{ Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: VMware Snapshots found on msf1vvcs01p" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml
} } else{ Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: NO VMware Snapshots found on msf1vvcs01p" -Body "" -SmtpServer "$smtpsrv"
} Disconnect-VIServer -Server $vcenterserver -Confirm:$false exit
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi there,
I copied and paste the info into the script when i go to the run the script I get this error.
Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "Send-MailMessage" to type "System.Char". Error: "String must be exactly one character long."
At C:\Users\ray00055\Desktop\Snapshots-msf1vvcs01p-2.ps1:34 char:90
+ ... date.year).csv"Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNIN ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand
Remove-Item : Cannot find path 'C:\Users\ray00055\Desktop\SnapshotReport 2-27-2013.csv' because it does not exist.
At C:\Users\ray00055\Desktop\Snapshots-msf1vvcs01p-2.ps1:35 char:1
+ Remove-Item "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\ray000...t 2-27-2013.csv:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Connect-VIServer -server $vcenterserver
$date = Get-Date
$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select VM,Name,Created,sizemb)
# Make sure the body isn't empty. If it is, skip this part and exit the script.
if($VMsWithSnaps -ne $null){
$body = @("
<center><table border=1 width=50 % cellspacing=0 cellpadding=8 bgcolor=Black cols=3>
<tr bgcolor=White><td>Virtual Machine</td><td>Snapshot</td><td>Size in MB</td><td>Creation Time</td></tr>")
$i = 0
do {
if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td><td>$($VMsWithSnaps[$i].Created)</td></tr>";$i++}
else {$body += "<tr bgcolor=#EFEFEF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td><td>$($VMsWithSnaps[$i].Created)</td></tr>";$i++}
}
while ($VMsWithSnaps[$i] -ne $null)
$body += "</table></center>"
# Send email alerting recipients about snapshots.
if($attachmentPref){
$VMsWithSnaps | Export-CSV "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: VMware Snapshots found on msf1vvcs01p" -Body "$body" -Attachments "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv" -SmtpServer "$smtpsrv" -BodyAsHtml
Remove-Item "SnapshotReport $($date.month)-$($date.day)-$($date.year).csv"
}
Else{
Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: VMware Snapshots found on msf1vvcs01p" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml
}
}
Else{
Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: NO VMware Snapshots found on msf1vvcs01p" -Body "" -SmtpServer "$smtpsrv"
}
Disconnect-VIServer -Server $vcenterserver -Confirm:$false
exit
Some <CR><LF> got lost during the copy/paste it seems.
I corrected the lines above, please try again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello there,
I was able to get this to work, I had to fix some issues with <CR><LF> when I pasted it as well. One thing I did find, it did not like the body to be blank when it goes to send the email saying No Snapshots found, so I removed the following section (-Body "") and once I did that it ran without any problems.
Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "WARNING: NO VMware Snapshots found on msf1vvcs01p" -SmtpServer "$smtpsrv"
Thanks for your help.
Ray
Thank you LucD and Ray,
I used this script and it works except I get two rows/entries per snapshot in the table. Is anyone else seeing that?
and If it helps, loading powerCLI snap-ins with some error checks by adding this to code:
If (-not (Get-PSSnapin VMware.VimAutomation.Core))
{ Try { Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop }
Catch { Write-Host "Unable to load PowerCLI, is it installed?" -ForegroundColor Red; Break }
}
* This I borrowed from this guy:
http://thesurlyadmin.com/2013/03/18/report-all-snapshots-in-your-vmware-environment/
Hello there,
Can you please paste the output of the the two rows/entries you are seeing? I had an issue before so I want to see if it is the same thing I was running into.
Thanks
Ray
Virtual Machine | Snapshot | Size in MB | Creation Time |
WSUS01 | Test1 | 4138.39 | 04/16/2013 15:29:52 |
VC01 | Test1 | 10421.57 | 04/16/2013 15:25:12 |
WSUS01 | Test1 | 4138.39 | 04/16/2013 15:29:52 |
VC01 | Test1 | 10421.57 | 04/16/2013 15:25:12 |
This is what I get - two entries per snap. How did you fix this?
Could it be that you 2 open connections to the vCenter and are running in multi mode ?
Do a
$defaultviservers
Get-PowerCLIConfiguration
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are right (see below). I would launch PowerCLI, login manually and then run the script using '.\ScriptName.ps1' (eventually going to run as scheduled task) and the script itself logs in again.
[vSphere PowerCLI] C:\support> $defaultviservers
Name Port User
---- ---- ----
VC01 443 badmin
[vSphere PowerCLI] C:\support> Get-PowerCLIConfiguration
Proxy Policy Default Server
Mode
------------ ---------------
UseSystemProxy Multiple
THanks for the link! I have a VM snapshot script too if anyone wants to take a look at it, as well as a sceenshot generator.