Skip to content

Commit

Permalink
Merge pull request OctopusDeploy#690 from MichaelThorBateman/master
Browse files Browse the repository at this point in the history
Add ability to specify MO & RI arguments with any schedule.
  • Loading branch information
hnrkndrssn authored Jun 13, 2018
2 parents 0cfc737 + eb2afb3 commit 219d341
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions step-templates/windows-scheduled-task-create.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"Name": "Windows Scheduled Task - Create",
"Description": "Create Windows scheduled task. If the task exists it will be torn down and re-added to ensure consistency",
"ActionType": "Octopus.Script",
"Version": 16,
"Version": 17,
"Properties": {
"Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = \"Stop\";\nSet-StrictMode -Version \"Latest\";\n\n# use http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx for API reference\n\nFunction Create-ScheduledTask($TaskName,$RunAsUser,$RunAsPassword,$TaskRun,$Arguments,$Schedule,$StartTime,$StartDate,$RunWithElevatedPermissions,$Days,$Interval,$Duration)\n{\n\n # SCHTASKS /Create [/S system [/U username [/P [password]]]]\n # [/RU username [/RP password]] /SC schedule [/MO modifier] [/D day]\n # [/M months] [/I idletime] /TN taskname /TR taskrun [/ST starttime]\n # [/RI interval] [ {/ET endtime | /DU duration} [/K] [/XML xmlfile] [/V1]]\n # [/SD startdate] [/ED enddate] [/IT | /NP] [/Z] [/F] [/HRESULT] [/?]\n\n # note - /RL and /DELAY appear in the \"Parameter list\" for \"SCHTASKS /Create /?\" but not in the syntax above\n\n $argumentList = @();\n $argumentList += @( \"/Create\" );\n\n $argumentList += @( \"/RU\", $RunAsUser );\n \n if( -not (StringIsNullOrWhiteSpace($RunAsPassword)))\n {\n $argumentList += @( \"/RP\", $RunAsPassword );\n }\n\n $argumentList += @( \"/SC\", $Schedule );\n\n if( -not (StringIsNullOrWhiteSpace($Interval)) )\n {\n switch -Regex ($Schedule)\n {\n \"MINUTE|HOURLY|ONLOGON|ONIDLE\" {\n $argumentList += @( \"/MO\", $Interval );\n }\n \"WEEKLY|MONTHLY\" {\n $argumentList += @( \"/RI\", $Interval );\n }\n \"ONCE|ONSTART|ONEVENT\" {\n # we don't currently support providing an XPATH query string\n throw new-object System.NotImplementedException(\"Unsupported schedule option '$Schedule'.\");\n }\n }\n }\n\n if( -not (StringIsNullOrWhiteSpace($Days)) -And $Schedule -Ne \"DAILY\" )\n {\n if($Schedule -ne \"WEEKDAYS\") {\n $argumentList += @( \"/D\", $Days );\n } else {\n $argumentList += @( \"/D\", \"MON,TUE,WED,THU,FRI\" );\n }\n }\n\n $argumentList += @( \"/TN\", \"`\"$TaskName`\"\" );\n\n if( $Arguments )\n {\n $argumentList += @( \"/TR\", \"`\"'$TaskRun' $Arguments`\"\" );\n }\n else\n {\n $argumentList += @( \"/TR\", \"`\"'$TaskRun'`\"\" );\n }\n\n if( -not (StringIsNullOrWhiteSpace($StartTime)) )\n {\n $argumentList += @( \"/ST\", $StartTime );\n }\n\n if( -not (StringIsNullOrWhiteSpace($Duration)) )\n {\n $argumentList += @( \"/DU\", $Duration );\n }\n\n if( -not (StringIsNullOrWhiteSpace($StartDate)) )\n {\n $argumentList += @( \"/SD\", $StartDate );\n }\n\n $argumentList += @( \"/F\" );\n\n if( $RunWithElevatedPermissions )\n {\n $argumentList += @( \"/RL\", \"HIGHEST\" );\n }\n\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList $argumentList;\n\n}\n\nFunction Delete-ScheduledTask($TaskName) {\n # SCHTASKS /Delete [/S system [/U username [/P [password]]]]\n # /TN taskname [/F] [/HRESULT] [/?]\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList @( \"/Delete\", \"/S\", \"localhost\", \"/TN\", \"`\"$TaskName`\"\", \"/F\" );\n}\n\nFunction Stop-ScheduledTask($TaskName) {\n # SCHTASKS /End [/S system [/U username [/P [password]]]]\n # /TN taskname [/HRESULT] [/?]\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList @( \"/End\", \"/S\", \"localhost\", \"/TN\", \"`\"$TaskName`\"\" );\n}\n\nFunction Start-ScheduledTask($TaskName) {\n # SCHTASKS /Run [/S system [/U username [/P [password]]]] [/I]\n # /TN taskname [/HRESULT] [/?]\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList @( \"/Run\", \"/S\", \"localhost\", \"/TN\", \"`\"$TaskName`\"\" );\n}\n\nFunction Enable-ScheduledTask($TaskName) {\n # SCHTASKS /Change [/S system [/U username [/P [password]]]] /TN taskname\n # { [/RU runasuser] [/RP runaspassword] [/TR taskrun] [/ST starttime]\n # [/RI interval] [ {/ET endtime | /DU duration} [/K] ]\n # [/SD startdate] [/ED enddate] [/ENABLE | /DISABLE] [/IT] [/Z] }\n # [/HRESULT] [/?]\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList @( \"/Change\", \"/S\", \"localhost\", \"/TN\", \"`\"$TaskName`\"\", \"/ENABLE\" );\n}\n\nFunction ScheduledTask-Exists($taskName) {\n $schedule = new-object -com Schedule.Service\n $schedule.connect()\n $tasks = $schedule.getfolder(\"\\\").gettasks(0)\n foreach ($task in ($tasks | select Name)) {\n #echo \"TASK: $($task.name)\"\n if($task.Name -eq $taskName) {\n #write-output \"$task already exists\"\n return $true\n }\n }\n return $false\n}\n\nFunction StringIsNullOrWhitespace([string] $string)\n{\n if ($string -ne $null) { $string = $string.Trim() }\n return [string]::IsNullOrEmpty($string)\n}\n\nfunction Invoke-CommandLine\n{\n param\n (\n [Parameter(Mandatory=$true)]\n [string] $FilePath,\n [Parameter(Mandatory=$false)]\n [string[]] $ArgumentList = @( ),\n [Parameter(Mandatory=$false)]\n [string[]] $SuccessCodes = @( 0 )\n )\n write-host ($FilePath + \" \" + ($ArgumentList -join \" \"));\n $process = Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -Wait -NoNewWindow -PassThru;\n if( $SuccessCodes -notcontains $process.ExitCode )\n {\n throw new-object System.InvalidOperationException(\"process terminated with exit code '$($process.ExitCode)'.\");\n }\n}\n\nfunction Invoke-OctopusStep\n{\n param\n (\n [Parameter(Mandatory=$true)]\n [hashtable] $OctopusParameters\n )\n\n $taskName = $OctopusParameters['TaskName']\n $runAsUser = $OctopusParameters['RunAsUser']\n $runAsPassword = $OctopusParameters['RunAsPassword']\n $command = $OctopusParameters['Command']\n $arguments = $OctopusParameters['Arguments']\n $schedule = $OctopusParameters['Schedule']\n $startTime = $OctopusParameters['StartTime']\n $startDate = $OctopusParameters['StartDate']\n\n if( $OctopusParameters.ContainsKey(\"RunWithElevatedPermissions\") )\n {\n $runWithElevatedPermissions = [boolean]::Parse($OctopusParameters['RunWithElevatedPermissions'])\n }\n else\n {\n $runWithElevatedPermissions = $false;\n }\n\n $days = $OctopusParameters['Days']\n $interval = $OctopusParameters['Interval']\n $duration = $OctopusParameters['Duration']\n\n if((ScheduledTask-Exists($taskName))){\n Write-Output \"$taskName already exists, Tearing down...\"\n Write-Output \"Stopping $taskName...\"\n Stop-ScheduledTask($taskName)\n Write-Output \"Successfully Stopped $taskName\"\n Write-Output \"Deleting $taskName...\"\n Delete-ScheduledTask($taskName)\n Write-Output \"Successfully Deleted $taskName\"\n }\n Write-Output \"Creating Scheduled Task - $taskName\"\n\n Create-ScheduledTask $taskName $runAsUser $runAsPassword $command $arguments $schedule $startTime $startDate $runWithElevatedPermissions $days $interval $duration\n Write-Output \"Successfully Created $taskName\"\n Enable-ScheduledTask($taskName)\n Write-Output \"$taskName enabled\"\n\n}\n\n\n# only execute the step if it's called from octopus deploy,\n# and skip it if we're runnning inside a Pester test\nif( Test-Path -Path \"Variable:OctopusParameters\" )\n{\n Invoke-OctopusStep -OctopusParameters $OctopusParameters;\n}\n",
"Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = \"Stop\";\nSet-StrictMode -Version \"Latest\";\n\n# use http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx for API reference\n\nFunction Create-ScheduledTask($TaskName,$RunAsUser,$RunAsPassword,$TaskRun,$Arguments,$Schedule,$StartTime,$StartDate,$RunWithElevatedPermissions,$Days,$Interval,$Duration, $Modifier)\n{\n\n # SCHTASKS /Create [/S system [/U username [/P [password]]]]\n # [/RU username [/RP password]] /SC schedule [/MO modifier] [/D day]\n # [/M months] [/I idletime] /TN taskname /TR taskrun [/ST starttime]\n # [/RI interval] [ {/ET endtime | /DU duration} [/K] [/XML xmlfile] [/V1]]\n # [/SD startdate] [/ED enddate] [/IT | /NP] [/Z] [/F] [/HRESULT] [/?]\n\n # note - /RL and /DELAY appear in the \"Parameter list\" for \"SCHTASKS /Create /?\" but not in the syntax above\n\n $argumentList = @();\n $argumentList += @( \"/Create\" );\n\n $argumentList += @( \"/RU\", $RunAsUser );\n \n if( -not (StringIsNullOrWhiteSpace($RunAsPassword)))\n {\n $argumentList += @( \"/RP\", $RunAsPassword );\n }\n\n $argumentList += @( \"/SC\", $Schedule );\n\n if( -not (StringIsNullOrWhiteSpace($Interval)) )\n {\n\t\t$argumentList += @( \"/RI\", $Interval );\n }\n\n if( -not (StringIsNullOrWhiteSpace($Modifier)))\n\t{\n switch -Regex ($Schedule)\n {\n \"MINUTE|HOURLY|DAILY|WEEKLY|MONTHLY|ONEVENT\" {\n $argumentList += @( \"/MO\", $Modifier );\n }\n \"ONCE|ONSTART|ONLOGON|ONIDLE\" {\n $argumentList += @( \"/MO\" );\n }\n }\n\t}\n\n if( -not (StringIsNullOrWhiteSpace($Days)))\n {\n if ($Schedule -ne \"WEEKDAYS\"){\n\t\t\t$argumentList += @( \"/D\", $Days );\n } else {\n $argumentList += @( \"/D\", \"MON,TUE,WED,THU,FRI\" );\n }\n }\n\n $argumentList += @( \"/TN\", \"`\"$TaskName`\"\" );\n\n if( $Arguments )\n {\n $argumentList += @( \"/TR\", \"`\"'$TaskRun' $Arguments`\"\" );\n }\n else\n {\n $argumentList += @( \"/TR\", \"`\"'$TaskRun'`\"\" );\n }\n\n if( -not (StringIsNullOrWhiteSpace($StartTime)) )\n {\n $argumentList += @( \"/ST\", $StartTime );\n }\n\n if( -not (StringIsNullOrWhiteSpace($Duration)) )\n {\n $argumentList += @( \"/DU\", $Duration );\n }\n\n if( -not (StringIsNullOrWhiteSpace($StartDate)) )\n {\n $argumentList += @( \"/SD\", $StartDate );\n }\n\n $argumentList += @( \"/F\" );\n\n if( $RunWithElevatedPermissions )\n {\n $argumentList += @( \"/RL\", \"HIGHEST\" );\n }\n\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList $argumentList;\n\n}\n\nFunction Delete-ScheduledTask($TaskName) {\n # SCHTASKS /Delete [/S system [/U username [/P [password]]]]\n # /TN taskname [/F] [/HRESULT] [/?]\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList @( \"/Delete\", \"/S\", \"localhost\", \"/TN\", \"`\"$TaskName`\"\", \"/F\" );\n}\n\nFunction Stop-ScheduledTask($TaskName) {\n # SCHTASKS /End [/S system [/U username [/P [password]]]]\n # /TN taskname [/HRESULT] [/?]\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList @( \"/End\", \"/S\", \"localhost\", \"/TN\", \"`\"$TaskName`\"\" );\n}\n\nFunction Start-ScheduledTask($TaskName) {\n # SCHTASKS /Run [/S system [/U username [/P [password]]]] [/I]\n # /TN taskname [/HRESULT] [/?]\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList @( \"/Run\", \"/S\", \"localhost\", \"/TN\", \"`\"$TaskName`\"\" );\n}\n\nFunction Enable-ScheduledTask($TaskName) {\n # SCHTASKS /Change [/S system [/U username [/P [password]]]] /TN taskname\n # { [/RU runasuser] [/RP runaspassword] [/TR taskrun] [/ST starttime]\n # [/RI interval] [ {/ET endtime | /DU duration} [/K] ]\n # [/SD startdate] [/ED enddate] [/ENABLE | /DISABLE] [/IT] [/Z] }\n # [/HRESULT] [/?]\n Invoke-CommandLine -FilePath \"$($env:SystemRoot)\\System32\\schtasks.exe\" `\n -ArgumentList @( \"/Change\", \"/S\", \"localhost\", \"/TN\", \"`\"$TaskName`\"\", \"/ENABLE\" );\n}\n\nFunction ScheduledTask-Exists($taskName) {\n $schedule = new-object -com Schedule.Service\n $schedule.connect()\n $tasks = $schedule.getfolder(\"\\\").gettasks(0)\n foreach ($task in ($tasks | select Name)) {\n #echo \"TASK: $($task.name)\"\n if($task.Name -eq $taskName) {\n #write-output \"$task already exists\"\n return $true\n }\n }\n return $false\n}\n\nFunction StringIsNullOrWhitespace([string] $string)\n{\n if ($string -ne $null) { $string = $string.Trim() }\n return [string]::IsNullOrEmpty($string)\n}\n\nfunction Invoke-CommandLine\n{\n param\n (\n [Parameter(Mandatory=$true)]\n [string] $FilePath,\n [Parameter(Mandatory=$false)]\n [string[]] $ArgumentList = @( ),\n [Parameter(Mandatory=$false)]\n [string[]] $SuccessCodes = @( 0 )\n )\n write-host ($FilePath + \" \" + ($ArgumentList -join \" \"));\n $process = Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -Wait -NoNewWindow -PassThru;\n if( $SuccessCodes -notcontains $process.ExitCode )\n {\n throw new-object System.InvalidOperationException(\"process terminated with exit code '$($process.ExitCode)'.\");\n }\n}\n\nfunction Invoke-OctopusStep\n{\n param\n (\n [Parameter(Mandatory=$true)]\n [hashtable] $OctopusParameters\n )\n\n $taskName = $OctopusParameters['TaskName']\n $runAsUser = $OctopusParameters['RunAsUser']\n $runAsPassword = $OctopusParameters['RunAsPassword']\n $command = $OctopusParameters['Command']\n $arguments = $OctopusParameters['Arguments']\n $schedule = $OctopusParameters['Schedule']\n $startTime = $OctopusParameters['StartTime']\n $startDate = $OctopusParameters['StartDate']\n\n if( $OctopusParameters.ContainsKey(\"RunWithElevatedPermissions\") )\n {\n $runWithElevatedPermissions = [boolean]::Parse($OctopusParameters['RunWithElevatedPermissions'])\n }\n else\n {\n $runWithElevatedPermissions = $false;\n }\n\n $days = $OctopusParameters['Days']\n $interval = $OctopusParameters['Interval']\n $duration = $OctopusParameters['Duration']\n $Modifier = $OctopusParameters['Modifier']\n\n if((ScheduledTask-Exists($taskName))){\n Write-Output \"$taskName already exists, Tearing down...\"\n Write-Output \"Stopping $taskName...\"\n Stop-ScheduledTask($taskName)\n Write-Output \"Successfully Stopped $taskName\"\n Write-Output \"Deleting $taskName...\"\n Delete-ScheduledTask($taskName)\n Write-Output \"Successfully Deleted $taskName\"\n }\n Write-Output \"Creating Scheduled Task - $taskName\"\n\n Create-ScheduledTask $taskName $runAsUser $runAsPassword $command $arguments $schedule $startTime $startDate $runWithElevatedPermissions $days $interval $duration $Modifier\n Write-Output \"Successfully Created $taskName\"\n Enable-ScheduledTask($taskName)\n Write-Output \"$taskName enabled\"\n\n}\n\n\n# only execute the step if it's called from octopus deploy,\n# and skip it if we're runnning inside a Pester test\nif( Test-Path -Path \"Variable:OctopusParameters\" )\n{\n Invoke-OctopusStep -OctopusParameters $OctopusParameters;\n}\n",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.ScriptFileName": "",
Expand Down Expand Up @@ -84,6 +84,13 @@
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "Modifier",
"Label": "Modifier",
"HelpText": "A value that specifies how often the task runs within its schedule type. For ONCE, ONSTART, ONLOGON and ONIDLE any value can be used since it will be ignored (They do not use modifiers with MO).",
"DefaultValue": null,
"DisplaySettings": {}
},
{
"Name": "Interval",
"Label": "Interval",
Expand Down Expand Up @@ -118,11 +125,11 @@
}
}
],
"LastModifiedBy": "pdeffendol",
"LastModifiedBy": "MichaelThorBateman",
"$Meta": {
"ExportedAt": "2017-01-05T23:33:10.000Z",
"ExportedAt": "2018-06-08T16:10:46.000Z",
"OctopusVersion": "3.3.27",
"Type": "ActionTemplate"
},
"Category": "windows"
}
}

0 comments on commit 219d341

Please sign in to comment.