Link to home
Start Free TrialLog in
Avatar of technolutions
technolutionsFlag for South Africa

asked on

Running test-systemhealth | out-File "C:\smtptest.txt" returns me a blank file?

I'm just starting out in the world of powershell scripting and I'm working on a script to run "test-systemhealth" on exchange, chuck the output of that into a txt file and e-mail it to me. That way I can schedule it to run every morning and I can check if my server is fine each morning while sipping away at my coffee. Find attached the code draft for my script.

Problem: everything works but when I run test-systemhealth | out-File "C:\smtptest.txt" it gives me the following message in powershell and I'm left with a blank txt file:

Test-SystemHealth : Database 'Mailbox Database' on server SEMI contains 18 mail
boxes. This database has never had a full online backup.
At line:1 char:18
+ test-systemhealth  <<<< | out-File "C:\smtptest.txt"
Test-SystemHealth : Database 'Public Folder Database' on server SEMI has never
had a full online backup.
At line:1 char:18
+ test-systemhealth  <<<< | out-File "C:\smtptest.txt"
Test-SystemHealth : The Microsoft Exchange System Attendant process on server S
EMI is configured to interact with the desktop. This configuration should only
be used under the guidance of Microsoft Product Support Services.
At line:1 char:18
+ test-systemhealth  <<<< | out-File "C:\smtptest.txt"
test-systemhealth | out-File "C:\smtptest.txt"
 
$SMTPserver = "server.domain.local"
$fileattachment = "c:\smtptest.txt"
$from = "administrator@blah.com"
$to = "me@blah.com"
$subject = "Mail Test"
$emailbody = "Morning Server Test"
 
$mailer = new-object Net.Mail.SMTPclient($SMTPserver)
$msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
$attachment = new-object Net.Mail.Attachment($fileattachment)
$msg.attachments.add($attachment)
$mailer.send($msg)

Open in new window

Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Those are expected errors, so I am assuming you just want to make sure they make it in the file. The error stream is alot like old cmd.exe. You need to redirect it.,

test-systemhealth 2>&1 | out-File "C:\smtptest.txt"

BTW... you should post this question here

Powershell Zone
https://www.experts-exchange.com/Programming/Languages/Scripting/MSH-Monad/ 
Avatar of technolutions

ASKER

Thank you so much! I'll post all my questions there from now on thanks. Can you explain to me how 2>&1 works and how exactly what happened? I increased the point value to 500 for that explanation for you.
ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial