Skip to content

Instantly share code, notes, and snippets.

@randyburden
Created November 8, 2017 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randyburden/b1d5e6273b416e3a610642d84f4f8541 to your computer and use it in GitHub Desktop.
Save randyburden/b1d5e6273b416e3a610642d84f4f8541 to your computer and use it in GitHub Desktop.
Registers Windows Scheduled Tasks. Taken from a working example I created that ran as a post-deployment step. It also cleans up old deployment directories.
##########################################
### Registers Windows Scheduled Tasks ###
##########################################
<###############################################################################################################
Notes:
- This example was from a working solution I created where this script was run as a post-deploy step for a
console application that served as the entry point for multiple scheduled tasks that could be executed
by supplying the task name.
- Google "SchTasks" for instructions on how to register scheduled tasks
- If you want to manually run these commands run them in a PowerShell window opened as Administrator
- The "/F" (Force) argument needs to be included as it basically functions as a "Create or Update" command
- The "/SD 01/01/2017" (Start Date) and "/ST" arguments need to be included to enable the runs to be bound
to a specific starting point otherwise each time a deployment occurs it will reset the next run time
based off the current deployment time. When choosing a start time, consider picking a time slot that
isn't already chosen in order to ensure the server doesn't get overwhelmed with all tasks running at once.
This will require staggering start times to ensure runs are offset from each other.
###############################################################################################################>
[CmdletBinding()]
Param(
# Example: D:\programs\MyApplicationName\MyApplicationName-23\bin\Release\
[Parameter(Mandatory=$True,Position=1)]
[string]$directoryPath,
[Parameter(Mandatory=$False)]
[switch]$someOptionalParameterName
)
$executableName = "MyApplicationName.exe"
$pathToExecutable = Join-Path $directoryPath $executableName
Write-Output "Path to executable: $pathToExecutable"
# Run "TaskName1" every hour
SchTasks /Create /TN "$executableName --TaskName TaskName1" /F /SC HOURLY /MO 1 /SD "01/01/2017" /ST 12:00 /TR "$pathToExecutable --TaskName TaskName1" /RU SYSTEM
# Run "TaskName2" every 15 minutes
SchTasks /Create /TN "$executableName --TaskName TaskName2" /F /SC MINUTE /MO 15 /SD "01/01/2017" /ST 12:05 /TR "$pathToExecutable --TaskName TaskName2" /RU SYSTEM
# Run "TaskName3" every 30 min
SchTasks /Create /TN "$executableName --TaskName TaskName3" /F /SC MINUTE /MO 30 /SD "01/01/2017" /ST 12:10 /TR "$pathToExecutable --TaskName TaskName3" /RU SYSTEM
# Run "TaskName4" every 24 hours
SchTasks /Create /TN "$executableName --TaskName TaskName4" /F /SC DAILY /MO 1 /SD "01/01/2017" /ST 12:15 /TR "$pathToExecutable --TaskName TaskName4" /RU SYSTEM
# This was a task that should only run in certain environments so the release process would only pass this optional parameter for those particular environments
if ($someOptionalParameterName) {
# Run "TaskName5" every 4 hours
SchTasks /Create /TN "$executableName --TaskName TaskName5" /F /SC HOURLY /MO 4 /SD "01/01/2017" /ST 12:20 /TR "$pathToExecutable --TaskName TaskName5" /RU SYSTEM
}
###############################
### Cleanup Old Deployments ###
###############################
<###############################################################################################################
Notes:
- The deployment process creates a new folder each time it is released and thus we need to cleanup the
old folders no longer in use. The script below deletes all but the last 2 created directories. We keep
the last 2 directories because the 2nd most recent directory may still be in use by running tasks.
###############################################################################################################>
# Delete all but the last 2 created directories
$directoryPath = "D:\programs\MyApplicationName\"
$directories = Get-ChildItem -Path $directoryPath |
Where-Object { $_.PsIsContainer } |
Sort-Object CreationTime -Descending |
Select-Object -skip 2
Foreach ($directory in $directories) {
Write-Output "Deleting directory: $($directory.FullName)"
Remove-Item $directory.FullName –Force -Recurse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment