VMware Cloud Community
aaronkeller
Contributor
Contributor
Jump to solution

PowerCLI snapshot report to exclude a datacenter

We have a PowerCLI script we've adapted to email us daily with a list of snapshots in our environment, but we'd like to exclude our datacenter that hosts our VDI as there are always snapshots present there that we don't want to be notified about. I'm having a hard time figuring out how to do this, so any help would be appreciated!

 

The text of the script is below.

<#

#>
param(
$VirtualCenter = "vcentername",
$smtpServer = "mailserver",
$smtpFrom = "email@ourdomain.com",
$smtpTo = "email@ourdomain.com",
$smtpSubject = "VMware Snapshots",
$SnapShotsOlderThanXDays = 0
)

Get-Module -ListAvailable VMware.VimAutomation.* | Import-Module -ErrorAction SilentlyContinue
If ($global:DefaultVIServer) {
Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue
}
$VCServer = Connect-VIServer -Server $VirtualCenter

 


$VmsWithAllowedSnaps = @(".*SnappyImage.*")
$LogEntriesPerVM = 4000

$VMs = Get-VM
Foreach ($VmsWithAllowedSnap in $VmsWithAllowedSnaps) {
$VMs = $VMs | Where {$_.Name -notmatch $VmsWithAllowedSnap}
}
$SnapShots = $VMs | Get-Snapshot

$date = Get-Date
$measure = Measure-Command {
$report = $Snapshots | Select-Object VM, Name, @{Name="User"; Expression = { (Get-VIEvent -Entity $_.VM -MaxSamples $LogEntriesPerVM -Start $_.Created.AddSeconds(-10) | Where {$_.Info.DescriptionId -eq "VirtualMachine.createSnapshot"} | Sort-Object CreatedTime | Select-Object -First 1).UserName}}, Created, @{Name="Days Old"; E={$_.Created - 2}}, Description | Sort-Object -Property "Created"
}
#($measure).TotalMinutes

$report = $report | Where {($_.Created).AddDays([int]$SnapShotsOlderThanXDays) -lt (Get-Date)}

 


$head = @"
<title>Snapshot Daily/Weekly Report</title>
<style type="text/css">
body { background-color: white; }
table { border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse; }
th {border-width: 1px; padding: 0px; border-style: solid; border-color: black; background-color:thistle }
td {border-width: 1px ;padding: 0px; border-style: solid; border-color: black; }
tr:nth-child(odd) { background-color:#d3d3d3; }
tr:nth-child(even) { background-color:white; }
</style>
"@

$postContent = @"
<p>Number of Snapshots: $($report.count)</p>
<p>Generated on $($ENV:COMPUTERNAME)</p>
"@

#Send Email Report
$date = Get-Date
$message = New-Object System.Net.Mail.MailMessage $smtpFrom, $smtpTo
$message.Subject = $smtpSubject
$message.IsBodyHTML = $true

$SnapshotReportHTML = $report | ConvertTo-Html -Head $head -PreContent "Report Date: $date" -PostContent $PostContent
$message.Body = $SnapshotReportHTML | Out-String
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

If you want to exclude multiple Datacenters, you could do

$excludedDC = 'DC1','DC2','DC3'

$VMs = Get-Datacenter | Where-Object { $_.Name -notin $excludedDC } | Get-VM


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

Change the line with the Get-VM to something like this

$excludedDC = 'VDIDC'
$VMs = Get-Datacenter | Where-Object { $_.Name -ne $excludedDC } | Get-VM


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LucD
Leadership
Leadership
Jump to solution

If you want to exclude multiple Datacenters, you could do

$excludedDC = 'DC1','DC2','DC3'

$VMs = Get-Datacenter | Where-Object { $_.Name -notin $excludedDC } | Get-VM


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

aaronkeller
Contributor
Contributor
Jump to solution

Thank you so much for your help! I actually misspoke in my question. I intended to say 'datastore' instead of 'datacenter', but I used "Get-Datastore" and it works! That was exactly what I needed.

Thanks again!

0 Kudos