Quantcast
Channel: Powershell – TheITBros
Viewing all articles
Browse latest Browse all 91

Run PowerShell Script from Task Scheduler

$
0
0
windows powershell

Let’s take a look at the basics of creating scheduled job in Windows Task Scheduler to run PowerShell script. For example, we have TestPSScript.ps1 script that is needed to run on a daily basis. There are two ways to solve this situation.

Create Scheduler Task from GUI

Open Task Scheduler snap-in (it can be found in the Administrative Tools, or by pressing Win + R and typing taskschd.msc) and select Create Task from right pane.

task scheduler create task

Now specify the name and description of the task on the General tab. If necessary, specify the user on whose behalf the task will be run. For the task to be performed regardless of whether the user is logged in the system, select the option «Run whether user is logged on or not». When a task requires elevated privileges, then mark the option «Run with highest privileges».

create task security

Now go to the Triggers tab and create a new trigger (New), which runs the job in our schedule that will be stored. Specify the start date and time in the Start field and in Expire field the date and time the job is finished. Specify to perform a task Daily and set the repeat time (Recur every) 1 Day.

task new trigger

Then go to Action tab and specify an action for a scheduled task. Let us remind you that for security purposes PowerShell script can be executed only interactively, so at first you need to run PowerShell process and specify the path to the script in it. Therefore, in the Action specify to start powershell.exe, and in the field Add Arguments -File option and the path to our script, like this:

-File ″C:\PS\TestPSScript.ps1″

You can specify additional arguments:

  • -Command – Performs the command and any passing parameters, for example:
    -Command ″& {C:\PS\TestPSScript.ps1 –param1 1 –param2 22}″
  • -NonInteractive – Disable this interactive console to the user
  • -WindowStyle Hidden – run PowerShell window in hidden mode, invisibly to the user
  • -NoProfile – Prevents the user profile loading, which can slightly speed up the execution of script
  • -NoExit – Leave the shell open after execution of the script. This can be useful when testing and debugging
  • -ExecutionPolicy – Sets the script execution policy for the current session, it can be set to Unrestricted, RemoteSigned, AllSigned or Restricted. The specified policy will be effective only in the current session and takes precedence over any previously established policies

Note. Usually it is enough to set value RemoteSigned for normal scripts. This policy allows you to run scripts prepared on the local computer without limitation. Scripts downloaded from the Internet are started only if they are signed by the digital signature. You can change the current policy with the help of Set-ExecutionPolicy, for example:

Set-ExecutionPolicy RemoteSigned –force

task sequence new action

Fill in the necessary fields, click OK and save the task. Now, the script will start on a schedule on daily basis at the specified time.

Create  Schedule Job using PowerShell 3.0 and higher

In PowerShell 3.0 on Windows 8 and Windows Server 2012 and higher, a new functional Scheduled Job appears. It gives the ability to create scheduled tasks from the PowerShell command console without using the Task Scheduler snap-in.

At first, create a schedule for running:

$t_trigger = New-JobTrigger -Daily -At 12:00PM

Then save credential to the variable:

$cred_saved = Get-Credential corp\obama

Specify the job to run with elevated privileges:

$elevated_job = New-ScheduledJobOption -RunElevated

And register the task with name Start:

Register-ScheduledJob -Name Start -FilePath C:\PS\TestPSScript.ps1 -Trigger $t_trigger -Credential $cred_saved -ScheduledJobOption $elevated_job

PowerShell Script scheduled job

To make sure that the job is created, you need to open the Task Sheduler and find the task under Microsoft\Windows\PowerShell\SheduledJobs that was created earlier.

task scheduler jobs

Tip. For each scheduled task, in the directory %systemdrive%\Users\%username%\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs PowerShell creates a folder with the same name. This folder contain XML file with task settings and folder Output, that stores the job history in the files Result.xml and Status.xml. These files can be useful for debugging and diagnostics if the task that doesn’t work properly.

The post Run PowerShell Script from Task Scheduler appeared first on TheITBros.


Viewing all articles
Browse latest Browse all 91

Trending Articles