官术网_书友最值得收藏!

Scheduling scripts to run at a later time

One of the most common tasks that Exchange administrators perform is scheduling scripts to run at a later time. This can be useful when performing maintenance after hours or running monitoring scripts on a regular basis. In this recipe, you'll learn how to schedule your PowerShell scripts to run with the Windows Task Scheduler.

How to do it...

To create a scheduled task that runs from one of your Exchange servers use the following steps:

  1. Open the Windows Task Scheduler by clicking on Start | All Programs | Accessories, click on the System Tools folder, and then click the Task Scheduler shortcut.
  2. From the Action menu, click Create Basic Task.
  3. Give your task a name and description, and click Next.
  4. On the Trigger screen, select the how often you'd like the script to run (Daily, Weekly, Monthly, and so on).
  5. When asked what action you want the task to perform, select Start a Program.
  6. Use the following syntax in the Program/Script field and click on Next: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\Scripts\MoveMailboxes.ps1".
  7. You will receive a prompt that says It appears as though arguments have been included in the program text box. Do you want to run the following program? Click Yes.
  8. This will bring you to a summary screen where you can click Finish.

How it works...

The syntax used in this example may look a little strange at first. What we are actually doing here is scheduling PowerShell.exe and using the -Command parameter to execute multiple statements. This allows us to pass the contents of a PowerShell script to PowerShell.exe. In this case, our script has multiple lines and each statement is separated by a semi-colon.

The first thing we do is dot-source the RemoteExchange.ps1 script located in the Exchange Server Bin directory. This file initializes some Exchange shell variables and imports several Exchange specific functions.

The next line of the script calls the Connect-ExchangeServer function using the -Auto parameter, allowing the Exchange Management Shell environment to load automatically from the best Exchange Server in the local AD site.

Finally, we provide the path to our .ps1 script that utilizes any required Exchange Management Shell cmdlets and the script is executed, carrying out whatever it is that we need to be done.

It's worth mentioning here that you do not have to use a .ps1 script file with this syntax. You can replace the call to the MoveMailboxes.ps1 file with any valid PowerShell commands. If you have a script that contains multiple lines, you can continue to separate each line using a semi-colon.

When using this method, make sure that you configure the scheduled task to run as a user that has administrative access to your Exchange organization. Also, if you have User Account Control (UAC) enabled, you may need to enable the option to Run with highest privileges in the properties of the scheduled task. Additionally, you will probably want to enable the option to Run whether user is logged on or not in the properties of the scheduled task.

There's more...

The previous example demonstrated scheduling a task from an Exchange server using the installed Exchange Management Shell tools. Since all of the Exchange Management Shell connections utilize PowerShell remoting, it is possible to schedule a script to run from a workstation or server without the Exchange tools installed. The only requirement is that the machine must be running PowerShell v2.

To schedule a task from a machine without the Exchange tools installed, use the steps from the previous example, but use the following syntax for the program action:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "$s = New-PSSession -ConfigurationNameMicrosoft.Exchange -ConnectionUri http://ex01.contoso.com/PowerShell/; Import-PSSession $s ; c:\Scripts\MoveMailboxes.ps1"

You can see here again we are scheduling the PowerShell.exe program and specifying the script using the -Command parameter. The difference is that this time we are not using the locally installed Exchange tools. Instead we are creating a manual implicit remoting connection to a particular Exchange server. The length of the command line wrapping makes it difficult to read, but keep in mind that this is all done on one line.

When using this method, you can configure the scheduled task to run as a user that has administrative access to your Exchange organization, or you can provide explicit credentials used to create the session object and run the script as another user.

See also

  • Manually configuring remote PowerShell connections
  • Using explicit credentials with PowerShell cmdlets
  • Creating and running scripts in Chapter 1, PowerShell Key Concepts
主站蜘蛛池模板: 吴堡县| 乐至县| 陇西县| 南丹县| 澄城县| 望都县| 包头市| 卢湾区| 通州市| 二连浩特市| 金乡县| 龙川县| 蕉岭县| 望江县| 潞城市| 清新县| 广元市| 嫩江县| 河北省| 尼木县| 新蔡县| 洛阳市| 丰台区| 财经| 新民市| 开封市| 正阳县| 达日县| 南平市| 泸水县| 辛集市| 麻城市| 论坛| 涡阳县| 革吉县| 泽普县| 吉林市| 莱芜市| 济源市| 义马市| 浑源县|