Exchange Management Shell
Since Exchange 2007, Microsoft has provided the Exchange Management Shell so administrators can manage all aspects of the Exchange server from the command line.
The Exchange Management Shell has Exchange specific PowerShell cmdlets. These Exchange cmdlets are not normally available in an ordinary PowerShell command environment.
An example of what can be done in the Exchange Management Shell is to run a PowerShell script to list all the mailboxes on the Exchange server to a file. You can output columns based on display name, size of the mailbox, last logon, and other available mailbox attributes.
You can also schedule a batch migration of mailboxes from one database to another such as the migration of mailboxes from Exchange 2010 to Exchange 2013.
Scheduling the PowerShell Script
Once you have written a PowerShell script and utilised the Exchange cmdlets, you can run it with no problems inside the Exchange Management Shell. If you were to try to run it under a normal PowerShell command line, it would not be able to know about the Exchange cmdlets. This means that if you were to schedule it in Task Scheduler the same way you would normally do with regular PowerShell scripts, it will not work either.
You have to add special parameters. These parameters are basically the same as what is being called when you launch the Exchange Management Shell. If we check the properties of the Exchange Management Shell shortcut, we can see the following.
The Target is :
Note: The above arguments are all on one line. The red writing above (c:\myscriptsfolder\myscript.ps1) should be the full path to the script that you want to run. Also, the above example is for Exchange 2016. You will need to check your Exchange Management Shell properties on your server to find the location of the RemoteExchange.ps1 file.
When you create your task, in the New Action window, fill it out so it looks something like this.
Since Exchange 2007, Microsoft has provided the Exchange Management Shell so administrators can manage all aspects of the Exchange server from the command line.
The Exchange Management Shell has Exchange specific PowerShell cmdlets. These Exchange cmdlets are not normally available in an ordinary PowerShell command environment.
An example of what can be done in the Exchange Management Shell is to run a PowerShell script to list all the mailboxes on the Exchange server to a file. You can output columns based on display name, size of the mailbox, last logon, and other available mailbox attributes.
You can also schedule a batch migration of mailboxes from one database to another such as the migration of mailboxes from Exchange 2010 to Exchange 2013.
Scheduling the PowerShell Script
Once you have written a PowerShell script and utilised the Exchange cmdlets, you can run it with no problems inside the Exchange Management Shell. If you were to try to run it under a normal PowerShell command line, it would not be able to know about the Exchange cmdlets. This means that if you were to schedule it in Task Scheduler the same way you would normally do with regular PowerShell scripts, it will not work either.
You have to add special parameters. These parameters are basically the same as what is being called when you launch the Exchange Management Shell. If we check the properties of the Exchange Management Shell shortcut, we can see the following.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-noexit -command ". 'C:\Program Files\Microsoft\Exchange
Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto
-ClientApplication:ManagementShell "
(Note: The above is ALL one line)
We are going to break it up into the components needed to schedule a task in Task Scheduler like this:
Program/Script: powershell
Add arguments (optional): -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto ; c:\myscriptsfolder\myscript.ps1 "
Note: The above arguments are all on one line. The red writing above (c:\myscriptsfolder\myscript.ps1) should be the full path to the script that you want to run. Also, the above example is for Exchange 2016. You will need to check your Exchange Management Shell properties on your server to find the location of the RemoteExchange.ps1 file.
We need to remove -noexit from the arguments.
If left in there, the script will not exit after it finishes processing and the Task will be left "running". You will then need to end it manually before this same Task can run again.
Comments
Post a Comment