在 Azure 自动化中创作图形 RunbookAuthor graphical runbooks in Azure Automation

Azure 自动化中的所有 Runbook 都是 Windows PowerShell 工作流。All runbooks in Azure Automation are Windows PowerShell workflows. 图形 Runbook 和图形 PowerShell 工作流 Runbook 生成 PowerShell 代码,这些代码由自动化辅助角色运行,但你无法查看或修改。Graphical runbooks and graphical PowerShell Workflow runbooks generate PowerShell code that the Automation workers run but that you cannot view or modify. 可以将图形 Runbook 转换为图形 PowerShell 工作流 Runbook,反之亦然。You can convert a graphical runbook to a graphical PowerShell Workflow runbook, and vice versa. 不过,无法将这些 Runbook 转换为文本 Runbook。However, you can't convert these runbooks to a textual runbook. 此外,自动化图形编辑器也无法导入文本 Runbook。Additionally, the Automation graphical editor can't import a textual runbook.

借助图形创作,可以为 Azure 自动化创建 Runbook,而不需要编写复杂的基础 Windows PowerShell 或 PowerShell 工作流代码。Graphical authoring allows you to create runbooks for Azure Automation without the complexities of the underlying Windows PowerShell or PowerShell Workflow code. 可以将 cmdlet 和 Runbook 库中的活动添加到画布,将它们链接到一起,并配置它们来形成工作流。You can add activities to the canvas from a library of cmdlets and runbooks, link them together, and configure them to form a workflow. 如果你曾经使用过 System Center Orchestrator 或 Service Management Automation (SMA),图形创作应该看起来很熟悉。If you have ever worked with System Center Orchestrator or Service Management Automation (SMA), graphical authoring should look familiar. 本文介绍了开始创建图形 Runbook 所需掌握的概念。This article provides an introduction to the concepts you need to get started creating a graphical runbook.

图形编辑器概述Overview of graphical editor

通过创建或编辑图形 Runbook,可以在 Azure 门户中打开图形编辑器。You can open the graphical editor in the Azure portal by creating or editing a graphical runbook.


以下各节介绍图形编辑器中的控件。The following sections describe the controls in the graphical editor.

“画布”控件Canvas control

使用“画布”控件,可以设计 Runbook。The Canvas control allows you to design your runbook. 可以将“库”控件中节点内的活动添加到 Runbook,并通过链接将它们连接起来,以定义 Runbook 逻辑。You can add activities from the nodes in the Library control to the runbook and connect them with links to define runbook logic. 在画布底部,有一些可用于放大和缩小的控件。At the bottom of the canvas, there are controls that allow you to zoom in and out.

库控件Library control

使用“库”控件,可以选择要添加到 Runbook 的活动The Library control allows you to select activities to add to your runbook. 可以将活动添加到画布中,在其中能够将它们连接到其他活动。You add them to the canvas, where you can connect them to other activities. “库”控件包含下表中定义的部分。The Library control includes the sections defined in the following table.

部分Section 说明Description
CmdletCmdlets 可以在 Runbook 中使用的所有 cmdlet。All the cmdlets that can be used in your runbook. Cmdlet 按模块组织。Cmdlets are organized by module. 你在自动化帐户中安装的所有模块都是可用的。All the modules that you have installed in your Automation account are available.
RunbookRunbooks 自动化帐户中的 Runbook。The runbooks in your Automation account. 可以将这些 Runbook 添加到画布中,以用作子 Runbook。You can add these runbooks to the canvas to be used as child runbooks. 只显示核心类型与所编辑的 Runbook 相同的 Runbook。Only runbooks of the same core type as the runbook being edited are shown. 对于图形 Runbook,只显示基于 PowerShell 的 Runbook。For graphical runbooks, only PowerShell-based runbooks are shown. 对于图形 PowerShell 工作流 Runbook,只显示基于 PowerShell 工作流的 Runbook。For graphical PowerShell Workflow runbooks, only PowerShell Workflow-based runbooks are shown.
资产Assets 自动化帐户中能够在 Runbook 中使用的自动化资产The automation assets in your Automation account that you can use in your runbook. 向 Runbook 添加资产会添加获取所选资产的工作流活动。Adding an asset to a runbook adds a workflow activity that gets the selected asset. 在使用变量资产的情况下,可以选择是否添加用于获取变量或设置变量的活动。In the case of variable assets, you can select whether to add an activity to get the variable or set the variable.
Runbook 控件Runbook Control 可以在当前 Runbook 中使用的控件活动。Control activities that can be used in your current runbook. “接合”活动需要使用多个输入,并等到所有输入都完成后再继续执行工作流。A Junction activity takes multiple inputs and waits until all have completed before continuing the workflow. “代码”活动运行一行或多行 PowerShell 或 PowerShell 工作流代码,具体视图形 Runbook 类型而定。A Code activity runs one or more lines of PowerShell or PowerShell Workflow code, depending on the graphical runbook type. 可以将此活动用于自定义代码或通过其他活动难以实现的功能。You can use this activity for custom code or for functionality that is difficult to achieve with other activities.

配置控件Configuration control

使用“配置”控件,可以提供在画布上选择的对象的详细信息。The Configuration control enables you to provide details for an object that is selected on the canvas. 此控件中的可用属性取决于所选对象的类型。The properties available in this control depend on the type of object selected. 当你在“配置”控件中选择一个选项时,它会打开其他边栏选项卡来提供更多信息。When you choose an option in the Configuration control, it opens additional blades to provide more information.

测试控件Test control

首次启动图形编辑器时,不显示测试控件。The Test control is not displayed when the graphical editor is first started. 当你以交互方式测试图形 Runbook 时,它会打开。It is opened when you interactively test a graphical runbook.

使用活动Use activities

活动是 Runbook 的构建基块。Activities are the building blocks of a runbook. 活动可以是 PowerShell cmdlet、子 Runbook 或工作流。An activity can be a PowerShell cmdlet, a child runbook, or a workflow. 通过在“库”控件中右键单击活动并选择“添加到画布”,可以向 Runbook 添加活动。You can add an activity to the runbook by right-clicking it in the Library control and selecting Add to canvas. 然后,可以通过单击拖放的方式将活动置于画布上喜欢的任何位置。You can then click and drag the activity to place it anywhere on the canvas that you like. 活动在画布上的位置不会影响 Runbook 的操作。The location of the activity on the canvas does not affect the operation of the runbook. 你可以用任何你认为最适合可视化 Runbook 操作的方式来对 Runbook 进行布局。You can lay out your runbook any way you find most suitable to visualize its operation.


在画布上选择一个活动,以在“配置”边栏选项卡中配置它的属性和参数。Select an activity on the canvas to configure its properties and parameters in the Configuration blade. 可以将活动的标签更改为更具描述性的名称。You can change the label of the activity to a name that you find descriptive. Runbook 仍运行原始 cmdlet。The runbook still runs the original cmdlet. 只需更改图形编辑器使用的显示名称。You are simply changing the display name that the graphical editor uses. 请注意,标签在 Runbook 中必须是唯一的。Note that the label must be unique within the runbook.

参数集Parameter sets

参数集用于定义会接受特定 cmdlet 的值的必需参数和可选参数。A parameter set defines the mandatory and optional parameters that accept values for a particular cmdlet. 所有 cmdlet 都至少有一个参数集,有些有多个参数集。All cmdlets have at least one parameter set, and some have several sets. 如果 cmdlet 有多个参数集,必须先选择要使用的参数集,然后才能配置参数。If a cmdlet has multiple parameter sets, you must select the one to use before you can configure parameters. 通过依次选择“参数集”和另一个参数集,可以更改活动使用的参数集。You can change the parameter set used by an activity by selecting Parameter Set and choosing another set. 在这种情况下,你已经配置的任何参数值都会丢失。In this case, any parameter values that you have already configured are lost.

在下面的示例中,Get-AzVM cmdlet 有三个参数集。In the following example, the Get-AzVM cmdlet has three parameter sets. 此示例使用一个名为 ListVirtualMachineInResourceGroupParamSet 的参数集(其中带有一个可选参数),用于返回资源组中的所有虚拟机。The example uses one set called ListVirtualMachineInResourceGroupParamSet, with a single optional parameter, for returning all virtual machines in a resource group. 此示例还使用 GetVirtualMachineInResourceGroupParamSet 参数集,用于指定要返回的虚拟机。The example also uses the GetVirtualMachineInResourceGroupParamSet parameter set for specifying the virtual machine to return. 此参数集有两个强制参数和一个可选参数。This set has two mandatory parameters and one optional parameter.


参数值Parameter values

指定某个参数的值时,可以选择一个数据源,以便确定如何指定该值。When you specify a value for a parameter, you select a data source to determine how the value is specified. 可用于特定参数的数据源取决于相应参数的有效值。The data sources that are available for a particular parameter depend on the valid values for that parameter. 例如,对于不允许 Null 值的参数,Null 不会是可用选项。For example, Null is not an available option for a parameter that does not allow null values.

数据源Data Source 说明Description
常量值Constant Value 键入参数的值。Type in a value for the parameter. 此数据源只适用于以下数据类型:Int32、Int64、String、Boolean、DateTime、Switch。This data source is only available for the following data types: Int32, Int64, String, Boolean, DateTime, Switch.
活动输出Activity Output 使用工作流中当前活动之前的活动的输出。Use output from an activity that precedes the current activity in the workflow. 将列出所有有效的活动。All valid activities are listed. 对于参数值,只使用生成输出的活动。For the parameter value, use just the activity that produces the output. 如果活动输出的是包含多个属性的对象,可以在选择活动后键入特定属性的名称。If the activity outputs an object with multiple properties, you can type in the name of a specific property after selecting the activity.
Runbook 输入Runbook Input 选择 Runbook 输入作为活动参数的输入。Select a runbook input as an input for the activity parameter.
变量资产Variable Asset 选择自动化变量作为输入。Select an Automation variable as input.
凭据资产Credential Asset 选择自动化凭据作为输入。Select an Automation credential as input.
证书资产Certificate Asset 选择自动化证书作为输入。Select an Automation certificate as input.
连接资产Connection Asset 选择自动化连接作为输入。Select an Automation connection as input.
PowerShell 表达式PowerShell Expression 指定简单的 PowerShell 表达式Specify a simple PowerShell expression. 表达式计算先于活动,且结果用于参数值。The expression is evaluated before the activity and the result is used for the parameter value. 可以使用变量来引用活动或 Runbook 输入参数的输出。You can use variables to refer to the output of an activity or a runbook input parameter.
未配置Not Configured 清除以前配置的任何值。Clear any value that was previously configured.

可选的其他参数Optional additional parameters

所有 cmdlet 都会有提供其他参数的选项。All cmdlets have the option to provide additional parameters. 这些是 PowerShell 通用参数或其他自定义参数。These are PowerShell-common parameters or other custom parameters. 图形编辑器提供了一个文本框,可以在其中使用 PowerShell 语法提供参数。The graphical editor presents a text box where you can provide parameters using PowerShell syntax. 例如,若要使用 Verbose 通用参数,应指定 -Verbose:$TrueFor example, to use the Verbose common parameter, you should specify -Verbose:$True.

重试活动Retry activity

活动的重试功能可以让活动运行多次,直到满足特定的条件,这很像循环。Retry functionality for an activity allows it to be run multiple times until a particular condition is met, much like a loop. 可以将此功能用于以下活动:应该运行多次的活动、容易出错的活动、可能需要尝试多次才成功的活动,或测试活动输出信息中是否存在有效数据的活动。You can use this feature for activities that should run multiple times, are error prone, might need more than one attempt for success, or test the output information of the activity for valid data.

对活动启用重试时,可以设置延迟和条件。When you enable retry for an activity, you can set a delay and a condition. 延迟是 Runbook 再次运行活动之前等待的时间(以秒或分钟为单位)。The delay is the time (measured in seconds or minutes) that the runbook waits before it runs the activity again. 如果没有指定延迟,则活动在完成后立即再次运行。If you don't specify a delay, the activity runs again immediately after it completes.


重试条件是在每次活动运行后计算的 PowerShell 表达式。The retry condition is a PowerShell expression that is evaluated after each time that the activity runs. 如果表达式解析为 True,则活动再次运行。If the expression resolves to True, the activity runs again. 如果表达式解析为 False,则活动不会再次运行,且 Runbook 转移到下一个活动。If the expression resolves to False, the activity does not run again and the runbook moves on to the next activity.


重试条件可以使用名为 RetryData 的变量,此变量提供对活动重试相关信息的访问权限。The retry condition can use a variable named RetryData that provides access to information about the activity retries. 此变量具有下表中的属性。This variable has the properties in the following table:

propertiesProperty 说明Description
NumberOfAttempts 活动已运行的次数。Number of times that the activity has been run.
Output 活动上次运行的输出。Output from the last run of the activity.
TotalDuration 活动首次开始之后的经过时间。Timed elapsed since the activity was started the first time.
StartedAt 活动第一次开始时的时间(采用 UTC 格式)。Time (in UTC format) when the activity was first started.

下面是活动重试条件示例。The following are examples of activity retry conditions.

# Run the activity exactly 10 times.
$RetryData.NumberOfAttempts -ge 10
# Run the activity repeatedly until it produces any output.
$RetryData.Output.Count -ge 1
# Run the activity repeatedly until 2 minutes has elapsed.
$RetryData.TotalDuration.TotalMinutes -ge 2

配置活动的重试条件以后,该活动将包含两个用于提醒可视提示。After you configure a retry condition for an activity, the activity includes two visual cues to remind you. 一个提示在活动中显示,另一个提示在你审阅活动配置时显示。One is presented in the activity and the other is shown when you review the configuration of the activity.


工作流脚本控件Workflow Script control

“工作流脚本”控件是一种特殊的活动,它接受 PowerShell 或 PowerShell 工作流脚本,具体视要创作的图形 Runbook 的类型而定。A workflow Script control is a special activity that accepts PowerShell or PowerShell Workflow script, depending on the type of graphical runbook being authored. 此控件提供了其他方式无法提供的功能。This control provides functionality that might not be available by other means. 它不能接受参数,但可以使用针对活动输出和 Runbook 输入参数的变量。It cannot accept parameters, but it can use variables for activity output and runbook input parameters. 此活动的任何输出都会添加到数据总线。Any output of the activity is added to the databus. 异常是没有传出链接的输出;在这种情况下,输出被添加到 Runbook 输出中。An exception is output with no outgoing link, in which case the output is added to the output of the runbook.

例如,以下代码使用名为 NumberOfDays 的 Runbook 输入变量来执行日期计算。For example, the following code performs date calculations using a runbook input variable named NumberOfDays. 然后,它将计算出的 DateTime 值作为输出发送给 Runbook 中的后续活动使用。It then sends a calculated DateTime value as output to be used by subsequent activities in the runbook.

$DateTimeNow = (Get-Date).ToUniversalTime()
$DateTimeStart = ($DateTimeNow).AddDays(-$NumberOfDays)}

图形 Runbook 中的链接用于连接两个活动。A link in a graphical runbook connects two activities. 它作为箭头显示在画布上,从源活动指向目标活动。It is displayed on the canvas as an arrow pointing from the source activity to the destination activity. 活动按箭头的方向运行,源活动完成后才会开始目标活动。The activities run in the direction of the arrow with the destination activity starting after the source activity completes.

通过选择源活动并单击形状底部的圆圈,可以在两个活动之间创建链接。You can create a link between two activities by selecting the source activity and clicking the circle at the bottom of the shape. 将箭头拖到目标活动,然后放开。Drag the arrow to the destination activity and release.


选择可在“配置”边栏选项卡中配置其属性的链接。Select the link to configure its properties in the Configuration blade. 属性包括链接类型,下表对此进行了描述。Properties include the link type, which is described in the following table.

链接类型Link Type 说明Description
管道Pipeline 对于源活动的每个对象输出,目标活动运行一次。The destination activity runs once for each object output from the source activity. 如果源活动没有生成任何输出,目标活动将不会运行。The destination activity does not run if the source activity results in no output. 源活动的输出可用作对象。Output from the source activity is available as an object.
序列Sequence 目标活动只在收到源活动的输出时运行一次。The destination activity runs only once when it receives output from the source activity. 源活动的输出可用作对象数组。Output from the source activity is available as an array of objects.

Runbook 启动活动Start runbook activity

图形 Runbook 会通过任何没有传入链接的活动启动。A graphical runbook starts with any activities that do not have an incoming link. 通常只有一个活动作为 Runbook 启动活动。There is often only one activity that acts as the starting activity for the runbook. 如果多个活动没有传入链接,Runbook 在启动时并行运行这些活动。If multiple activities do not have an incoming link, the runbook starts by running them in parallel. 然后,它会在每个活动完成时,按链接来运行其他活动。It follows the links to run other activities as each completes.

如果你指定链接条件,那么仅当条件解析为 True 时,目标活动才运行。When you specify a condition on a link, the destination activity runs only if the condition resolves to True. 通常在条件中使用 ActivityOutput 变量来检索源活动的输出。You typically use an ActivityOutput variable in a condition to retrieve the output from the source activity.

对于管道链接,必须为单个对象指定条件。For a pipeline link, you must specify a condition for a single object. Runbook 对源活动输出的每个对象进行条件计算。The runbook evaluates the condition for each object output by the source activity. 然后,它会为符合条件的每个对象运行目标活动。It then runs the destination activity for each object that satisfies the condition. 例如,对于源活动 Get-AzVM,可以对条件管道链接使用以下语法,只检索名为 Group1 的资源组中的虚拟机。For example, with a source activity of Get-AzVM, you can use the following syntax for a conditional pipeline link to retrieve only virtual machines in the resource group named Group1.

$ActivityOutput['Get Azure VMs'].Name -match "Group1"

对于序列链接,Runbook 只计算一次条件,因为会返回一个包含源活动中所有对象的数组。For a sequence link, the runbook only evaluates the condition once, since a single array containing all objects from the source activity is returned. 因此,Runbook 不能像使用管道链接那样使用序列链接进行筛选。Because of this, the runbook can't use a sequence link for filtering, like it can with a pipeline link. 序列链接可以直接确定是否运行下一个活动。The sequence link can simply determine whether or not the next activity is run.

例如,以“启动 VM”Runbook 中的以下活动集为例:For example, take the following set of activities in our Start VM runbook:


Runbook 使用三个不同的序列链接来验证输入参数 VMNameResourceGroupName 的值,以确定要执行的适当操作。The runbook uses three different sequence links that verify values of the input parameters VMName and ResourceGroupName to determine the appropriate action to take. 可能会执行的操作有启动一个 VM、启动资源组中的所有 VM 或启动订阅中的所有 VM。Possible actions are start a single VM, start all VMs in the resource group, or start all VMs in a subscription. 对于 Connect to AzureGet single VM 之间的序列链接,条件逻辑如下:For the sequence link between Connect to Azure and Get single VM, here is the condition logic:

Both VMName and ResourceGroupName runbook input parameters have values
(($VMName -ne $null) -and ($VMName.Length -gt 0))
) -and (
(($ResourceGroupName -ne $null) -and ($ResourceGroupName.Length -gt 0))

使用条件链接时,将通过条件来筛选该分支中从源活动到其他活动所提供的数据。When you use a conditional link, the data available from the source activity to other activities in that branch is filtered by the condition. 如果某个活动是多个链接的源,那么每个分支中的活动可用的数据取决于连接到相应分支的链接中的条件。If an activity is the source to multiple links, the data available to activities in each branch depends on the condition in the link connecting to that branch.

例如,下面 Runbook 中的 Start-AzVM 活动启动所有虚拟机。For example, the Start-AzVM activity in the runbook below starts all virtual machines. 它有两个条件链接。It has two conditional links. 第一个条件链接使用表达式 $ActivityOutput['Start-AzVM'].IsSuccessStatusCode -eq $true 来筛选 Start-AzVM 活动是否已成功完成。The first conditional link uses the expression $ActivityOutput['Start-AzVM'].IsSuccessStatusCode -eq $true to filter if the Start-AzVM activity completes successfully. 第二个条件链接使用表达式 $ActivityOutput['Start-AzVM'].IsSuccessStatusCode -ne $true 来筛选 Start-AzVm 活动是否无法启动虚拟机。The second conditional link uses the expression $ActivityOutput['Start-AzVM'].IsSuccessStatusCode -ne $true to filter if the Start-AzVm activity fails to start the virtual machine.


任何遵循第一个链接并使用 Get-AzureVM 的活动输出的活动只检索在 Get-AzureVM 运行时启动的虚拟机。Any activity that follows the first link and uses the activity output from Get-AzureVM only retrieves the virtual machines that were started at the time when Get-AzureVM was run. 任何遵循第二个链接的活动只获取在 Get-AzureVM 运行时停止的虚拟机。Any activity that follows the second link only gets the virtual machines that were stopped at the time when Get-AzureVM was run. 任何按第三个链接进行的活动都会获取所有虚拟机,不管这些虚拟机的运行状态如何。Any activity following the third link gets all virtual machines regardless of their running state.

使用接合Use junctions

一个交接点是一个特殊活动,它会一直等到所有传入分支完成为止。A junction is a special activity that waits until all incoming branches have completed. 这样,Runbook 可以并行运行多个活动,并确保在继续之前所有活动都已完成。This allows the runbook to run multiple activities in parallel and ensure that all have completed before moving on.

虽然接合可以有无限数量的传入链接,但这些链接中只有一个可以是管道。While a junction can have an unlimited number of incoming links, only one of those links can be a pipeline. 传入序列链接的数目不受限制。The number of incoming sequence links is not constrained. 你可以创建包含多个传入管道链接的接合,并保存 Runbook,但它在运行时会失败。You can create the junction with multiple incoming pipeline links and save the runbook, but it will fail when it is run.

下面的示例是某个 Runbook 的一部分,该 Runbook 可以启动一组虚拟机,同时还会下载要应用到这些虚拟机的修补程序。The example below is part of a runbook that starts a set of virtual machines while simultaneously downloading patches to be applied to those machines. 它使用接合来确保两个进程在 Runbook 继续之前都已完成。It uses a junction to ensure that both processes are completed before the runbook continues.


使用循环Work with cycles

如果目标活动链接回其源活动,或链接回另一个最终会链接回其源的活动,则形成循环。A cycle is formed when a destination activity links back to its source activity or to another activity that eventually links back to its source. 图形创作目前不支持循环。Graphical authoring does not currently support cycles. 如果 Runbook 有一个周期,可以正常保存它,但在运行它时会收到一个错误。If your runbook has a cycle, it saves properly but receives an error when it runs.


在活动之间共享数据Share data between activities

活动使用传出链接输出的任何数据都被写入到 Runbook 的数据总线。Any data that an activity outputs with an outgoing link is written to the databus for the runbook. Runbook 中的任何活动都可以使用数据总线上的数据来填充参数值或添加脚本代码。Any activity in the runbook can use data on the databus to populate parameter values or include in script code. 一个活动可以访问工作流中任何以前的活动的输出。An activity can access the output of any previous activity in the workflow.

数据写入数据总线的方式取决于活动的链接的类型。How the data is written to the databus depends on the type of link on the activity. 对于管道链接,数据以多个对象的形式输出。For a pipeline link, the data is output as multiple objects. 对于序列链接,数据以数组的形式输出。For a sequence link, the data is output as an array. 如果只有一个值,数据以单元素数组的形式输出。If there is only one value, it is output as a single-element array.

Runbook 有两种方式来访问数据总线中的数据:Your runbook has two ways to access data on the databus:

  • 使用活动输出数据源。Use an activity output data source.
  • 使用 PowerShell 表达式数据源。Use a PowerShell expression data source.

第一种机制使用活动输出数据源来填充另一活动的参数。The first mechanism uses an activity output data source to populate a parameter of another activity. 如果输出是对象,Runbook 可以指定单个属性。If the output is an object, the runbook can specify a single property.


第二种数据访问机制使用 ActivityOutput 变量,检索 PowerShell 表达式数据源中活动的输出或工作流脚本活动的输出,语法如下所示。The second data access mechanism retrieves the output of an activity in a PowerShell expression data source or a workflow script activity with an ActivityOutput variable, using the syntax shown below. 如果输出是对象,Runbook 可以指定单个属性。If the output is an object, your runbook can specify a single property.

$ActivityOutput['Activity Label']
$ActivityOutput['Activity Label'].PropertyName

使用检查点Use checkpoints

通过在任何活动上选择“检查点 Runbook”,可以在图形 PowerShell 工作流 Runbook 中设置检查点You can set checkpoints in a graphical PowerShell Workflow runbook by selecting Checkpoint runbook on any activity. 这会导致在运行活动之后设置检查点。This causes a checkpoint to be set after the activity runs.


检查点只在图形 PowerShell 工作流 Runbook 中启用,在图形 Runbook 中不可用。Checkpoints are only enabled in graphical PowerShell Workflow runbooks, and are not available in graphical runbooks. 如果 Runbook 使用 Azure cmdlet,它应使用 Connect-AzAccount 活动遵循任何设置了检查点的活动。If the runbook uses Azure cmdlets, it should follow any checkpointed activity with a Connect-AzAccount activity. 连接操作用于以下情况:Runbook 已暂停,且必须从此检查点在另一个辅助角色上重启。The connect operation is used in case the runbook is suspended and must restart from this checkpoint on a different worker.

处理 Runbook 输入Handle runbook input

Runbook 需要输入,要么来自通过 Azure 门户启动 Runbook 的用户,要么来自另一个 Runbook(如果当前 Runbook 用作子 Runbook 的话)。A runbook requires input either from a user starting the runbook through the Azure portal or from another runbook, if the current one is used as a child. 例如,对于创建虚拟机的 Runbook,用户可能需要在 Runbook 每次启动时都提供虚拟机名称和其他属性等信息。For example, for a runbook that creates a virtual machine, the user might need to provide such information as the name of the virtual machine and other properties each time the runbook starts.

Runbook 通过定义一个或多个输入参数来接受输入。The runbook accepts input by defining one or more input parameters. 用户在 Runbook 每次启动时提供这些参数的值。The user provides values for these parameters each time the runbook starts. 当用户使用 Azure 门户启动 Runbook 时,系统会提示用户为 Runbook 支持的每个输入参数提供值。When the user starts the runbook using the Azure portal, the user is prompted to provide values for each input parameter supported by the runbook.

创作 Runbook 时,可以通过单击 Runbook 工具栏上的“输入和输出”来访问它的输入参数。When authoring your runbook, you can access its input parameters by clicking Input and output on the runbook toolbar. 这会打开“输入和输出”控件,在其中可以编辑现有输入参数,也可以通过单击“添加输入”来新建输入参数。This opens the Input and Output control where you can edit an existing input parameter or create a new one by clicking Add input.


按下表中的属性定义每个输入参数:Each input parameter is defined by the properties in the following table:

propertiesProperty 说明Description
名称Name 必需。Required. 参数的名称。The name of the parameter. 此名称在 Runbook 中必须是唯一的。The name must be unique within the runbook. 它必须以字母开头,且只能包含字母、数字和下划线字符。It must start with a letter and can contain only letters, numbers, and underscores. 此名称不得包含空格。The name cannot contain a space.
说明Description 可选。Optional. 有关输入参数用途的说明。Description of the purpose for the input parameter.
类型Type 可选。Optional. 参数值应有的数据类型。Data type expected for the parameter value. 提示输入时,Azure 门户将针对每个参数的数据类型提供相应的控件。The Azure portal provides an appropriate control for the data type for each parameter when prompting for input. 支持的参数类型是 String、Int32、Int64、Decimal、Boolean、DateTime 和 Object。Supported parameter types are String, Int32, Int64, Decimal, Boolean, DateTime, and Object. 如果没有选择数据类型,则默认为 String。If a data type is not selected, it defaults to String.
必需Mandatory 可选。Optional. 指定是否必须为参数提供值的设置。Setting that specifies if a value must be provided for the parameter. 如果选择 yes,则必须在 Runbook 启动时提供值。If you choose yes, a value must be provided when the runbook is started. 如果选择 no,则不需要在 Runbook 启动时提供值,可以使用默认值。If you choose no, a value is not required when the runbook is started, and a default value can be used. 如果不为没有定义默认值的每个强制参数提供值,Runbook 就无法启动。The runbook cannot start if you do not provide a value for each mandatory parameter that does not have a default value defined.
默认值Default Value 可选。Optional. 用于在 Runbook 启动时没有值传入的参数的值。The value used for a parameter if one is not passed in when the runbook is started. 若要设置默认值,请选择 CustomTo set a default value, choose Custom. 如果不想提供任何默认值,请选择 NoneSelect None if you don't want to provide any default value.

处理 Runbook 输出Handle runbook output

图形创作将由任何没有传出链接的活动所创建的数据都保存到 Runbook 输出Graphical authoring saves data created by any activity that does not have an outgoing link to the output of the runbook. 输出将与 Runbook 作业一起保存,在该 Runbook 作为子 Runbook 使用的情况下,还可供父 Runbook 使用。The output is saved with the runbook job and is available to a parent runbook when the runbook is used as a child.

使用 PowerShell 表达式Work with PowerShell expressions

图形创作的一个优点是,你可以在对 PowerShell 了解最少的情况下创建 Runbook。One of the advantages of graphical authoring is that it allows you to build a runbook with minimal knowledge of PowerShell. 但目前,确实需要了解一点 PowerShell,以便填充某些参数值和设置链接条件Currently, though, you do need to know a bit of PowerShell for populating certain parameter values and for setting link conditions. 此部分快速介绍了 PowerShell 表达式。This section provides a quick introduction to PowerShell expressions. 使用 Windows PowerShell 编写脚本中提供了 PowerShell 的完整详细信息。Full details of PowerShell are available at Scripting with Windows PowerShell.

使用 PowerShell 表达式作为数据源Use a PowerShell expression as a data source

可以使用 PowerShell 表达式作为数据源,以使用 PowerShell 代码的结果来填充活动参数的值。You can use a PowerShell expression as a data source to populate the value of an activity parameter with the results of PowerShell code. 表达式可以是执行简单函数的一行代码,也可以是执行复杂逻辑的多行代码。The expression can be a single line of code that performs a simple function or multiple lines that perform some complex logic. 未分配给变量的任何命令输出都将输出到参数值。Any output from a command that is not assigned to a variable is output to the parameter value.

例如,以下命令输出当前日期。For example, the following command outputs the current date.


下一个代码片段通过当前日期生成字符串,并将它分配给变量。The next code snippet builds a string from the current date and assigns it to a variable. 此代码将变量的内容发送到输出。The code sends the contents of the variable to the output.

$string = "The current date is " + (Get-Date)

以下命令计算当前日期,并返回指明当天是工作日还是周末的字符串。The following commands evaluate the current date and return a string indicating whether the current day is a weekend or a weekday.

$date = Get-Date
if (($date.DayOfWeek = "Saturday") -or ($date.DayOfWeek = "Sunday")) { "Weekend" }
else { "Weekday" }

使用活动输出Use activity output

若要在 Runbook 中使用上一个活动的输出,请通过以下语法使用 ActivityOutput 变量。To use the output from a previous activity in your runbook, use the ActivityOutput variable with the following syntax.

$ActivityOutput['Activity Label'].PropertyName

例如,可以有一个活动,它的属性需要使用虚拟机名称。For example, you can have an activity with a property that requires the name of a virtual machine. 在这种情况下,Runbook 可以使用以下表达式。In this case, your runbook can use the following expression.


如果属性需要使用虚拟机对象(而不仅仅是名称),Runbook 使用以下语法返回整个对象。If the property requires the virtual machine object instead of just a name, the runbook returns the entire object using the following syntax.


Runbook 可以在更复杂的表达式中使用活动输出,如下所示。The runbook can use the output of an activity in a more complex expression, such as the following. 此表达式将文本连接到虚拟机名称。This expression concatenates text to the virtual machine name.

"The computer name is " + $ActivityOutput['Get-AzureVM'].Name

比较值Compare values

使用比较运算符来比较值或确定值是否与指定的模式匹配。Use comparison operators to compare values or determine if a value matches a specified pattern. 比较操作返回 True 或 False 值。A comparison returns a value of either True or False.

例如,以下条件确定活动 Get-AzureVM 中的虚拟机目前是否已停止。For example, the following condition determines if the virtual machine from an activity named Get-AzureVM is currently stopped.

$ActivityOutput["Get-AzureVM"].PowerState –eq "Stopped"

以下条件确定同一虚拟机是否处于除“已停止”以外的其他任何状态。The following condition determines if the same virtual machine is in any state other than stopped.

$ActivityOutput["Get-AzureVM"].PowerState –ne "Stopped"

可以使用逻辑运算符(如 -and-or)在 Runbook 中联接多个条件。You can join multiple conditions in your runbook using a logical operator, such as -and or -or. 例如,以下条件检查上一示例中的虚拟机是处于“已停止”状态,还是处于“正在停止”状态。For example, the following condition checks to see if the virtual machine in the previous example is in a state of Stopped or Stopping.

($ActivityOutput["Get-AzureVM"].PowerState –eq "Stopped") -or ($ActivityOutput["Get-AzureVM"].PowerState –eq "Stopping")

使用哈希表Use hashtables

哈希表是名称/值对,对于返回一组值很有用。Hashtables are name-value pairs that are useful for returning a set of values. 你可能还会发现,哈希表有时亦称为“字典”。You might also see a hashtable referred to as a dictionary. 某些活动的属性需要使用哈希表,而不是简单的值。Properties for certain activities expect a hashtable instead of a simple value.

使用以下语法创建哈希表。Create a hashtable using the following syntax. 它可以包含任意数量的条目,但每个条目都由名称和值定义。It can contain any number of entries, but each is defined by a name and value.

@{ <name> = <value>; [<name> = <value> ] ...}

例如,以下表达式创建要用作活动参数的数据源的哈希表,此活动参数需要使用包含值的哈希表进行 Internet 搜索。For example, the following expression creates a hashtable to be used as the data source for an activity parameter that expects a hashtable of values for an internet search.

$query = "Azure Automation"
$count = 10
$h = @{'q'=$query; 'lr'='lang_ja';  'count'=$Count}

以下示例使用活动 Get Twitter Connection 的输出来填充哈希表。The following example uses output from an activity called Get Twitter Connection to populate a hashtable.

@{'ApiKey'=$ActivityOutput['Get Twitter Connection'].ConsumerAPIKey;
    'ApiSecret'=$ActivityOutput['Get Twitter Connection'].ConsumerAPISecret;
    'AccessToken'=$ActivityOutput['Get Twitter Connection'].AccessToken;
    'AccessTokenSecret'=$ActivityOutput['Get Twitter Connection'].AccessTokenSecret}

访问 Azure 资源所需的身份验证Authenticate to Azure resources

Azure 自动化中用于管理 Azure 资源的 Runbook 将需要通过 Azure 进行身份验证。Runbooks in Azure Automation that manage Azure resources require authentication to Azure. 运行方式帐户(亦称为“服务主体”)是自动化 Runbook 用于访问订阅中的 Azure 资源管理器资源的默认机制。The Run As account, also referred to as a service principal, is the default mechanism that an Automation runbook uses to access Azure Resource Manager resources in your subscription. 通过向画布添加使用 PowerShell Get-AutomationConnection cmdlet 的 AzureRunAsConnection 连接资产,可以将此功能添加到图形 Runbook。You can add this functionality to a graphical runbook by adding the AzureRunAsConnection connection asset, which uses the PowerShell Get-AutomationConnection cmdlet, to the canvas. 还可以添加 Connect-AzAccount cmdlet。You can also add the Connect-AzAccount cmdlet. 以下示例展示了此方案。This scenario is illustrated in the following example.


Get Run As Connection 活动(或 Get-AutomationConnection)配置了名为 AzureRunAsConnection 的常量值数据源。The Get Run As Connection activity, or Get-AutomationConnection, is configured with a constant value data source named AzureRunAsConnection.


下一个活动(即 Connect-AzAccount)添加经过身份验证的运行方式帐户,以供在 Runbook 中使用。The next activity, Connect-AzAccount, adds the authenticated Run As account for use in the runbook.

Connect-AzAccount 参数集


对于 PowerShell Runbook,Add-AzAccountAdd-AzureRMAccountConnect-AzAccount 的别名。For PowerShell runbooks, Add-AzAccount and Add-AzureRMAccount are aliases for Connect-AzAccount. 请注意,这些别名不适用于图形 Runbook。Note that these aliases are not available for your graphical runbooks. 图形 Runbook 只能使用 Connect-AzAccount 本身。A graphical runbook can only use Connect-AzAccount itself.

对于参数字段 APPLICATIONID、CERTIFICATETHUMBPRINT 和 TENANTID,指定字段路径的属性名,因为活动输出的是包含多个属性的对象。For the parameter fields APPLICATIONID, CERTIFICATETHUMBPRINT, and TENANTID, specify the name of the property for the field path, since the activity outputs an object with multiple properties. 否则,当 Runbook 执行时,它会在尝试进行身份验证时失败。Otherwise, when the runbook executes, it fails while attempting to authenticate. 这是你在通过运行方式帐户对 Runbook 进行身份验证时必须满足的最低要求。This is what you need at a minimum to authenticate your runbook with the Run As account.

一些订阅者使用 Azure AD 用户帐户创建自动化帐户,用于管理 Azure 经典部署,或用于 Azure 资源管理器资源。Some subscribers create an Automation account using an Azure AD user account to manage Azure classic deployment or for Azure Resource Manager resources. 为了保持这些订阅者的向后兼容性,Runbook 中使用的身份验证机制是包含凭据资产Add-AzureAccount cmdlet。To maintain backward compatibility for these subscribers, the authentication mechanism to use in your runbook is the Add-AzureAccount cmdlet with a credential asset. 资产表示有权访问 Azure 帐户的 Active Directory 用户。The asset represents an Active Directory user with access to the Azure account.

可以为图形 Runbook 启用此功能,具体方法是将凭据资产添加到画布中,后加使用凭据资产作为输入的 Add-AzureAccount 活动。You can enable this functionality for your graphical runbook by adding a credential asset to the canvas, followed by an Add-AzureAccount activity that uses the credential asset for its input. 请参阅以下示例。See the following example.


Runbook 必须在启动时和每个检查点之后进行身份验证。The runbook must authenticate at its start and after each checkpoint. 因此,必须在任何 Checkpoint-Workflow 活动之后使用 Add-AzureAccount 活动。Thus you must use an Add-AzureAccount activity after any Checkpoint-Workflow activity. 不需要使用额外的凭据活动。You do not need to use an additional credential activity.


导出图形 RunbookExport a graphical runbook

可以只导出图形 Runbook 的已发布版本。You can only export the published version of a graphical runbook. 如果 Runbook 还没有发布,“导出”按钮处于禁用状态。If the runbook has not yet been published, the Export button is disabled. 在你单击“导出”按钮后,Runbook 下载到本地计算机。When you click the Export button, the runbook downloads to your local computer. 文件名与扩展名为 .graphrunbook 的 Runbook 的名称一致。The name of the file matches the name of the runbook with a .graphrunbook extension.

导入图形 RunbookImport a graphical runbook

通过在添加 Runbook 时选择“导入”选项,可以导入图形 Runbook 或图形 PowerShell 工作流 Runbook 文件。You can import a graphical or graphical PowerShell Workflow runbook file by selecting the Import option when adding a runbook. 选择要导入的文件时,可以保留相同的名称,也可以提供新名称。When you select the file to import, you can keep the same name or provide a new one. “Runbook 类型”字段在评估所选文件后显示 Runbook 的类型。The Runbook Type field displays the type of runbook after it assesses the file selected. 如果你尝试选择不正确的其他类型,图形编辑器会显示一条消息,指明存在潜在冲突,并且在转换过程中可能会出现语法错误。If you attempt to select a different type that is not correct, the graphical editor presents a message noting that there are potential conflicts and there might be syntax errors during conversion.

导入 Runbook

测试图形 RunbookTest a graphical runbook

Azure 自动化中的每个图形 Runbook 都有草稿版和发布版。Each graphical runbook in Azure Automation has a Draft version and a Published version. 只能运行发布版,同时只能编辑草稿版。You can run only the Published version, while you can only edit the Draft version. 已发布版不受对草稿版所做的任何更改的影响。The Published version is unaffected by any changes to the Draft version. 当草稿版可以使用时,发布它,这样草稿版就会覆盖当前的发布版。When the Draft version is ready for use, you publish it, which overwrites the current Published version with your Draft version.

可以在 Azure 门户中测试草稿版 Runbook,同时保持发布版 Runbook 不变。You can test the Draft version of a runbook in the Azure portal while leaving the Published version unchanged. 或者,也可以在发布新的 Runbook 之前测试它,这样可以在替换任何版本之前验证 Runbook 是否正常运行。Alternatively, you can test a new runbook before it has been published so that you can verify that the runbook works correctly before any version replacements. Runbook 测试执行的是草稿版,并确保它执行的所有操作都已完成。Testing of a runbook executes the Draft version and ensures that any actions that it performs are completed. 不会创建作业历史记录,但“测试输出”窗格中会显示输出。No job history is created, but the Test Output pane displays the output.

打开要编辑的 Runbook,然后单击“测试窗格”,以打开图形 Runbook 的“测试”控件。Open the Test control for your graphical runbook by opening the runbook for edit and then clicking Test pane. “测试”控件会提示提供输入参数,可以通过单击“启动”来启动 Runbook。The Test control prompts for input parameters, and you can start the runbook by clicking Start.

发布图形 RunbookPublish a graphical runbook

打开要编辑的 Runbook,然后单击“发布”,以发布图形 Runbook。Publish a graphical runbook by opening the runbook for editing and then clicking Publish. 可能的 Runbook 状态包括:Possible statuses for the runbook are:

  • 新 - Runbook 尚未发布。New -- the runbook has not been published yet.
  • 已发布 - Runbook 已发布。Published -- the runbook has been published.
  • 正在编辑 - Runbook 在发布后进行了编辑,草稿版和发布版是不同的。In edit -- the runbook has been edited after it has been published, and the Draft and Published versions are different.

Runbook 状态

可以视需要选择还原到发布版 Runbook。You have the option to revert to the Published version of a runbook. 此操作会丢弃自上次发布 Runbook 以来所做的任何更改。This operation throws away any changes made since the runbook was last published. 它将草稿版 Runbook 替换为发布版。It replaces the Draft version of the runbook with the Published version.

后续步骤Next steps