Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

List VM Snapshots email

Posted on 2012-03-26
4
Medium Priority
?
669 Views
Last Modified: 2012-03-26
Ive got the below script, which compiles an email of all our VMs that have snapshots and sends that out to a selected user.

The script works well but id like to exclude selected VMs by using a wildcard and part of their name and wonder how i can do that?

 
#############################################################################
# List all VM with snapshots
# Run as scheduled task with a user with rights to view all VMs
# 									

Add-PSSnapin VMware.VimAutomation.Core

# HTML/CSS style for the output file
$head = "<style>"
$head = $head + "BODY{background-color:white;}"
$head = $head + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$head = $head + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}"
$head = $head + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
$head = $head + "</style>"

# SMTP info
$smtpServer = "smtp.domain.com" 
$strFrom = "VMSnapshots@domain.com"
$strTo = "VM-Admins@domain.com"
$strSubject = “Snapshot list - ” + (get-date -DisplayHint date)
$strBody = "Attached is the list of Snapshots"
$strMail = "<H2><u>" + $strSubject + "</u></H2>"

# List your vCenter servers in quotes separated by commas
$Servers="vCenterServer1","vCenterServer2"
foreach ($Server in $Servers){
	Connect-VIServer $Server
	$date=Get-Date -uFormat "%Y%m%d%H%M%S"
	$strOutFile = "C:\Scripting\Output\snapshot_list$date.htm"
	$strSubject = “Snapshot list $Server - ” + (get-date -DisplayHint date)

	# Get the list of VM's
	$vms = Get-VM

	$myCol = @()
	ForEach ($vm in $vms){
	$snapshots = Get-SnapShot -VM $vm
	if ($snapshots.Name.Length -ige 1 -or $snapshots.length){
		ForEach ($snapshot in $snapshots){
			$myObj = "" | Select-Object VM, Snapshot, Created, Description
			$myObj.VM = $vm.name
			$myObj.Snapshot = $snapshot.name
			$myObj.Created = $snapshot.created
			$myObj.Description = $snapshot.description
			$myCol += $myObj
		}
	}
}

	# Write the output to an HTML file
	if ($myCol -ne $null){
 		$myCol | Sort-Object VM | ConvertTo-HTML -Head $head -Body $strMail | Out-File $strOutFile

	# Mail the output file
	$msg = new-object Net.Mail.MailMessage
	$att = new-object Net.Mail.Attachment($strOutFile)
	$smtp = new-object Net.Mail.SmtpClient($smtpServer)
	$msg.From = $strFrom
	$msg.To.Add($strTo)
	$msg.Subject = $strSubject
	$msg.IsBodyHtml = 1
	$msg.Body = Get-Content $strOutFile
	$msg.Attachments.Add($att)
	$msg.Headers.Add("message-id", "<3BD50098E401463AA228377848493927-1>")	# Adding a Bell Icon for Outlook users

	$smtp.Send($msg)}
	DisConnect-VIServer -Server * -Force -Confirm:$false
}

Open in new window

0
Comment
Question by:Colchester_Institute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
papalala earned 2000 total points
ID: 37766394
instead of line 33 ($vms = Get-VM) use following:

$vms = Get-VM | where {$_.Name -notmatch "your_exclude"}
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 37766544
yep that seems to work....Is there a way i can put more than one exclusion?
0
 
LVL 2

Assisted Solution

by:papalala
papalala earned 2000 total points
ID: 37766583
Just use pipe to add more exclusions.

 Get-VM | where {$_.Name -notmatch "excl1"} | where {$_.Name -notmatch "excl2"} | where...
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 37766657
yep thats worked i should have guessed that second part really! Many thanks
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When rebooting a vCenters 6.0 and try to connect using vSphere Client we get this issue "Invalid URL: The hostname could not parsed." When we get this error we need to do some changes in the vCenter advanced settings to fix the issue.
Ransomware is a malware that is again in the list of security  concerns. Not only for companies, but also for Government security and  even at personal use. IT departments should be aware and have the right  knowledge to how to fight it.
Teach the user how to edit .vmx files to add advanced configuration options Open vSphere Web Client: Edit Settings for a VM: Choose VM Options -> Advanced: Add Configuration Parameters:
Teach the user how to use configure the vCenter Server storage filters Open vSphere Web Client:  Navigate to vCenter Server Advanced Settings: Add the four vCenter Server storage filters: Review the advanced settings: Modify the values of the four v…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question