PowerShell for Storage and File System Management (Part 10)

If you would like to read the other parts in this article series please go to:


So far in this article series, I have shown you how to build a PowerShell script that checks hard disk health and the potential for storage failure on each of your servers. As nice as this script is in its current form, it does have one major disadvantage. The script must be run manually. Eventually, I am going to show you how you can run the script automatically, on a scheduled basis. However, we’ve got a bit more work to do first. Automating the script will do little good if no one is around to view the output. That being the case, I want to show you how to modify the script to produce E-mail alerts whenever the script detects a problem.

Before I Begin

Before I get started, I need to point out that although PowerShell is capable of sending an E-mail message, it depends on an external SMTP server. The way in which you construct your script will vary based on the E-mail platform that you are going to be using. In my case, I am going to be using a lab deployment of Exchange Server 2013. In this article, I will show you how to get mail working. In the next article, we will integrate the mail related code into our existing script.

Credential Handling

The first problem that you are going to need to deal with is that of credential handling. Using an Exchange Server mailbox for sending mail requires a user to authenticate into the Active Directory environment. A PowerShell script can be configured to ask for a user’s credentials by way of the Get-Credential cmdlet, but this will do little good if the script is to be run on an automated basis. At the same time however, you really don’t want to be hard coding a plain text password into your script. Fortunately, there is a solution.

The trick is to create a file containing an encrypted password, and to configure your script to use this file for authentication. This process is easier than it sounds. For example, if you wanted to create a file named C:\Scripts\Password.txt, you could create the file by using the following command:

Read-Host –AsSecureString | ConvertFrom-SecureString | Out-File C:\Scripts\Password.txt

So let’s take a look at how this works. The Read-Host portion of the command tells PowerShell that you want to type something – in this case, a password. The AsSecureString parameter tells PowerShell to mask your input. Finally, the password is converted from a secure string into encrypted text and written to the destination text file. You can see what this looks like in Figure A.

Figure A: The password is masked as it is typed.

As you can see in the figure above, PowerShell didn’t prompt me to enter a password. I simply started typing. As I did, PowerShell masked my input. You can see the contents of the password file in Figure B.

Figure B: This is my password file.

Sending E-mail

Now that I have shown you a method for handling authentication credentials, let’s go ahead and build a script that will send an E-mail message. For the purpose of this article, I am going to design my script to send a test E-mail. Once the code has been verified to work, we can worry about sending something more meaningful. You can see my code below:

$EmailTo = “[email protected]

$EmailFrom = “[email protected]

$Subject = “Test”

$Body = “Test Body”

$SMTPServer = “E2K13.poseydemo.com”


$Username = “poseydemo\Administrator”

$Password = Get-Content c:\scripts\cred.txt | ConvertTo-SecureString

$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password


Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $Subject -Body $Body -Credential $Cred -SMTPServer $smtpserver

As you can see, my code is really simple. In fact, it only takes one line of code to send an E-mail message. All of the other code that you see simply defines the characteristics of the message that is being sent. We could do all of this in one line, but I have found that the code is less confusing, and easier to modify if variables are used.

So with that said, the first block of code defines five essential variables. These variables include:

$EmailTo – The E-mail address that the message will be sent to

$EmailFrom – The E-mail address where the message will appear to have come from

$Subject – The message’s subject line

$Body – The message body

$SMTPServer – The name or IP address of your SMTP server

Even though these variables are really straightforward, there are a few things worth knowing. For starters, you aren’t limited to using only the variables that I have used. PowerShell supports the use of attributes such as CC and BCC, and even allows mail attachments to be sent.

Another thing that I want to mention is that in my script, the messages will be sent to the domain administrator, and will appear to have come from the domain administrator. In a production environment, you may wish to consider creating a dedicated mailbox or sending mail to an administrator’s regular E-mail address.

The next block of code establishes the authentication credentials for the mail server. In this case, $Username contains the login name for the account that will be logging into the mail server. In the case of a Microsoft Exchange environment, this account should be specified in domain\username format. If you are using a mail server other than Exchange Server, you will probably have to specify your username in a different format.

The $Password variable stores the contents of the password file. In this case, I am reading a file named C:\Scripts\Cred.txt. The password is being read from the file, and then converted into a secure string.

The third line of code in this section sets up a variable named $Cred. This variable will store the user’s credentials in a way that PowerShell can use.

The final line of code is the line that actually sends the mail message. This line of code uses the Send-MailMessage cmdlet, which is supported only in PowerShell 2.0 and above. The cmdlet uses parameters such as To, From, Subject, Body, Credential, and SMTPServer. Notice in the code how I am using variables to populate these parameters.

So what happens when I run this script? Well, if you look at Figure C, you can see that the script does not produce any visible output.

Figure C: My mail script does not produce any visible output within PowerShell.

Even though PowerShell does not display any output, the script does generate the specified E-mail message. You can see what the message looks like in Figure D.

Figure D: This E-mail message was created by PowerShell.


In this article, I have explained how it is possible to write a PowerShell script that sends an E-mail message. In the next article in this series, I will integrate my mail script into the storage health script so that we can receive E-mail alerts for any storage health problems that might be detected.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top