Monday, November 11, 2013

Script in windows scheduler

1.

Open Task Scheduler

Open Task Scheduler and Create a new task. Name it and set your security options.
36cad5283fcfcf840b353680676936550349eb4d68470f275925743e337c0c6c_createtask_big
2.

Set Triggers

Click on the Triggers tab and set your schedule or event that will trigger the running of your Powershell script.
758c72854bd646378e604516076a136ca73e18b08236afb681a5ccc46e62f41f_settrigger_big
3.

Create your Action

Click on the Actions tab and click on New.
Action: Start a program
Program/script: Powershell.exe
You don't need to put a path as it should already be on your system.
8519a2f519b07af55c2de43af78056169599e68ae99fa824792803483bf79d8d_newaction_big
4.

Set Argument

First you need to set the ExecutionPolicy. You have two options here, you can set the ExecutionPolicy on the machine or you can do it on a per-script basis. Read the PowerShell ExecutionPolicy link below as it talks about or you can issue the command:
Get-Help About_execution_policies
To set the execution policy globally, you can issue this command from within PowerShell:
Set-ExecutionPolicy Unrestricted
Or use one of the other settings available depending on your environment. In the context of this how-to, however, we want to set the execution policy on a per script basis and open up security for us to run the script. This security policy will only be in effect for the script we are running and not compromise security otherwise.
That means we use the following Argument:
-ExecutionPolicy Bypass
D9a7403f0e632b5c85e040aef0e2a1612c6ef9c6f0ec67c601c738218e3b0ffa_executionpolicy_big
5.

Set the next argument

Next comes the path and name of your script. This can be either a drive letter path, or a full UNC. I've run into problems using the -FILE parameter so I don't use it.
c:\scripts\myscript.ps1
Bf8c65c134cd5b4a63a973530d14b32246a6dae40e72b0b0eb49d1e3a84b1acd_script_big
6.

Add parameters

If your script has any parameters, you want to add them now:
-RunType $true -Path c:\mydatafiles
Please keep in mind these arguments/parameters are purely as an example, many scripts won't have any arguments at all.
7.

Full Argument

That makes your full argument line look like so:
-ExecutionPolicy Bypass c:\scripts\myscript.ps1 -RunType $true -Path c:\mydatafiles
8.

Save the scheduled task

Save your scheduled task and run it.
I've found it helpful to run the task with the "Run only when user is logged on" security option turned on (on the General tab). This will run your script interactively and will allow you to observe it and see if there are any errors.
Once you have a clean run you can change the security option back to "Run whether user is logged on or not".

No comments:

Post a Comment