O'Reilly logo

Pro Windows PowerShell by Hristo Deshev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required