Created
September 29, 2017 15:35
-
-
Save thecliguy/fdc72cbed2ce4e9ebb27d7dc3ebd0d2b to your computer and use it in GitHub Desktop.
Sample Scheduled Task Creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$TaskName = "My Scheduled Task"; | |
$SchedTaskCred = Get-Credential "domain\user" ` | |
-Message "Scheduled Task Service Account Credentials"; ` | |
$SchedTaskCredUser = $SchedTaskCred.UserName; ` | |
$SchedTaskCredPwd = $SchedTaskCred.GetNetworkCredential().Password; ` | |
$Action = New-ScheduledTaskAction ` | |
-Execute "PowerShell.exe" ` | |
-Argument '-ExecutionPolicy Bypass -file "C:\MyScript.ps1"' ` | |
-ErrorAction Stop; ` | |
$Trigger = New-ScheduledTaskTrigger ` | |
-Daily ` | |
-At 6pm ` | |
-ErrorAction Stop; ` | |
$Settings = New-ScheduledTaskSettingsSet ` | |
-ExecutionTimeLimit ([TimeSpan]::FromHours(2)) ` | |
-ErrorAction Stop; ` | |
$Task = New-ScheduledTask ` | |
-Action $Action ` | |
-Trigger $Trigger ` | |
-Settings $Settings ` | |
-ErrorAction Stop; ` | |
Register-ScheduledTask $TaskName ` | |
-InputObject $Task ` | |
-User $SchedTaskCredUser ` | |
-Password $SchedTaskCredPwd ` | |
-ErrorAction Stop; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment