管理 Azure 自动化中的计划Manage schedules in Azure Automation

要将 Azure 自动化中的 Runbook 计划为在指定的时间启动,可以将它链接到一个或多个计划。To schedule a runbook in Azure Automation to start at a specified time, you link it to one or more schedules. 可以在 Azure 门户中为 runbook 配置计划,使其运行一次或者每小时或每日定期运行。A schedule can be configured to either run once or on a recurring hourly or daily schedule for runbooks in the Azure portal. 还可以将它们计划为每周、每月、每周的特定几天或每月的特定几天或每月的特定一天运行。You can also schedule them for weekly, monthly, specific days of the week or days of the month, or a particular day of the month. 可将一个 Runbook 链接到多个计划,一个计划可以链接多个 Runbook。A runbook can be linked to multiple schedules, and a schedule can have multiple runbooks linked to it.

备注

Azure 自动化支持夏令时并针对自动化操作进行适当的计划。Azure Automation supports Daylight Savings Time and schedules it appropriately for automation operations.

备注

当前未为 Azure 自动化 DSC 配置启用计划。Schedules currently are not enabled for Azure Automation DSC configurations.

用于访问计划的 PowerShell cmdletPowerShell cmdlets used to access schedules

下表中的 cmdlet 使用 PowerShell 创建和管理自动化计划。The cmdlets in the following table create and manage Automation schedules with PowerShell. 它们作为 Az 模块的一部分提供。They ship as part of the Az modules.

CmdletCmdlets 说明Description
Get-AzAutomationScheduleGet-AzAutomationSchedule 检索计划。Retrieves a schedule.
Get-AzAutomationScheduledRunbookGet-AzAutomationScheduledRunbook 检索计划 Runbook。Retrieves scheduled runbooks.
New-AzAutomationScheduleNew-AzAutomationSchedule 创建新计划。Creates a new schedule.
Register-AzAutomationScheduledRunbookRegister-AzAutomationScheduledRunbook 将 Runbook 与计划相关联。Associates a runbook with a schedule.
Remove-AzAutomationScheduleRemove-AzAutomationSchedule 删除计划。Removes a schedule.
Set-AzAutomationScheduleSet-AzAutomationSchedule 设置现有计划的属性。Sets the properties for an existing schedule.
Unregister-AzAutomationScheduledRunbookUnregister-AzAutomationScheduledRunbook 将 Runbook 与计划取消关联。Dissociates a runbook from a schedule.

创建计划Create a schedule

可以在 Azure 门户中或使用 PowerShell 为 runbook 创建新计划。You can create a new schedule for your runbooks in the Azure portal or with PowerShell. 为了避免影响 runbook 及其自动化进程,你应该首先测试已将计划与专用于测试的自动化帐户进行链接的 runbook。To avoid affecting your runbooks and the processes they automate, you should first test any runbooks that have linked schedules with an Automation account dedicated for testing. 测试将验证计划 runbook 是否继续正常工作。A test validates that your scheduled runbooks continue to work correctly. 如果出现问题,可以在将更新的 runbook 版本迁移到生产环境之前进行故障排除并应用所需的更改。If you see a problem, you can troubleshoot and apply any changes required before you migrate the updated runbook version to production.

备注

自动化帐户不会自动获取模块的任何新版本,除非你通过从模块中选择“更新 Azure 模块”选项来手动更新它们。Your Automation account doesn't automatically get any new versions of modules unless you've updated them manually by selecting the Update Azure modules option from Modules. 当运行新的计划作业时,Azure 自动化将在自动化帐户中使用最新模块。Azure Automation uses the latest modules in your Automation account when a new scheduled job is run.

在 Azure 门户中创建新计划Create a new schedule in the Azure portal

  1. 在自动化帐户的左侧窗格中,选择“共享资源”下的“计划” 。From your Automation account, on the left-hand pane select Schedules under Shared Resources.

  2. 在“计划”页上选择“添加计划”。On the Schedules page, select Add a schedule.

  3. 在“新建计划”页中,输入新计划的名称,还可以选择输入新计划的说明。On the New schedule page, enter a name and optionally enter a description for the new schedule.

    备注

    自动化计划目前不支持在计划名称中使用特殊字符。Automation schedules do not currently support using special characters in the schedule name.

  4. 通过选择“一次”或“定期”来选择该计划是运行一次,还是按计划重复运行。Select whether the schedule runs once or on a reoccurring schedule by selecting Once or Recurring. 如果选择“一次”,请指定开始时间,然后选择“创建” 。If you select Once, specify a start time and then select Create. 如果选择“定期”,请指定开始时间。If you select Recurring, specify a start time. 对于“重复间隔”,请选择想要 runbook 重复运行的频率。For Recur every, select how often you want the runbook to repeat. 选择“时”、“天”、“周”或“月”。Select by hour, day, week, or month.

    • 如果选择“周”,则会显示一周中可供选择的日期。If you select Week, the days of the week are presented for you to choose from. 根据需要选择天数。Select as many days as you want. 计划的第一次运行将在开始时间之后选择的第一天进行。The first run of your schedule will happen on the first day selected after the start time. 例如,若要选择周末计划,请选择“星期六”和“星期日”。For example, to choose a weekend schedule, select Saturday and Sunday.

    设置周末定期计划

    • 如果选择“月”,则会看到不同的选项。If you select Month, you're given different options. 对于“每月进行次数”选项,请选择“每月天数”或“每周天数” 。For the Monthly occurrences option, select either Month days or Week days. 如果选择“每月天数”,则会显示一个可根据需要选择天数的日历。If you select Month days, a calendar appears so that you can choose as many days as you want. 如果选择当月不存在的日期(例如 31 日),则计划将不会运行。If you choose a date such as the 31st that doesn't occur in the current month, the schedule won't run. 如果希望计划在最后一天运行,请在“在月份的最后一天运行”下选择“是” 。If you want the schedule to run on the last day, select Yes under Run on last day of month. 如果选择“每周天数”,则会显示“重复间隔”选项 。If you select Week days, the Recur every option appears. 选择“第一”、“第二”、“第三”、“第四”或“最后” 。Choose First, Second, Third, Fourth, or Last. 最后选择一天进行重复。Finally, choose a day to repeat on.

    在每月第一天、第十五天和最后一天运行的每月计划

  5. 完成后,请选择“创建”。When you're finished, select Create.

使用 PowerShell 创建新计划Create a new schedule with PowerShell

使用 New-AzAutomationSchedule cmdlet 创建计划。Use the New-AzAutomationSchedule cmdlet to create schedules. 必须指定计划的开始时间以及运行频率。You specify the start time for the schedule and the frequency it should run. 以下示例演示如何创建许多不同的计划场景。The following examples show how to create many different schedule scenarios.

备注

自动化计划目前不支持在计划名称中使用特殊字符。Automation schedules do not currently support using special characters in the schedule name.

创建一次性计划Create a one-time schedule

以下示例创建一个一次性计划。The following example creates a one-time schedule.

$TimeZone = ([System.TimeZoneInfo]::Local).Id
New-AzAutomationSchedule -AutomationAccountName "ContosoAutomation" -Name "Schedule01" -StartTime "23:00" -OneTime -ResourceGroupName "ResourceGroup01" -TimeZone $TimeZone

创建定期计划Create a recurring schedule

以下示例演示如何创建每天下午 1:00 运行且持续一年的定期计划。The following example shows how to create a recurring schedule that runs every day at 1:00 PM for a year.

$StartTime = Get-Date "13:00:00"
$EndTime = $StartTime.AddYears(1)
New-AzAutomationSchedule -AutomationAccountName "ContosoAutomation" -Name "Schedule02" -StartTime $StartTime -ExpiryTime $EndTime -DayInterval 1 -ResourceGroupName "ResourceGroup01"

创建每周重复执行计划Create a weekly recurring schedule

以下示例演示如何创建仅在工作日运行的每周计划。The following example shows how to create a weekly schedule that runs on weekdays only.

$StartTime = (Get-Date "13:00:00").AddDays(1)
[System.DayOfWeek[]]$WeekDays = @([System.DayOfWeek]::Monday..[System.DayOfWeek]::Friday)
New-AzAutomationSchedule -AutomationAccountName "ContosoAutomation" -Name "Schedule03" -StartTime $StartTime -WeekInterval 1 -DaysOfWeek $WeekDays -ResourceGroupName "ResourceGroup01"

创建每周定期在周末运行的计划Create a weekly recurring schedule for weekends

以下示例演示如何创建仅在周末运行的每周计划。The following example shows how to create a weekly schedule that runs on weekends only.

$StartTime = (Get-Date "18:00:00").AddDays(1)
[System.DayOfWeek[]]$WeekendDays = @([System.DayOfWeek]::Saturday,[System.DayOfWeek]::Sunday)
New-AzAutomationSchedule -AutomationAccountName "ContosoAutomation" -Name "Weekends 6PM" -StartTime $StartTime -WeekInterval 1 -DaysOfWeek $WeekendDays -ResourceGroupName "ResourceGroup01"

创建在每月的第一天、第十五天和最后一天运行的定期计划Create a recurring schedule for the first, fifteenth, and last days of the month

以下示例演示如何创建在每月的第一天、第十五天和最后一天运行的定期计划。The following example shows how to create a recurring schedule that runs on the first, fifteenth, and last day of a month.

$StartTime = (Get-Date "18:00:00").AddDays(1)
New-AzAutomationSchedule -AutomationAccountName "TestAzureAuto" -Name "1st, 15th and Last" -StartTime $StartTime -DaysOfMonth @("One", "Fifteenth", "Last") -ResourceGroupName "TestAzureAuto" -MonthInterval 1

可将一个 Runbook 链接到多个计划,一个计划可以链接多个 Runbook。A runbook can be linked to multiple schedules, and a schedule can have multiple runbooks linked to it. 如果 Runbook 包含参数,则可以提供这些参数的值。If a runbook has parameters, you can provide values for them. 必须为所有必需参数提供值,还可以为任何可选参数提供值。You must provide values for any mandatory parameters, and you also can provide values for any optional parameters. 此计划每次启动 Runbook 时,都将使用这些值。These values are used each time the runbook is started by this schedule. 可以将同一个 Runbook 附加到另一个计划,并指定不同的参数值。You can attach the same runbook to another schedule and specify different parameter values.

  1. 在 Azure 门户中,从你的自动化帐户中,在“流程自动化”下选择“Runbook” 。In the Azure portal, from your automation account, select Runbooks under Process Automation.
  2. 选择要计划的 runbook 的名称。Select the name of the runbook to schedule.
  3. 如果 runbook 当前未链接到计划,则系统会提供“创建新计划”或“链接到现有计划”选项。If the runbook isn't currently linked to a schedule, you're offered the option to create a new schedule or link to an existing schedule.
  4. 如果 runbook 有参数,可以选择选项“修改运行设置(默认值:Azure)”,此时会显示“参数”窗格 。If the runbook has parameters, you can select the option Modify run settings (Default:Azure) and the Parameters pane appears. 可以在其中输入参数信息。You can enter parameter information here.

使用 Register-AzAutomationScheduledRunbook cmdlet 链接计划。Use the Register-AzAutomationScheduledRunbook cmdlet to link a schedule. 可以使用 Parameters 参数为 Runbook 的参数指定值。You can specify values for the runbook's parameters with the Parameters parameter. 有关如何指定参数值的详细信息,请参阅在 Azure 自动化中启动 RunbookFor more information on how to specify parameter values, see Starting a Runbook in Azure Automation. 以下示例演示了如何使用带参数的 Azure 资源管理器 cmdlet 将计划链接到 runbook。The following example shows how to link a schedule to a runbook by using an Azure Resource Manager cmdlet with parameters.

$automationAccountName = "MyAutomationAccount"
$runbookName = "Test-Runbook"
$scheduleName = "Sample-DailySchedule"
$params = @{"FirstName"="Joe";"LastName"="Smith";"RepeatCount"=2;"Show"=$true}
Register-AzAutomationScheduledRunbook -AutomationAccountName $automationAccountName `
-Name $runbookName -ScheduleName $scheduleName -Parameters $params `
-ResourceGroupName "ResourceGroup01"

计划 runbook 以使其更频繁地运行Schedule runbooks to run more frequently

可为 Azure 自动化中的计划配置的最频繁间隔为一小时。The most frequent interval for which a schedule in Azure Automation can be configured is one hour. 如果你需要运行比这更频繁的计划,有两个选项:If you require schedules to run more frequently than that, there are two options:

  • 为 runbook 创建 Webhook,并使用 Azure 逻辑应用调用 webhook。Create a webhook for the runbook, and use Azure Logic Apps to call the webhook. Azure 逻辑应用提供更精细的粒度来定义计划。Azure Logic Apps provides more fine-grained granularity to define a schedule.

  • 创建四个计划,每个计划在 15 分钟内开始,每小时运行一次。Create four schedules that all start within 15 minutes of each other and run once every hour. 此方案使用不同的计划,可让 Runbook 每隔 15 分钟运行一次。This scenario allows the runbook to run every 15 minutes with the different schedules.

禁用计划Disable a schedule

禁用计划后,链接到该计划的所有 Runbook 不再按该计划运行。When you disable a schedule, any runbook linked to it no longer runs on that schedule. 可以手动禁用计划,也可以在创建带频率的计划时设置其过期时间。You can manually disable a schedule or set an expiration time for schedules with a frequency when you create them. 到达过期时间时,会禁用该计划。When the expiration time is reached, the schedule is disabled.

从 Azure 门户禁用计划Disable a schedule from the Azure portal

  1. 在你的自动化帐户中的“共享资源”下选择“计划” 。In your Automation account, select Schedules under Shared Resources.
  2. 选择某个计划的名称以打开详细信息窗格。Select the name of a schedule to open the details pane.
  3. 将“已启用”更改为“否”。Change Enabled to No.

备注

如果要禁用开始时间为过去时间的计划,则必须在保存之前将开始日期更改为将来的时间。If you want to disable a schedule that has a start time in the past, you must change the start date to a time in the future before you save it.

使用 PowerShell 禁用计划Disable a schedule with PowerShell

可使用 Set-AzAutomationSchedule cmdlet 更改现有计划的属性。Use the Set-AzAutomationSchedule cmdlet to change the properties of an existing schedule. 若要禁用计划,请为 IsEnabled 参数指定 False。To disable the schedule, specify False for the IsEnabled parameter.

以下示例演示了如何使用 Azure 资源管理器 cmdlet 禁用 runbook 的计划。The following example shows how to disable a schedule for a runbook by using an Azure Resource Manager cmdlet.

$automationAccountName = "MyAutomationAccount"
$scheduleName = "Sample-MonthlyDaysOfMonthSchedule"
Set-AzAutomationSchedule -AutomationAccountName $automationAccountName `
-Name $scheduleName -IsEnabled $false -ResourceGroupName "ResourceGroup01"

删除计划Remove a schedule

准备删除计划时,可以使用 Azure 门户或 PowerShell。When you're ready to remove your schedules, you can either use the Azure portal or PowerShell. 请记住,只能删除已禁用的计划,如前一部分中所述。Remember that you can only remove a schedule that has been disabled as described in the previous section.

使用 Azure 门户删除计划Remove a schedule using the Azure portal

  1. 在自动化帐户的左侧窗格中,选择“共享资源”下的“计划” 。In your Automation account, on the left-hand pane select Schedules under Shared Resources.
  2. 选择某个计划的名称以打开详细信息窗格。Select the name of a schedule to open the details pane.
  3. 单击 “删除”Click Delete.

使用 PowerShell 删除计划Remove a schedule with PowerShell

可以按如下所示使用 Remove-AzAutomationSchedule cmdlet 来删除现有计划。You can use the Remove-AzAutomationSchedule cmdlet as shown below to delete an existing schedule.

$automationAccountName = "MyAutomationAccount"
$scheduleName = "Sample-MonthlyDaysOfMonthSchedule"
Remove-AzAutomationSchedule -AutomationAccountName $automationAccountName `
-Name $scheduleName -ResourceGroupName "ResourceGroup01"

后续步骤Next steps