Windows scheduled task created by Puppet

scheduled tasks

A scheduled task is a computer program or script that is scheduled to run at a predetermined time or when a specific event occurs. You can use the Task Scheduler in Windows to create and manage scheduled tasks. This can be useful for automating certain tasks or programs that you need to run on a regular basis. For example, you might create a scheduled task to run a backup program every night or to check for updates to your antivirus software every week. To create a scheduled task in Windows, you can use the Task Scheduler program that is included with Windows.

Windows scheduled task

While working with Puppet in Windows environment sooner or later one will need to create a scheduled task. Especially, it might be very useful to execute activity that requires elevation to local administrator. Below I am presenting two examples for scheduled task creation by Puppet. The first is typical backup task. It could be used to backup either file system or database. The second shows SQL server instance installation. It requires elevated permission that can be vary easily achieve by using scheduled task.

BackupTask

  • It is scheduled daily at 9 PM
  • ${msaaccount} it is user used to run the task. Its value is set in Puppet external facts for windows
  • d:\Backup.ps1 it is location of script that is going to be run by the task
exec { 'CreateTask_BackupTask':
  command => "Register-ScheduledTask -Action (New-ScheduledTaskAction -Execute 'c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe' -Argument '-executionpolicy bypass -file d:\\Backup.ps1) -Trigger (New-ScheduledTaskTrigger -Daily -At 9pm) -TaskName BackupTask -Principal (New-ScheduledTaskPrincipal -UserId '${msaaccount}' -LogonType Password) -Settings (New-ScheduledTaskSettingsSet)",
  unless => 'if (Get-ScheduledTask -TaskName "BackupTask") { Exit 0 } else { Exit 1 }',
  provider => powershell,
  logoutput => true,
  require => File['d:\Backup.ps1'],
}

InstallSqlInstance

  • It is scheduled to be run once 1 min from now
  • ${domainuser} and ${domainpassword it is user, and its password, to run the task. Their values are set in Puppet hiera data.
  • User that runs the taks is elevated to local administrator
  • MSSQLSERVER is default SQL Instance, for named instance adjustment is needed
  • d:\installsqlinst.ps1 it is location of the script that is going to be run by the task
exec { 'CreateTask_InstallSqlInstance':
  command => "Unregister-ScheduledTask -TaskName InstallSqlInstance -Confirm:\$false -ErrorAction SilentlyContinue;Register-ScheduledTask -Action (New-ScheduledTaskAction -Execute 'c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe' -Argument '-executionpolicy bypass -file d:\\installsqlinst.ps1') -Trigger (New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1).ToString(\"HH:mm\")) -TaskName InstallSqlInstance -User \"${domainuser}\" -Password \"${domainpassword}\" -RunLevel Highest",
  unless => 'if ( (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server" -ErrorAction SilentlyContinue).InstalledInstances -eq "MSSQLSERVER" ) { Exit 0 } else { Exit 1 }',
  provider => powershell,
  logoutput => true,
  require => File['d:\installsqlinst.ps1'],
}

Definitely, there are more cases to use Windows scheduled task. The examples showed above can be adjusted to serve other purposes as well.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *