Octopus部署Windows计划任务

时间:2017-03-27 22:30:33

标签: octopus-deploy

这个问题适用于所有使用Octopus Deploy运行计划任务的人。

https://library.octopusdeploy.com/step-template/actiontemplate-windows-scheduled-task-create

是否有人遇到过必须在计划任务中指定“开始(可选):”参数的情况?

我想知道Octopus Deploy是否可行,或者是否有任何解决方法?

2 个答案:

答案 0 :(得分:2)

Octopus部署社区步骤只是带有变量的Powershell脚本。您可以编辑Powershell以设置任何变量为"开始于"路径并将其传递给计划任务。我可以举个例子说明你需要一个。

<强>更新

在准确查看任务的Posh脚本之后,我认为更好的选择是为XML文件添加一个参数,用于定义任务参数并在Octopus部署步骤中设置该参数。这将为您提供最大的可修复性,以防您需要提供除&#34; Start in&#34;之外的任何其他参数。参数。

更新2

所以我写了一个自定义Step来做你想做的事情,然后查看社区提要,傻我。已有stemp模板从XML文件创建计划任务。 XML将允许您设置工作目录。步骤模板称为&#34;从XML创建计划任务&#34;你可以在http://library.octopusdeploy.com/step-templates/26c779af-4cce-447e-98bb-4741c25e0b3c/actiontemplate-create-scheduled-tasks-from-xml找到它。

此外,这是我使用自定义步骤的地方,它只是Powershell:

$ErrorActionPreference = "Stop";
Set-StrictMode -Version "Latest";

function New-ScheduledTask {
    param (
        [Parameter(Mandatory = $true)][hashtable] $octopusParameters
    )

    $arguments = @{
        TaskName = $octopusParameters['TaskName']
        User = $octopusParameters['RunAsUser']
    }

    if ($octopusParameters['RunAsPassword']) {
        $arguments.Password = $runAsPassword
    }

    if ($octopusParameters.ContainsKey('RunWithElevatedPermissions')) {
        if ([boolean]::Parse($octopusParameters['RunWithElevatedPermissions'])) {
            $arguments.RunLevel = 'Highest'
        }
    }

    switch ($octopusParameters['Schedule']) {
        'Once' {
            $triggerArguments.Once = $true
            $triggerArguments.At = $runAt
        }
        'Daily' {
            $triggerArguments.Daily = $true
            $triggerArguments.At = $runAt

            if ($interval) {
                $triggerArguments.DaysInterval = $octopusParameters['Interval']
            }
        }
        'Weekly' {
            $triggerArguments.Weekly = $true
            $triggerArguments.At = $runAt

            if ($interval) {
                $triggerArguments.WeeksInterval = $octopusParameters['Interval']
            }
        }
        'Startup' {
            $triggerArguments.AtStartup = $true
        }
        'Logon' {
            $triggerArguments.AtLogOn = $true
        }
    }

    $actionArguments = @{
        Execute = $octopusParameters['Executable']
        Argument = $octopusParameters['Arguments']
        WorkingDirectory = $octopusParameters['WorkingDirectory']
    }
    $arguments.Action = New-ScheduledTaskAction @actionArguments

    $triggerArguments = @{
        TaskName = $taskName
        User = $runAsUser
    }
    $arguments.Trigger = New-ScheduledTaskTrigger @triggerArguments

    Write-Output "Creating Scheduled Task - $taskName"

    Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction:SilentlyContinue
    Register-ScheduledTask @arguments | Out-Null

    Write-Output "Successfully Created $taskName"
}

# only execute the step if it's called from octopus deploy,
# and skip it if we're runnning inside a Pester test
if (Test-Path -Path "Variable:octopusParameters") {
    New-ScheduledTask $octopusParameters
}

答案 1 :(得分:1)

遇到相同问题后,我发现schtasks.exe没有使用“工作目录”(“开始于”(可选))参数。

我做了以下事情:

  1. 使用八达通模板创建计划任务(Windows计划任务-创建-使用密码)
  2. 使用PowerShell将计划任务保存为XML
  3. 编辑XML以添加工作目录
  4. 使用八达通模板(通过XML创建计划任务),并使用更新的XML创建计划任务。

这是我在Octopus中使用的PowerShell,以XML形式获取计划任务并插入工作目录节点:

$scheduleFolder = $OctopusParameters["ScheduledTaskFolder"]
$scheduleName = $OctopusParameters["ScheduledTaskName"]
$scheduleWorkingDirectory = $OctopusParameters["ScheduledTaskWorkingDirectory"]
$scheduleXmlFileName = $OctopusParameters["ScheduledTaskXmlFileName"]
$installFolder = $OctopusParameters["InstallFolder"]

Write-Output "Connecting to Schedule Service"
$schedule = New-Object -Com("Schedule.Service") 
$schedule.Connect()

Write-Output "Getting $scheduleName task in folder $scheduleFolder as xml"
$task = $schedule.GetFolder($scheduleFolder).GetTasks(0) | Where {$_.Name -eq 
$scheduleName}

$xml = [xml]$task.Xml

# Parent node
$execNode = $xml.Task.Actions.Exec

# Create WorkingDirectory node
$workingDirectoryElement = $xml.CreateElement("WorkingDirectory", 
$execNode.NamespaceURI) 
$workingDirectoryElement.InnerText = $scheduleWorkingDirectory

# Insert the WorkingDirectory node after the last child node
Write-Output "Inserting WorkingDirectory node in $execNode.Name node"
$numberExecNodes = $execNode.ChildNodes.Count

$execNode.InsertAfter($workingDirectoryElement, $execNode.ChildNodes[$numberExecNodes 
- 1])

# Output the xml to a file
Write-Output "Saving $installFolder\$scheduleXmlFileName"
$xml.Save("$installFolder\$scheduleXmlFileName")

另一种选择是将XML文件(带有工作目录节点)保存为项目的一部分,并仅使用Octopus模板(从XML创建计划任务)进行部署。

...
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Program Files\Test Application\Application.exe"</Command>
      <WorkingDirectory>C:\Program Files\Test Application</WorkingDirectory>
    </Exec>
  </Actions>
</Task>
相关问题