Last active
August 29, 2015 14:16
-
-
Save bradymholt/8b1ae540c65ce898bcfa to your computer and use it in GitHub Desktop.
Octopus PostDeploy script that sets up Scheduled Tasks using Task Scheduler definition files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #PostDeploy.ps1 - This script is called by Octopus Deploy and is responsible for setting up | |
| # Scheduled Tasks to execute jobs. The task definitions are located in \Tasks folder. | |
| # This file should be located in the root of the jobs projects and have Build Action marked to "Content" | |
| # and Copy to Output Directory marked "Copy if newer". | |
| $exePath = (Get-Item -Path ".\" -Verbose).FullName + "\.JobExecutor.exe" | |
| function CreateScheduleXml($sourceXmlPath, $outXmlPath) | |
| { | |
| $xml = [xml](Get-Content $sourceXmlPath) | |
| $ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) | |
| $ns.AddNamespace("ns", "http://schemas.microsoft.com/windows/2004/02/mit/task") | |
| $xml.SelectNodes("/ns:Task//ns:Actions/ns:Exec/ns:Command", $ns)[0].InnerText = $exePath | |
| $xml.Save($outXmlPath); | |
| } | |
| #tasksPath should contain Task Scheduler .xml definition files (exported from Task Scheduler) | |
| $tasksPath = (Get-Item -Path ".\" -Verbose).FullName + "\Tasks" | |
| $tempJobXmlPath = "job.xml" | |
| Get-ChildItem $tasksPath -Filter *.xml | ` | |
| Foreach-Object{ | |
| If ($_.Name -ne $tempJobXmlPath) { | |
| $taskName = $_.BaseName | |
| CreateScheduleXml $_.FullName $tempJobXmlPath | |
| schtasks /delete /tn "${taskName}" /f | |
| schtasks /create /tn "${taskName}" /ru "NT AUTHORITY\NETWORKSERVICE" /xml "${tempJobXmlPath}" /f | |
| Remove-Item $tempJobXmlPath | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment