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.

Advertisement

About Robert van den Nieuwendijk
Robert van den Nieuwendijk is a freelance senior systems engineer with over 30 years of experience in the IT industry. He focusses on VMware vCloud Suite and Microsoft Windows Server. He tries to automate as much of his work as possible using Microsoft PowerShell. Robert is the author of the books “Learning PowerCLI” and “Learning PowerCLI – Second Edition.” 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 2016, VTSP 2016, VCP4-DCV, VCP5-DCV, VCP6-DCV, VCP6-CMA, VCA-Cloud, VCA-WM, VCA-NV, VMSP, VMTSP, ZCS, ZCP, ZCP-Cloud, MCSE, MCSA, MCP, MCP+I, PRINCE2 Foundation and ITIL Foundation. In 2012, 2013, 2014, 2015, 2016, 2017, 2018 and 2019 Robert received the VMware vExpert award for his contribution to the community of VMware users over the past year. In 2017 Robert also received the VMware vExpert Cloud award. PernixData made him in 2015 a member of the PernixPro.

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 )

Connecting to %s

%d bloggers like this: