18.2. Building a Reusable Script

The script in the previous section does a fine job of sending a simple message, but it is not reusable at all. Should we have to send a message with a different recipient, we would have to copy the code, paste it to a new script, and make the change there. That leaves us with two almost identical scripts that are twice as hard to maintain. We need to move the mail server configuration and the user credentials to a separate place and include them in our scripts. Additionally, we have to abstract the message data, the recipient, subject, and body and introduce parameters that allow us to pass different values from the command line. Let's start with the configuration.

A slightly more secure location for storing ...

Get Pro Windows PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.