Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like this script that I wrote to send an email of its output as well as display the out put in the console. I have written this but it doesnt seem work as expected.

How can I make this email?

$serverlist = Get-Content -Path c:\Users\jasonbe\Serverlist.txt
$Path = "\z$\Backups\daily\Daily_Year2012Month7Day1.bkf"

$smtp = "mailserverhost" 
$to = "" 
$from = "" 
$sub = "Server Status" 
$body = @"

foreach ($server in $serverlist) 
{if ((Test-Path "\\$server\$Path") -eq $False)
    {write-host -ForegroundColor Red "$server needs backup"}
elseif ((Test-Path "\\$Server\$Path") -eq $True)
{write-host -ForegroundColor blue "$server has backup"}}

send-MailMessage -SmtpServer $smtp -To $to -Subject $sub -Body $body  -From $from
share|improve this question
Perhaps you could say exactly how it doesn't work as expected, and what you have tried already. – Dominic Cronin Jul 2 '12 at 13:55
Why don't you just write the results of each test using both write-host ... and to the $body variable, then just e-mail out? – Chris S Jul 2 '12 at 17:00
up vote 1 down vote accepted

Fixed multiple errors in the script to get a working one:

$serverlist = Get-Content -Path c:\Users\jasonbe\Serverlist.txt
$bkf = "Daily_Year$(date -F yyyy)Month$(date -F MM)Day$(date -F dd).bkf" <# Added dynamic date variable so it doesn't have to be updated manually each time #>
$Path = "z$\Backups\daily\$bkf" <# removed beginning '\' since it will screw up the Test-Path check below #>

[array]$array = $null <# declare a null array that we add data to later (also resets the variable to null if running multiple times) #>

foreach ($server in $serverlist) {
    if ((Test-Path \\$server\$Path) -eq $False) { <# no need to quote the path #>
        write-host -ForegroundColor Red "$server needs backup"
        $array += "$server needs backup" <# added update to $array variable #>
    else { <#if ((Test-Path "\\$($Server)\$Path") -eq $True)#> <# no need to check for this since it must be True at this point in the script #>
        write-host -ForegroundColor blue "$server has backup"
        $array += "$server has backup" <# added update to $array variable #>
$EmailBody = ForEach ($row in $array) {"`r`n",$row} <# puts each addition to the $body variable on its own line in the e-mail, otherwise they will all run together.#>
$messageParameters = @{ <# put all e-mail variables in another variable to simplify send-MailMessage command #>
SmtpServer = "mailserverhost" 
To = "" 
From = "" 
Subject = "Server Status"
Body = "$($Emailbody)"

send-MailMessage @messageParameters
share|improve this answer
Thank you! This is legendary :) I have learned a lot from studying your script. – Jake Jul 16 '12 at 6:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.