Exchange Server 2013 Powershell Script Task Scheduler


Sometime we try to automate the powershell script using the task scheduler. Let me explain in detail the step by step on how to move forward for Exchange Server 2013 Powershell Script in the Task Scheduler

To connect another Exchange Server Management Shell use the following command

Connect-ExchangeServer <Exchange server FQDN>

To verify the Windows Powershell can able to execute the Exchange management Shell command.

Open the Windows Powershell – Execute the following command to verify

C:\Windows\System32\WindowsPowerShell\v1.0>Powershell.exe -PSconsoleFile “E:\Program Files\Microsoft\Exchange Server\V15\Bin\ExShell.psc1” -command Get-queue

The above command works in Exchange 2013 Mailbox Server but when we execute them in Exchange 2013 CAS Server it fails. Reason we need use to remotepowershell instead of localhost powershell

C:\Windows\System32\WindowsPowerShell\v1.0>Powershell.exe -Command “.’E:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; Get-queue”

To provide the output in the text file

C:\Windows\System32\WindowsPowerShell\v1.0>Powershell.exe -Command “.’E:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; Get-queue > C:\temp\queu.txt”

To execute the Exchange 2013 Powershell script in the Windows Powershell

C:\Windows\System32\WindowsPowerShell\v1.0>Powershell.exe -Command “.’E:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; C:\Temp\Raji\Test-ExchangeServerHealth.ps1”

To send email using the Powershell command of the Exchange 2013 Output using Windows Powershell

C:\Windows\System32\WindowsPowerShell\v1.0>Powershell.exe -Command “.’E:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; Send-MailMessage -From admin@mail.com -To user1@mail.com,user2@mail.com -SmtpServer smtp.mail.com -Subject Exchange_2013_DBStatus -body (Get-Mailboxdatabasecopystatus * | sort-object ContentIndexState | Select-Object Name,ContentindexState,Status | format-table -autosize | out-string)”

I belive this is more than enough….Now we can move how to bring them in Task Schedule

1. Open the Task Scheduler in Windows > Task Scheduler Library > Right click and “Create Task..“

Exchange 2013 Automated Health Report

2. Type a descriptive name for your task and click Next.

3. On the Trigger section, choose your schedule that fits your needs.

4. On the Action, choose “Start a program” ,Click Next.

5. On the “Start a Program” subsection, Enter:

In the Program/Script, copy and paste

C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -Command “.’E:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; Send-MailMessage -From admin@mail.com -To user1@mail.com,user2@mail.com -SmtpServer smtp.mail.com -Subject Exchange_2013_DBStatus -body (Get-Mailboxdatabasecopystatus * | sort-object ContentIndexState | Select-Object Name,ContentindexState,Status | format-table -autosize | out-string)”

It prompt dialog box where it says

In the Add Arguments (Optional) -Command “.’E:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; Send-MailMessage -From admin@mail.com -To user1@mail.com,user2@mail.com -SmtpServer smtp.mail.com -Subject Exchange_2013_DBStatus -body (Get-Mailboxdatabasecopystatus * | sort-object ContentIndexState | Select-Object Name,ContentindexState,Status | format-table -autosize | out-string)”

Say Yes – In the Start in (Optional), leave it empty

6. Click Okay

Run the task now to verify is it working fine. Schedule the time accordingly to your time when you want to get it executed.

This is similar for Exchange 2007 and 2010 except the location of RemoteExchange.PS1 file

For Exchange 2007 – C:\Program Files\Microsoft\Exchange Server\RemoteExchange.ps1
For Exchange 2010 – C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1

Advertisements

About Raji Subramanian

Nothing great to say about me...Just want to share my knowledge for others that will be useful at any moment of time when they stuck in critical issue....
This entry was posted in Exchange Server 2013, PSconsoleFile and tagged , , , . Bookmark the permalink.

One Response to Exchange Server 2013 Powershell Script Task Scheduler

  1. Zoltan says:

    Nice post Raji, thanks 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s