Tuesday, February 18, 2014

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

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

No comments:

Post a Comment

The blog is written to the share the knowledge mainly on Microsoft Exchange Server and other Microsoft product that experienced on day-to-day life.