Avatar of Allanore
Allanore
 asked on

Powershell command to check exchange server health and send to me

Im trying to create an exchange powershell script to run these three following commands to check my exchange server status every so often.


Test-ServiceHealth servername | ft Role,RequiredServicesRunning -auto

test-mapiconnectivity -server "servername"

test-mailflow servername -targetemailaddress test@testaccount.com

What would be the easiest way to code this via powershell into a csv file and then send to me it as an attachment via email.
Windows Server 2008Microsoft Server OSExchange

Avatar of undefined
Last Comment
Allanore

8/22/2022 - Mon
bepsoccer1

Export all your tests out to your csv/s using: cmdlet | export-csv [-append](you can only use append if you have powershell 3.0 installed).  Rather than export-csv though you might be better off using "out-file -append" as the data returned from the three separate cmdlets will be formatted differently.  To "export-csv -append" to the same file all the data's headers would need to be the same.

Then build your email using the following:
$msg = new-object Net.Mail.MailMessage
$msg.from = <from address>
$msg.subject = <subject>
$msg.body = <body>
$msg.to.add(<someone@email.com>)
$attachment = <path to attachment>
$msg.attachments.add($attachment)
$smtpServer = <SMTP server>
$smtp = new-object Net.Mail.SmtpClient($smtpServer, <port(25)>)
$smtp.Send($msg)

Open in new window

ASKER CERTIFIED SOLUTION
Amit

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Allanore

ASKER
For the health check report powershell script, how would i create a scheduled task to run this with my specific parameters in windows 2008 R2? Would i have to create a .bat file to launch the .ps1 script with the -server -sendmail parameters?
Your help has saved me hundreds of hours of internet surfing.
fblack61