PowerCLI – checking for snapshots on VMs and emailing the report back

Checking for any snapshots running on VMs in various clusters can be quite repetitive if done manually, looking through vCenter at each of your VMs. In the clusters I work with there are a LOT of VMs to check, and naturally I wanted to automate this process. Sure, I could rely on the vCenter alarms for snapshot size warning, but these are not completely reliable, as they only alert me when snapshots start growing large in size. I wanted something that would alert me to the presence of a snapshot regardless of its size. I therefore set about learning the basics of PowerCLI (as you can see in my last post) and searched around for some sample cmdlets that would help me retrieve a list of VMs with snapshots on them.

 

So here is the end result of running this snapshot checking script. It uses powershell cmdlets to generate an HTML email and sends it across to the address you specify. You will of course need to ensure you can connect out on port 25 for mail and have authentication on your mail server (or being sending from and to a domain hosted on your mail server (i.e. connecting to relay mail internally). Enter your mail server, to, and from details in the script to customise it. You’ll also need to authenticate with your vCenter server before running the script of course – you could use a cmdlet in the script to do this automatically. I have just been manually authenticating for now as I have not yet deployed this in production and have just been testing.

 

 

So here is the all important PowerCLI script!

 

#These are the properties assigned to the HTML table via the ConvertTo-HTML cmdlet - this is used to liven up the report and make it a bit easier on the eyes!

$tableProperties = "<style>"
$tableProperties = $tableProperties + "TABLE{border-width: 1px;border-style: solid;border-color: black;}"
$tableProperties = $tableProperties + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;}"
$tableProperties = $tableProperties + "TD{text-align:center;border-width: 1px;padding: 5px;border-style: solid;border-color: black;}"
$tableProperties = $tableProperties + "</style>"

# Main section of check
Write-Host "Looking for snapshots"
$date = get-date
$datefile = get-date -uformat '%m-%d-%Y-%H%M%S'
$filename = "F:\VMwareSnapshots_" + $datefile + ".htm"

#Get your list of VMs, look for snapshots. In larger environments, this may take some time as the Get-VM cmdlet is not very quick.
$ss = Get-vm | Get-Snapshot
Write-Host "   Complete" -ForegroundColor Green
Write-Host "Generating snapshot report"
$ss | Select-Object vm, name, description, powerstate | ConvertTo-HTML -head $tableProperties -body "<th><font style = `"color:#FFFFFF`"><big> Snapshots Report (the following VMs currently have snapshots on!)</big></font></th> <br></br> <style type=""text/css""> body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;} ol{margin:0;padding: 0 1.5em;} table{color:#FFF;background:#C00;border-collapse:collapse;width:647px;border:5px solid #900;} thead{} thead th{padding:1em 1em .5em;border-bottom:1px dotted #FFF;font-size:120%;text-align:left;} thead tr{} td{padding:.5em 1em;} tbody tr.odd td{background:transparent url(tr_bg.png) repeat top left;} tfoot{} tfoot td{padding-bottom:1.5em;} tfoot tr{} * html tr.odd td{background:#C00;filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='tr_bg.png', sizingMethod='scale');} #middle{background-color:#900;} </style> <body BGCOLOR=""#333333""> <table border=""1"" cellpadding=""5""> <table> <tbody> </tbody> </table> </body>" | Out-File $filename
Write-Host "   Complete" -ForegroundColor Green
Write-Host "Your snapshot report has been saved to:" $filename

# Create mail message

$server = "yourmailserveraddress.com"
$port = 25
$to      = "youremailaddress"
$from    = "youremailaddress"
$subject = "vCenter Snapshot Report"

$message = New-Object system.net.mail.MailMessage $from, $to, $subject, $body

#Create SMTP client
$client = New-Object system.Net.Mail.SmtpClient $server, $port
# Credentials are necessary if the server requires the client # to authenticate before it will send e-mail on the client's behalf.
$client.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials

# Try to send the message

try {
    # Convert body to HTML
    $message.IsBodyHTML = $true
    $attachment = new-object Net.Mail.Attachment($filename)
    $message.attachments.add($attachment)
    # Send message
    $client.Send($message)
    "Message sent successfully"

}

# Catch an error

catch {

	"Exception caught in CreateTestMessage1(): "

}

 

Another point worth mentioning – you should change the path that the report is saved to on disk – in my script it is set to F:\, so just modify this to suit your environment. Kudos to Andrew at winception for his Snapshot checking code – I have used a lot of it above, but modified it somewhat to include additional information, and style the HTML table so that it is much easier on the eyes. I also added the e-mail functionality to the script. The following is a screenshot after I executed the script in PowerCLI manually. You would of course look to automate the process by scheduling this script in on your machine.

 

 

Enjoy, and please drop any comments, improvements or feedback in the comments section!

Auto BCC emails to anyone in Salesforce

This is a bit of a different post today. Relevant if you use Salesforce to send out emails (which I do often in my day to day job). It also has some other great uses too – auto filling text fields, checkboxes / radio buttons on websites.

What you’ll need to get it working is to either be using Google Chrome or Mozilla Firefox as your browser. Grab the extension or addon called “Autofill” by thdoan. Here is a direct link to the Chrome version:
https://chrome.google.com/extensions/detail/nlmmgnhgdeffjkdckmikfpnddkbbfkkk. Once that is installed in your browser, you’ll need to figure out the “id” of the text field that you want auto filled. In my case I wanted to automatically fill out an email address field in my Salesforce emails. I started a new email, then right-clicked the page to select “Get Page Source”. After some searching through the source of the page, I managed to find an “id” of value “p5” for the BCC email address textfield. To help, I just searched the source for “bcc” to start with to get to that section of the source. Once you have the id of the control or field you want to autofill, it is quite easy to set up the rest.

Open the extension / addon settings, select the button for a new rule (the little plus sign). In the new rule “Type” dropdown, select “Text” for a text field. The name will be your “id” you have located. So for my BCC field, I entered a name of “p5”. The “value” will be the text I want to populate this field automatically. So I entered the email address as the value. You can leave the “site” field blank if you want, but this restricts the rule to whatever you specify here. In my case I entered eu1.salesforce.com so that this rule would only ever apply to URLs I am working with that work on this domain / site. This is quite useful, as if I had to ever come across another site with a text field id value of “p5” it would suddenly populate with the email address I specified as a value – so in my case it is useful to specify a site. You can also use Regular Expressions in this field for more flexibility. Once you are done, just click “Save” at the bottom.

The next time you load up a page with that textfield id, autofill should work its magic and fill it in with you. You don’t even need to click the field. There are tons of other uses that this addon / extension can provide, so have a look at the default options that are provided to get some ideas.

How to set e-mail disclaimers using Sophos Puremessage for outgoing mail.

This is a short how-to for setting up a disclaimer to be appended to any outgoing exchange mail.

The version of the Sophos Puremessage admin console that I will be using is Version: 3.0.1.0.

First of all open up the Puremessage administration console.

Expand “Configuration” then expand “Transport (SMTP) Scanning policy” Now click on the “Disclaimers” item.

sophos-admin-console-1

Click the dropdown menu and select “Add disclaimer”. A link with the name “Text” will now appear. Click on this and type in your disclaimer text – you will need to type your disclaimer in the text and HTML area, then click “OK”

sophos-add-disclaimer

Also make sure that the disclaimer is turned on. When it is enabled, the ON status will show near the top right of the console. If the OFF status is currently showing in orange, then that means that the disclaimer rule is off.