Skip to content

Instantly share code, notes, and snippets.

@gsuttie
Created July 30, 2015 09:27
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 gsuttie/bdf164dea1f67a590084 to your computer and use it in GitHub Desktop.
Save gsuttie/bdf164dea1f67a590084 to your computer and use it in GitHub Desktop.
create a scheduled task using PowerShell
#use http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx for API reference
Function Create-ScheduledTask($TaskName,$RunAsUser,$TaskRun,$Schedule,$StartTime,$StartDate,$user, $password){
$cmdStartDate = if([string]::IsNullOrWhiteSpace($StartDate)){""}else{"/sd $StartDate"}
$cmdStartTime = if([string]::IsNullOrWhiteSpace($StartTime)){""}else{"/st $StartTime"}
$cmdInterval = if([string]::IsNullOrWhiteSpace($Interval)){""}else{"/ri $Interval"}
$cmdDuration = if([string]::IsNullOrWhiteSpace($Duration)){""}else{"/du $Duration"}
$Command = "schtasks.exe /create /ru $User /rp $Password /tn `"$TaskName`" /tr `"'$($TaskRun)'`" /sc $Schedule $cmdStartDate $cmdStartTime /F $cmdInterval $cmdDuration"
echo $Command
Invoke-Expression $Command
}
Function Delete-ScheduledTask($TaskName) {
$Command = "schtasks.exe /delete /s localhost /tn `"$TaskName`" /F"
Invoke-Expression $Command
}
Function Stop-ScheduledTask($TaskName) {
$Command = "schtasks.exe /end /s localhost /tn `"$TaskName`""
Invoke-Expression $Command
}
Function Start-ScheduledTask($TaskName) {
$Command = "schtasks.exe /run /s localhost /tn `"$TaskName`""
Invoke-Expression $Command
}
Function Enable-ScheduledTask($TaskName) {
$Command = "schtasks.exe /change /s localhost /tn `"$TaskName`" /ENABLE"
Invoke-Expression $Command
}
Function ScheduledTask-Exists($taskName) {
$schedule = new-object -com Schedule.Service
$schedule.connect()
$tasks = $schedule.getfolder("\").gettasks(0)
foreach ($task in ($tasks | select Name)) {
#echo "TASK: $($task.name)"
if($task.Name -eq $taskName) {
#write-output "$task already exists"
return $true
}
}
return $false
}
#$taskName = $OctopusParameters['TaskName']
#$runAsUser = $OctopusParameters['RunAsUser']
#$command = $OctopusParameters['Command']
#$schedule = $OctopusParameters['Schedule']
#$startTime = $OctopusParameters['StartTime']
#$startDate = $OctopusParameters['StartDate']
#$interval = $OctopusParameters['Interval']
#$duration = $OctopusParameters['Duration']
#$User = $OctopusParameters['User']
#$Password = $OctopusParameters['Password']
$taskName = 'test task'
#$command = $OctopusParameters['Command']
$schedule = 'Weekly'
$startTime = "03:00"
$startDate = "09/04/2015"
#$interval = $OctopusParameters['Interval']
#$duration = $OctopusParameters['Duration']
$User = "usernamegoeshere"
$Password = "pwdgoeshere"
if((ScheduledTask-Exists($taskName))){
Write-Output "$taskName already exists, Tearing down..."
Write-Output "Stopping $taskName..."
Stop-ScheduledTask($taskName)
Write-Output "Successfully Stopped $taskName"
Write-Output "Deleting $taskName..."
Delete-ScheduledTask($taskName)
Write-Output "Successfully Deleted $taskName"
}
Write-Output "Creating Scheduled Task - $taskName"
Create-ScheduledTask $taskName $runAsUser $command $schedule $startTime $startDate $user $password
Write-Output "Successfully Created $taskName"
Enable-ScheduledTask($taskName)
Write-Output "$taskName enabled"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment