在 Azure 自动化中管理 RunbookManage runbooks in Azure Automation

可以通过以下方法将 Runbook 添加到 Azure 自动化:创建新的 Runbook 或从文件中导入现有 Runbook。You can add a runbook to Azure Automation by either creating a new one or by importing an existing runbook from a file . 本文介绍如何通过文件创建和导入 Runbook。This article provides information on creating and importing runbooks from a file.

创建 runbookCreate a runbook

可以使用其中一个 Azure 门户或 Windows PowerShell 在 Azure 自动化中创建一个新的 Runbook。You can create a new runbook in Azure Automation using one of the Azure portals or Windows PowerShell. 一旦创建了 Runbook,便可以利用了解 PowerShell 工作流Azure 自动化中的图形创作中的信息对其进行编辑。Once the runbook has been created, you can edit it using information in Learning PowerShell Workflow and Graphical authoring in Azure Automation.

在 Azure 门户中创建 RunbookCreate a runbook in the Azure portal

  1. 在 Azure 门户中,打开自动化帐户。In the Azure portal, open your Automation account.
  2. 从中心内,在“过程自动化”下选择“Runbook”,打开 Runbook 列表。 From the hub, select Runbooks under Process Automation to open the list of runbooks.
  3. 单击“创建 Runbook” 。Click Create a runbook.
  4. 为 Runbook 键入一个名称并选择其类型Enter a name for the runbook and select its Type. Runbook 名称必须以字母开头,可以包含字母、数字、下划线和短划线。The runbook name must start with a letter and can contain letters, numbers, underscores, and dashes.
  5. 单击“创建” 以创建 Runbook 并打开编辑器。Click Create to create the runbook and open the editor.

通过 PowerShell 创建 RunbookCreate a runbook with PowerShell

可以使用 New-AzAutomationRunbook cmdlet 创建空的 PowerShell 工作流 RunbookYou can use the New-AzAutomationRunbook cmdlet to create an empty PowerShell Workflow runbook. 使用 Type 参数指定为 New-AzAutomationRunbook定义的 runbook 类型之一。Use the Type parameter to specify one of the runbook types defined for New-AzAutomationRunbook.

以下示例演示了如何创建新的空 runbook。The following example shows how to create a new empty runbook.

New-AzAutomationRunbook -AutomationAccountName MyAccount `
-Name NewRunbook -ResourceGroupName MyResourceGroup -Type PowerShell

导入 RunbookImport a runbook

可以在 Azure 自动化中创建新的 Runbook,方法是导入 PowerShell 脚本或 PowerShell 工作流 ( .ps1)、导出的图形 Runbook ( .graphrunbook),或 Python 2 脚本 ( .py)。You can create a new runbook in Azure Automation by importing a PowerShell script or PowerShell Workflow (.ps1), an exported graphical runbook (.graphrunbook), or a Python 2 script (.py). 必须指定在导入期间创建的 Runbook 类型,并考虑以下注意事项。You must specify the type of runbook that is created during import, taking into account the following considerations.

  • 不包含工作流的 .ps1 文件可以导入到 PowerShell Runbook 中,也可以导入到 PowerShell 工作流 Runbook 中。A .ps1 file that doesn't contain a workflow can be imported into either a PowerShell runbook or a PowerShell Workflow runbook. 如果将其导入到 PowerShell 工作流 Runbook 中,则它将转换为工作流。If you import it into a PowerShell Workflow runbook, it is converted to a workflow. 在这种情况下,Runbook 中将包含注释来描述所做的更改。In this case, comments are included in the runbook to describe the changes that have been made.

  • 包含 PowerShell 工作流的 .ps1 文件只能导入到 PowerShell 工作流 Runbook中。A .ps1 file containing a PowerShell Workflow can only be imported into a PowerShell Workflow runbook. 如果该文件包含多个 PowerShell 工作流,则导入将失败。If the file contains multiple PowerShell workflows, the import fails. 必须将每个工作流保存到各自的文件中,并分别导入每个工作流。You must save each workflow to its own file and import each separately.

  • 包含 PowerShell 工作流的 .ps1 文件不应导入到 PowerShell runbook 中,因为 PowerShell 脚本引擎无法识别该文件。A .ps1 file containing a PowerShell Workflow should not be imported into a PowerShell runbook, as the PowerShell script engine can't recognize it.

  • .graphrunbook 文件只能导入到新的图形 runbook 中。A .graphrunbook file can only be imported into a new graphical runbook. 请注意,只能基于 .graphrunbook 文件创建图形 runbook。Note that you can only create a graphical runbook from a .graphrunbook file.

使用 Azure 门户通过文件导入 RunbookImport a runbook from a file with the Azure portal

可通过以下过程将脚本文件导入 Azure 自动化。You can use the following procedure to import a script file into Azure Automation.

备注

只能通过门户将 .ps1 文件导入 PowerShell 工作流 Runbook。You can only import a .ps1 file into a PowerShell Workflow runbook using the portal.

  1. 在 Azure 门户中,打开自动化帐户。In the Azure portal, open your Automation account.
  2. 从中心内,在“过程自动化”下选择“Runbook”,打开 Runbook 列表。 From the hub, select Runbooks under Process Automation to open the list of runbooks.
  3. 单击“导入 Runbook”。 Click Import a runbook.
  4. 单击“Runbook 文件” ,并选择要导入的文件。Click Runbook file and select the file to import.
  5. 如果“名称” 字段已启用,则可以更改 runbook 名称。If the Name field is enabled, you have the option of changing the runbook name. 名称必须以字母开头,可以包含字母、数字、下划线和短划线。The name must start with a letter and can contain letters, numbers, underscores, and dashes.
  6. 将自动选择 Runbook 类型,但可以在考虑适用的限制后更改该类型。The runbook type is automatically selected, but you can change the type after taking the applicable restrictions into account.
  7. 单击创建Click Create. 新的 runbook 会出现在自动化帐户的 runbook 列表中。The new runbook appears in the list of runbooks for the Automation account.
  8. 必须先 发布 Runbook ,才能运行它。You must publish the runbook before you can run it.

备注

在导入图形 Runbook 或图形 PowerShell 工作流 Runbook 后,可以将其转换为其他类型。After you import a graphical runbook or a graphical PowerShell Workflow runbook, you can convert it to another type. 但是,无法将这些图形 Runbook 之一转换为文本 Runbook。However, you can't convert one of these graphical runbooks to a textual runbook.

使用 Windows PowerShell 从脚本文件导入 RunbookImport a runbook from a script file with Windows PowerShell

可以使用 Import-AzAutomationRunbook cmdlet 将脚本文件导入为 PowerShell 工作流草稿 Runbook。Use the Import-AzAutomationRunbook cmdlet to import a script file as a draft PowerShell Workflow runbook. 如果 Runbook 已存在,除非为该 cmdlet 使用 -Force 参数,否则导入将失败。If the runbook already exists, the import fails unless you use the Force parameter with the cmdlet.

以下示例演示了如何将脚本文件导入到 Runbook 中。The following example shows how to import a script file into a runbook.

$automationAccountName =  "AutomationAccount"
$runbookName = "Sample_TestRunbook"
$scriptPath = "C:\Runbooks\Sample_TestRunbook.ps1"
$RGName = "ResourceGroup"

Import-AzAutomationRunbook -Name $runbookName -Path $scriptPath `
-ResourceGroupName $RGName -AutomationAccountName $automationAccountName `
-Type PowerShellWorkflow

测试 RunbookTest a runbook

测试 Runbook 时,将执行草稿版,并会完成其执行的任何操作。When you test a runbook, the Draft version is executed and any actions that it performs are completed. 不会创建作业历史记录,但会在“测试输出”窗格中显示“输出”“警告和错误”流。No job history is created, but the Output and Warning and Error streams are displayed in the Test output pane. 仅当 $VerbosePreference 变量设置为 Continue 时,才会在“输出”窗格中显示发送到详细流的消息。Messages to the Verbose stream are displayed in the Output pane only if the $VerbosePreference variable is set to Continue.

即使草稿版正在运行,该 Runbook 也仍会正常执行,并针对环境中的资源执行任何操作。Even though the draft version is being run, the runbook still executes normally and performs any actions against resources in the environment. 因此,只能在非生产资源中测试 Runbook。For this reason, you should only test runbooks on non-production resources.

测试每种 Runbook 类型的过程是相同的。The procedure to test each type of runbook is the same. 并且,在 Azure 门户中使用文本编辑器进行测试和使用图形编辑器进行测试没有区别。There's no difference in testing between the textual editor and the graphical editor in the Azure portal.

  1. 文本编辑器图形编辑器中打开 Runbook 的草稿版本。Open the Draft version of the runbook in either the textual editor or the graphical editor.
  2. 单击“测试”按钮以打开“测试”页 。Click the Test button to open the Test page.
  3. 如果 Runbook 有参数,参数会在左窗格中列出,可以在其中提供用于测试的值。If the runbook has parameters, they are listed in the left pane, where you can provide values to be used for the test.
  4. 如果要在混合 Runbook 辅助角色上运行测试,则将“运行设置” 更改为“混合辅助角色” 并选择目标组的名称。If you want to run the test on a Hybrid Runbook Worker, change Run Settings to Hybrid Worker and select the name of the target group. Azure ,以在云中运行测试。Otherwise, keep the default Azure to run the test in the cloud.
  5. 单击“开始” 按钮以开始测试。Click the Start button to begin the test.
  6. 在测试期间,可以使用“输出”窗格下方的按钮来停止或暂停 PowerShell 工作流图形 runbook。You can use the buttons under the Output pane to stop or suspend a PowerShell Workflow or graphical runbook while it's being tested. 暂停 Runbook 时,该 Runbook 会完成它在被暂停之前正在进行的活动。When you suspend the runbook, it completes the current activity before being suspended. 暂停 Runbook 后,可以将它停止或重启。Once the runbook is suspended, you can stop it or restart it.
  7. 在输出窗格中检查 Runbook 的输出。Inspect the output from the runbook in the Output pane.

发布 RunbookPublish a runbook

创建或导入新的 Runbook 时,必须先将其发布,才能导入。When you create or import a new runbook, you must publish it before you can run it. Azure 自动化中的每个 Runbook 都有草稿版和已发布版。Each runbook in Azure Automation has a Draft version and a Published version. 只有已发布版才能用来运行,只有草稿版才能用来编辑。Only the Published version is available to be run, and only the Draft version can be edited. 已发布版不受对草稿版所做的任何更改的影响。The Published version is unaffected by any changes to the Draft version. 当草稿版可以使用时,可以发布草稿版,这样草稿版就会覆盖当前已发布版。When the Draft version should be made available, you publish it, overwriting the current Published version with the Draft version.

在 Azure 门户中发布 RunbookPublish a runbook in the Azure portal

  1. 在 Azure 门户中打开 Runbook。Open the runbook in the Azure portal.
  2. 单击“编辑”。 Click Edit.
  3. 单击“发布” ,然后单击“是”来响应验证消息 。Click Publish and then Yes in response to the verification message.

使用 PowerShell 发布 RunbookPublish a runbook using PowerShell

可以通过 Windows PowerShell 使用 Publish-AzAutomationRunbook cmdlet 发布 Runbook。Use the Publish-AzAutomationRunbook cmdlet to publish a runbook with Windows PowerShell. 以下示例展示了如何发布示例 Runbook。The following example shows how to publish a sample runbook.

$automationAccountName =  "AutomationAccount"
$runbookName = "Sample_TestRunbook"
$RGName = "ResourceGroup"

Publish-AzAutomationRunbook -AutomationAccountName $automationAccountName `
-Name $runbookName -ResourceGroupName $RGName

后续步骤Next steps