How to Run PowerShell Scripts from Task Scheduler

Thanks to: https://community.spiceworks.com

PowerShell is quickly taking over as the defacto scripting language in Windows environments--in fact, Microsoft has decreed to all of it's software units that they must create PowerShell cmdlet's with full functionality to the software in all future releases.
That means we have to learn how to run scripts from the Task Scheduler. Follow the steps below to do this:
** Please note: I am assuming you already know how to schedule a task and don't need detailed instructions for that.

8 Steps total

Step 1: Open Task Scheduler

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

Step 2: Set Triggers

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

Step 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.

Step 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

Step 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

Step 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.

Step 7: Full Argument

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

Step 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".

Comentarios

Entradas populares de este blog

Guía de herramientas básicas para estudiantes: 31 apps y webs imprescindibles para ayudarte con los estudios

Comando FOR para archivos BAT

How to Setup and Configure Your Own GitLab Server on Ubuntu 20.04