Skip to content

Instantly share code, notes, and snippets.

@Kolossi
Last active January 31, 2020 12:26
Show Gist options
  • Save Kolossi/7606fec000e01dc994316c86e8c2a829 to your computer and use it in GitHub Desktop.
Save Kolossi/7606fec000e01dc994316c86e8c2a829 to your computer and use it in GitHub Desktop.
--- windows-scheduled-task-create_v19.json Fri Jan 31 12:23:09 2020
+++ windows-scheduled-task-create_v20.json Fri Jan 31 12:21:13 2020
@@ -122,6 +122,16 @@
-ArgumentList @( "/Change", "/S", "localhost", "/TN", "`"$TaskName`"", "/ENABLE" );
}
+Function Disable-ScheduledTask($TaskName) {
+ # SCHTASKS /Change [/S system [/U username [/P [password]]]] /TN taskname
+ # { [/RU runasuser] [/RP runaspassword] [/TR taskrun] [/ST starttime]
+ # [/RI interval] [ {/ET endtime | /DU duration} [/K] ]
+ # [/SD startdate] [/ED enddate] [/ENABLE | /DISABLE] [/IT] [/Z] }
+ # [/HRESULT] [/?]
+ Invoke-CommandLine -FilePath "$($env:SystemRoot)\System32\schtasks.exe" `
+ -ArgumentList @( "/Change", "/S", "localhost", "/TN", "`"$TaskName`"", "/DISABLE" );
+}
+
Function ScheduledTask-Exists($taskName) {
$schedule = new-object -com Schedule.Service
$schedule.connect()
@@ -205,8 +215,28 @@
Create-ScheduledTask $taskName $runAsUser $runAsPassword $command $arguments $schedule $startTime $startDate $runWithElevatedPermissions $days $interval $duration $Modifier
Write-Output "Successfully Created $taskName"
+
+ if( $OctopusParameters.ContainsKey('TaskStatus') )
+ {
+ $taskStatus = $OctopusParameters['TaskStatus']
+ if( -not (StringIsNullOrWhiteSpace($taskStatus)) )
+ {
+ if ( $taskStatus -eq "ENABLE" )
+ {
Enable-ScheduledTask($taskName)
Write-Output "$taskName enabled"
+ }
+ elseif ( $taskStatus -eq "DISABLE" )
+ {
+ Disable-ScheduledTask($taskName)
+ Write-Output "$taskName disabled"
+ }
+ else
+ {
+ Write-Output "$taskName status unchanged (on create, will be enabled)"
+ }
+ }
+ }
if( $OctopusParameters.ContainsKey("StartNewTaskNow") )
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment