Created
May 24, 2023 16:03
-
-
Save jessewolcott/0f6205dea3410577a16147783c9328a6 to your computer and use it in GitHub Desktop.
This file contains 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
$ScheduledTaskName = "Scriptname" | |
$Time = "00:00" | |
$Argument = ("-executionpolicy bypass -file 'C:\Scripts\Somescript.ps1'") | |
$TaskParams = @{ | |
Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "$Argument" | |
Trigger = New-ScheduledTaskTrigger -Daily -At $Time | |
Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -RunOnlyIfNetworkAvailable | |
Principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest | |
Description = "Run $($ScheduledTaskName) at $Time daily" | |
} | |
Write-Output "Creating scheduled task called $ScheduledTaskName " | |
Register-ScheduledTask -TaskName $ScheduledTaskName -InputObject (New-ScheduledTask @TaskParams) -Force | |
$task = Get-ScheduledTask -TaskName $ScheduledTaskName -ErrorAction SilentlyContinue | |
if ($null -ne $task){ | |
Write-Output "Created scheduled task: '$($task.ToString())'." | |
} | |
else{Write-Output "Created scheduled task: FAILED."} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment