Datasheet

UNDERSTAND THE BASICS OF POWERSHELL 37
Administering
Service Delivery
PART I
Schedule a Script to Run Automatically
Sometimes you’ll want to create a script and have it run repeatedly at a speci ed
interval. Windows has a powerful Task Scheduler service that allows you to set up
programs to launch in a very  exible manner. If you want to schedule a script to run
at a later time or a er a speci ed interval, you can schedule the script to run with
the Task Scheduler. In order to execute a PowerShell script via Task Scheduler, you
need to associate your PowerShell
.ps1
script le with the
PowerShell.exe
program or have the scheduler execute the command
PowerShell.exe
MyScript.ps1
to launch the script in the scheduled task.
ere are multiple ways to schedule a task with the Windows Task Scheduler.  e
easiest way is to use the Task Scheduler snap-in. In this example, you’ll use the Task
Scheduler to create a task that does the following things:
Runs a script every night that collects user statistics to determine who
has not logged on in the past 90 days. In this example, the script is named
GetLogonStats.ps1
.  is is a script that we created ourselves; it isn’t
installed with Active Directory. In the fake script, the statistics are saved in
the  le
C:\Stats\LogonStats.log
.
Emails the statistics  le to the mail-enabled distribution group called
ADTeam@contoso.com
.
To create the scheduled task, follow these steps:
1. Click the Start menu, and select Administrative Tools
Task Scheduler. e
Task Scheduler launches.
2. In the Action Pane on the right side of the Task Scheduler snap-in, choose the
option Create Task.  e Create Task dialog box opens.
3. On the General tab, give the task a name, and choose the option Run
Whether User Is Logged On Or Not. Set the account that you want the
script to run under using the Change User Or Group button. Ensure that the
account running the script has the appropriate permissions. Figure 1.13 shows
these options.
4. Click the Triggers tab, and click the New button to create a new trigger for the
task. Set this task to run every night at 1:00 a.m.
c01.indd 37c01.indd 37 5/12/2011 1:07:53 PM5/12/2011 1:07:53 PM