How to use the vCenter Server Settings from PowerCLI to send e-mail

PowerCLI logoIf you use PowerCLI to generate reports and send those reports to you via e-mail, you probably have the sender e-mail adress and the smtp server hard coded in your script. But what if one of those changes? You will have to modify all your scripts. Wouldn’t it be easier if you have stored them in one place, so you have to change them only once?

You can put the sender e-mail adress and the smtp server in the vCenter Server Mail Sender Settings.


Figure 1. vCenter Server Mail Sender Settings.

With PowerCLI you can retrieve the values from the vCenter Server Settings and use them in your scripts. If your smtp server address or your sender e-mail address changes, you have to change them only in the vCenter Server Settings and all your PowerCLI scripts remain working. Isn’t that nice?

The following PowerCLI script shows you how to retrieve the mail.sender and mail.smtp.server values from the vCenter Server Settings. And it uses the Send-MailMessage cmdlet to send you an overview of all your virtual machines.

$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings'
$MailSender = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.sender"}).Value
$MailSmtpServer = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.smtp.server"}).Value

$Report = Get-VM | Sort-Object -Property Name | Out-String
Send-MailMessage -from $MailSender -to "you@yourdomain.com" -subject "Sending the vSphere report" -body $Report -smtpServer $MailSmtpServer

Listing 1. PowerCLI script to retrieve the mail sender and smtp server from the vCenter Server Settings and send an e-mail using them.

About these ads

About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 25 years of experience in the IT industry. His main focus is VMware vSphere and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft Windows PowerShell. Robert is the author of the book “Learning PowerCLI”. Robert is a frequent contributor and moderator at the VMware VMTN Communities. He has a bachelor degree in software engineering and holds the following IT certifications and accreditations: VSP 5, VTSP 5, VCP4-DCV, VCP5-DCV, VCA-Cloud, VCA-WM, MCSE, MCSA, MCP, MCP+I, and ITIL Foundation. In 2012, 2013 and in 2014 Robert received the VMware vExpert title for his contribution to the community of VMware users over the past year.

3 Responses to How to use the vCenter Server Settings from PowerCLI to send e-mail

  1. LucD says:

    Nice trick.
    An alternative is to put the SMTP server in the $PSEmailServer variable in one of your profiles.
    Then you don’t need to use the -SmtpServer parameter on the Send-MailMessage cmdlet.

    Provided of course you only have 1 SMTP server in your environment.

  2. T. says:

    How come the script given works perfectly fine, but my triggered alarm won’t send the e-mail…??

  3. dan says:

    That Is cool idea

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 655 other followers

%d bloggers like this: