Wednesday, August 20, 2014

Create a schedule task to run PowerShell script

Nowadays, many administrators write their own PowerShell scripts and perform it in the production environment. Administrators may create a basic task in Task Scheduler to run the PowerShell script for daily operations.

To create a basic task to run the PowerShell script, we can do the following steps.

1. On a server or workstation, log in as Administrator.
2. Launch "Task Scheduler".
3. Next to "Actions" pane, click "Create Basic Task".

4. On "Create a Basic Task" window, enter the name of your task.

5. Click "Next".
6. On "Trigger" window, select the period of your task. In my lab environment, I selected "Daily".

7. Click "Next".
8. On "Daily" window, select the "Start" time of this task.

9. Click "Next".
10. On "Action" window, select "Start a program".

11. Click "Next".
12. On "Start a Program" window, next to "Program/script", enter "PowerShell".
13. Next to "Add arguments (optional)", enter the script path.

14. Click "Next".
15. Check "Open the Properties dialog for this task when I click Finish" and then click "Finish".

16. On "Move log files Properties" window, we can change to "SYSTEM" account by  entering "SYSTEM" in "Change User or Group".

17. Click "OK" to close "Move log files Properties".

As a result, the schedule task for this PowerShell script was created.

Remark: Before you create the schedule task, you may need to change the "Execution Policy" to "unrestricted" or "RemoteSigned" by performing "Set-ExecutionPolicy".

We can also perform the following cmdlets to create a schedule task in Windows 8, Windows 8.1, Windows Server 2012 and Windows Server 2012 R2.

$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "C:\Move-Log.ps1"
$Trigger = New-SchedulesTaskTrigger -Daily -At 1am
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
Register-ScheduledTask "Move Log File 2" -InputObject $Task -User "NT Authority\SYSTEM"

This posting is provided “AS IS” with no warranties, and confers no rights!

No comments:

Post a Comment