Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

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

#Email#
$smtp = "mailserverhost" 
$to = "Jason@company.com" 
$from = "BackupReport@incredible.co.za" 
$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"}}

$body   
send-MailMessage -SmtpServer $smtp -To $to -Subject $sub -Body $body  -From $from
share|improve this question
2  
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
1  
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

1 Answer 1

up vote 1 down vote accepted

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

Clear
$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.#>
#Email#
$messageParameters = @{ <# put all e-mail variables in another variable to simplify send-MailMessage command #>
SmtpServer = "mailserverhost" 
To = "Jason@company.com" 
From = "BackupReport@incredible.co.za" 
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

 
discard

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.