Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.
Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.

PowerShell – keep SMTP in environment variable

You probably use “Send-MailMessage” across many scripts.  I sure do.

Why repeat configuration in every script?   That’s a headache to update and manage long term.   I suggest keeping that configuration in Machine Environment Variables.   These save globally and are visible to any downstream code including:

  • PowerShell
  • Dot Net
  • IIS
  • Windows Service
  • Windows Form
  • .. and more

Below is an example using “$env:smtp” to send email from PowerShell.   Now all of your scripts are simpler and as you move PS1 files across environments (dev, test, acpt, prod) the configuration will follow naturally.   Cheers!  

shades_smile

 

Code

Send-MailMessage
 -To "admin@demo.com"
 -From "sharepoint-no-reply@demo.com"
 -Subject "hello world"
 -Body "hello world"
 -SmtpServer $env:smtp

 

Screenshot

image

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲