This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
microsoft:powershell_script_scheduler [2019/02/26 14:31] admin created |
microsoft:powershell_script_scheduler [2019/02/26 14:43] (current) admin |
||
---|---|---|---|
Line 9: | Line 9: | ||
===== Open Task Scheduler ===== | ===== Open Task Scheduler ===== | ||
- | Expand | ||
Open Task Scheduler and Create a new task. Name it and set your security options. | Open Task Scheduler and Create a new task. Name it and set your security options. | ||
- | Step 2: Set Triggers | + | {{ :microsoft:powershell_script_scheduler_01.jpg?direct&800 |}} |
- | Expand | + | |
+ | ===== Set Triggers ===== | ||
Click on the Triggers tab and set your schedule or event that will trigger the running of your Powershell script. | 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 | + | {{ :microsoft:powershell_script_scheduler_02.jpg?direct&800 |}} |
- | Expand | + | |
+ | ===== Create your Action ===== | ||
+ | |||
Click on the Actions tab and click on New. | Click on the Actions tab and click on New. | ||
+ | |||
+ | {{ :microsoft:powershell_script_scheduler_03.jpg?direct&800 |}} | ||
Action: Start a program | Action: Start a program | ||
Line 26: | Line 32: | ||
You don't need to put a path as it should already be on your system. | You don't need to put a path as it should already be on your system. | ||
- | Step 4: Set Argument | + | ===== Set Argument ===== |
- | Expand | + | |
+ | {{ :microsoft:powershell_script_scheduler_04.jpg?direct&800 |}} | ||
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: | 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: | ||
+ | <code winbatch> | ||
Get-Help About_execution_policies | Get-Help About_execution_policies | ||
+ | </code> | ||
To set the execution policy globally, you can issue this command from within PowerShell: | To set the execution policy globally, you can issue this command from within PowerShell: | ||
+ | <code winbatch> | ||
Set-ExecutionPolicy Unrestricted | Set-ExecutionPolicy Unrestricted | ||
+ | </code> | ||
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. | 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: | That means we use the following Argument: | ||
+ | <code winbatch> | ||
-ExecutionPolicy Bypass | -ExecutionPolicy Bypass | ||
+ | </code> | ||
+ | |||
+ | ===== Set the next argument ===== | ||
- | Step 5: Set the next argument | ||
- | Expand | ||
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. | 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. | ||
+ | <code winbatch> | ||
c:\scripts\myscript.ps1 | c:\scripts\myscript.ps1 | ||
+ | </code> | ||
+ | |||
+ | {{ :microsoft:powershell_script_scheduler_04.jpg?direct&800 |}} | ||
+ | |||
+ | ===== Add parameters ===== | ||
+ | |||
+ | {{ :microsoft:powershell_script_scheduler_05.jpg?direct&800 |}} | ||
- | Step 6: Add parameters | ||
If your script has any parameters, you want to add them now: | If your script has any parameters, you want to add them now: | ||
+ | <code winbatch> | ||
-RunType $true -Path c:\mydatafiles | -RunType $true -Path c:\mydatafiles | ||
+ | </code> | ||
Please keep in mind these arguments/parameters are purely as an example, many scripts won't have any arguments at all. | 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 | + | ===== Full Argument ===== |
That makes your full argument line look like so: | That makes your full argument line look like so: | ||
+ | <code winbatch> | ||
-ExecutionPolicy Bypass c:\scripts\myscript.ps1 -RunType $true -Path c:\mydatafiles | -ExecutionPolicy Bypass c:\scripts\myscript.ps1 -RunType $true -Path c:\mydatafiles | ||
+ | </code> | ||
+ | |||
+ | ===== Save the scheduled task ===== | ||
+ | |||
+ | {{ :microsoft:powershell_script_scheduler_06.jpg?direct&800 |}} | ||
- | Step 8: Save the scheduled task | ||
- | Expand | ||
Save your scheduled task and run it. | Save your scheduled task and run it. | ||