Quantcast
Channel: Practical 365
Viewing all articles
Browse latest Browse all 515

Using an XML Settings File for PowerShell Scripts

$
0
0

One of the most common feedback items for PowerShell scripts that I’ve published, such as Test-ExchangeServerHealth.ps1, is that having to edit the script to insert the correct email settings is a pain. The other is that the scripts are unsigned, requiring the execution policy of the machine running the script to be relaxed.

These two things go hand in hand. I don’t sign the scripts because they always need editing to work in your environment. And I include the settings that need editing in the script, instead of making them parameters, because I want the script to be easy to run (a lot of parameters seems to increase the number of “How do I…?” questions I get).

To solve these issues, at least partially (people may still choose to edit scripts to customize their operations in other ways), I am looking at breaking these settings out into a separate file instead.

Here is a demonstration of how this would work. I’ve created a simple demo script and Settings.xml file to go with it.

powershell-xml-settings-01

The PowerShell script is quite simple – it sends an email message.

<# .SYNOPSIS Script Settings File Demo #>
[CmdletBinding()]
param ()
#-------------------------------------------------
#  Variables
#-------------------------------------------------
$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# Import email settings from config file
[xml]$ConfigFile = Get-Content "$MyDir\Settings.xml"
$smtpsettings = @{
    To = $ConfigFile.Settings.EmailSettings.MailTo
    From = $ConfigFile.Settings.EmailSettings.MailFrom
    Subject = "Email Subject Line Goes here"
    SmtpServer = $ConfigFile.Settings.EmailSettings.SMTPServer
    }
$body = "Email body goes here"
#-------------------------------------------------
#  Script
#-------------------------------------------------
try
{
	Send-MailMessage @smtpsettings -Body $body -ErrorAction STOP
}
catch
{
    Write-Warning $_.Exception.Message
}
#-------------------------------------------------
#  The End
#-------------------------------------------------

The script imports the Settings.xml file to determine the various email settings it should use.

# Import email settings from config file
[xml]$ConfigFile = Get-Content "$MyDir\Settings.xml"

The XML file itself is also quite simple.

powershell-xml-settings-02

The script uses the data in the XML file to set parameters for the email message. Values can still be set directly in the script (like the email subject below), but I think it will make more sense to put them in the XML file instead.

$smtpsettings = @{
    To = $ConfigFile.Settings.EmailSettings.MailTo
    From = $ConfigFile.Settings.EmailSettings.MailFrom
    Subject = "Email Subject Line Goes here"
    SmtpServer = $ConfigFile.Settings.EmailSettings.SMTPServer
    }

When the script is executed an email message is sent. Simple as that.

This concept extents beyond just the email settings for scripts. Several of my scripts also include other variables such as alert thresholds, text strings for different languages, and so on. The more of these that are moved into a Settings.xml file like the above demonstration uses, the fewer reasons there will be to edit a script file directly.

If you have any feedback or comments on this I’d love to hear them.


This article Using an XML Settings File for PowerShell Scripts is © 2014 ExchangeServerPro.com

Get more Exchange Server tips at ExchangeServerPro.com


Viewing all articles
Browse latest Browse all 515

Trending Articles