【问题标题】:Octopus Deploy Windows Scheduled TaskOctopus 部署 Windows 计划任务
【发布时间】:2017-03-27 22:30:32
【问题描述】:

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

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

有没有人遇到过必须在定时任务中指定“Start in (optional):”参数的情况?

我想知道 Octopus Deploy 是否可以做到这一点,或者是否有任何解决方法?

【问题讨论】:

  • 意思是“开始于(可选):”
  • 章鱼回复:我前不久也有人问过这个问题,遗憾的是步骤模板大多是由社区创建和管理的。看起来您最好的选择是创建步骤模板的副本并在您自己的部分中添加/脚本以编辑“开始于”字段。可悲的是,目前这里没有任何其他选择。我们的步骤模板非常依赖社区贡献。如果您这样做,我们很乐意看到您修改后的 PR。以下链接包含有关如何提交到我们图书馆的信息:github.com/OctopusDeploy/Library

标签: octopus-deploy


【解决方案1】:

Octopus 部署社区步骤只是带有变量的 Powershell 脚本。您可以编辑 Powershell 为“Start in”路径设置任何变量,并将其传递给计划任务。我可以给你一个你需要的例子。

更新

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

更新 2

所以我写了一个自定义步骤来做你想做的事,然后看了社区提要,我傻了。已经有 stemp 模板可以从 XML 文件创建计划任务。 XML 将允许您设置工作目录。步骤模板称为“从 XML 创建计划任务”,您可以在 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
}

【讨论】:

  • 感谢博彦,示例会有所帮助。
  • 您是在 Windows 2012 还是 2016 上运行? Powershell 3 有一个新的 API,可以更轻松地创建任务,但 v3 默认仅在 Windows 8、10、2012 和 2016 中提供。
  • 我在 Windows 2012 上运行。
【解决方案2】:

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

我做了以下事情:

  1. 使用 Octopus 模板创建计划任务(Windows 计划任务 - 创建 - 使用密码)
  2. 使用 PowerShell 将计划任务保存为 XML
  3. 编辑 XML 以添加工作目录
  4. 使用 Octopus 模板(从 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-18
    • 2016-02-10
    • 2010-09-28
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多