Azure 自动化中的图形创作Graphical authoring in Azure Automation

图形创作可以用来为 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 add activities to the canvas from a library of cmdlets and runbooks, link them together and configure 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), then this should look familiar to you

本文提供了图形创作简介,以及着手创建图形 Runbook 所需的概念。This article provides an introduction to graphical authoring and the concepts you need to get started in creating a graphical runbook.

图形 RunbookGraphical runbooks

Azure 自动化中的所有 Runbook 都是 Windows PowerShell 工作流。All runbooks in Azure Automation are Windows PowerShell Workflows. 图形 Runbook 和图形 PowerShell 工作流 Runbook 会生成由自动化辅助角色运行的 PowerShell 代码,但你无法看到它,也不能直接修改它。Graphical and Graphical PowerShell Workflow runbooks generate PowerShell code that is run by the Automation workers, but you are not able to view it or directly modify it. 图形 Runbook 可以转换为图形 PowerShell 工作流 Runbook,反之亦然,但它们不能转换为文本 Runbook。A Graphical runbook can be converted to a Graphical PowerShell Workflow runbook and vice-versa, but they cannot be converted to a textual runbook. 不能将现有文本 Runbook 导入图形编辑器。An existing textual runbook cannot be imported into the graphical editor.

图形编辑器概述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

画布是设计 Runbook 的地方。The Canvas is where you design your runbook. 用户可以将库控件中节点的活动添加到 Runbook,并将其通过链接进行连接,以便定义 Runbook 的逻辑。You add activities from the nodes in the Library control to the runbook and connect them with links to define the logic of the runbook.

可以使用画布底部的控件来放大和缩小。You can use the controls at the bottom of the canvas to zoom in and out.

库控件Library control

库控件是你选择要添加到 Runbook 的 活动 的地方。The Library control is where you select activities to add to your runbook. 用户可以将活动添加到画布,再将它们连接到其他活动。You add them to the canvas where you connect them to other activities. 它包括下表中描述的四个部分:It includes four sections described in the following table:

部分Section 说明Description
CmdletCmdlets 包括可以在 Runbook 中使用的所有 cmdlet。Includes all the cmdlets that can be used in your runbook. Cmdlet 按模块组织。Cmdlets are organized by module. 所有安装在自动化帐户中的模块都可用。All of the modules that you have installed in your automation account are available.
RunbookRunbooks 包括你自动化帐户中的 Runbook。Includes the runbooks in your automation account. 这些 Runbook 可以添加到画布中用作子 Runbook。These runbooks can be added to the canvas to be used as child runbooks. 仅显示核心类型与所编辑 Runbook 相同的 Runbook;对于图形 Runbook,仅显示基于 PowerShell 的 Runbook,而对于图形 PowerShell 工作流 Runbook,则仅显示基于 PowerShell 工作流的 Runbook。Only runbooks of the same core type as the runbook being edited are shown; for Graphical runbooks only PowerShell-based runbooks are shown, while for Graphical PowerShell Workflow runbooks only PowerShell-Workflow-based runbooks are shown.
资产Assets 包括自动化帐户中能够在 Runbook 中使用的 自动化资产Includes the automation assets in your automation account that can be used in your runbook. 将资产添加到 Runbook 中时,它会添加一个可以获取所选资产的工作流活动。When you add an asset to a runbook, it 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 中使用的 Runbook 控件活动。Includes runbook control activities that can be used in your current runbook. 交接点 采用多个输入,并会等到所有输入完成后才会继续执行工作流。A Junction takes multiple inputs and waits until all have completed before continuing the workflow. Code 活动运行一行或多行 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 is where you provide details for an object selected on the canvas. 此控件中的可用属性将取决于所选对象的类型。The properties available in this control depends on the type of object selected. 在配置控件中选择一个选项时,它会打开用于提供额外信息的其他边栏选项卡。When you select an option in the Configuration control, it opens additional blades in order to provide additional 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.

图形 Runbook 过程Graphical runbook procedures

导出和导入图形 RunbookExporting and importing a graphical runbook

可以只导出图形 Runbook 的已发布版本。You can only export the published version of a graphical runbook. 如果尚未发布 Runbook,则将禁用“导出”按钮。If the runbook has not yet been published, then the Export button is disabled. 单击“导出”按钮时,Runbook 将下载到本地计算机。When you click the Export button, the runbook is downloaded to your local computer. 文件名与带有 graphrunbook 扩展名的 Runbook 名称匹配。The name of the file matches the name of the runbook with a graphrunbook extension.

可以在添加 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 will display the type of runbook after it assesses the file selected and if you attempt to select a different type that is not correct, a message will be presented noting there are potential conflicts and during conversion, there could be syntax errors.

导入 Runbook

测试图形 RunbookTesting a graphical runbook

可以在 Azure 门户中测试 Runbook 的草稿版,对 Runbook 的已发布版不做任何更改,也可以在新的 Runbook 发布前对其进行测试。You can test the Draft version of a runbook in the Azure portal while leaving the published version of the runbook unchanged, or you can test a new runbook before it has been published. 这样便可以确保新的 Runbook 在替代已发布版之前各项功能运行正常。This allows you to verify that the runbook is working correctly before replacing the published version. 测试 Runbook 时,将执行草稿版 Runbook,并会完成其所执行的任何操作。When you test a runbook, the Draft runbook is executed and any actions that it performs are completed. 不会创建作业历史记录,但会在“测试输出窗格”中显示输出。No job history is created, but output is displayed in the Test Output Pane.

如果要打开 Runbook 的测试控件,可先打开要编辑的该 Runbook,并单击“测试窗格” 按钮。Open the Test control for a runbook by opening the runbook for edit and then click on the Test pane button.

测试控件会提示输入参数,可以通过单击“启动”按钮来启动 Runbook。The Test control prompts for any input parameters, and you can start the runbook by clicking on the Start button.

发布图形 RunbookPublishing a graphical runbook

Azure 自动化中的每个 Runbook 都有草稿版和已发布版。Each runbook in Azure Automation has a Draft 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 is ready to be available, then you publish it, which overwrites the Published version with the Draft version.

发布图形 Runbook 时,用户可以先打开要编辑的 Runbook,并单击“发布” 按钮即可。You can publish a graphical runbook by opening the runbook for editing and then clicking on the Publish button.

当 Runbook 尚未发布时,其状态为“新建” 。When a runbook has not yet been published, it has a status of New. 当 Runbook 发布后,其状态为“已发布” 。When it is published, it has a status of Published. 如果对已发布的 Runbook 进行编辑,而草稿版不同于已发布版,则 Runbook 的状态为“正在编辑”。If you edit the runbook after it has been published, and the Draft and Published versions are different, the runbook has a status of In edit.

Runbook 状态

还可以选择还原到 Runbook 的已发布版。You also have the option to revert to the Published version of a runbook. 这会抛弃自上次发布 Runbook 以来所做的任何更改,并会将 Runbook 的草稿版替换为已发布版。This throws away any changes made since the runbook was last published and replaces the Draft version of the runbook with the Published version.

活动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 activity. 将活动添加到 Runbook 时,可以右键单击库控件中的该活动,并选择“添加到画布”。You 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 in any way. 可以对 Runbook 进行任何形式的布局,只要觉得该布局最适合实现 Runbook 操作的可视化即可。You can lay out your runbook however you find it most suitable to visualize its operation.

添加到画布

在画布上选择需要在“配置”边栏选项卡中配置其属性和参数的活动。Select the activity on the canvas to configure its properties and parameters in the Configuration blade. 可以将活动的“标签”更改成觉得更具描述性的内容。You can change the Label of the activity to something that is descriptive to you. 原始 cmdlet 仍在运行,只是更改其要在图形编辑器中使用的显示名称。The original cmdlet is still being run, you are simply changing its display name that is used in the graphical editor. 该标签在 Runbook 中必须是唯一的。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 都有至少一个参数集,一些 cmdlet 会有多个参数集。All cmdlets have at least one parameter set, and some have multiple. 如果某个 cmdlet 有多个参数集,则必须选择要使用的那个参数集,才能配置参数。If a cmdlet has multiple parameter sets, then you must select which one you use before you can configure parameters. 能够配置的参数将取决于所选择的参数集。The parameters that you can configure depends on the parameter set that you choose. 可以更改某个活动使用的参数集,只需选中“参数集”,然后选择另一参数集即可。You can change the parameter set used by an activity by selecting Parameter Set and selecting another set. 在这种情况下,配置的任何参数值都将丢失。In this case, any parameter values that you configured are lost.

在下面的示例中,Get-AzureRmVM cmdlet 有三个参数集。In the following example, the Get-AzureRmVM cmdlet has three parameter sets. 在选择其中一个参数集之前,你无法配置参数值。You cannot configure parameter values until you select one of the parameter sets. ListVirtualMachineInResourceGroupParamSet 参数集用于返回资源组中的所有虚拟机,有一个可选参数。The ListVirtualMachineInResourceGroupParamSet parameter set is for returning all virtual machines in a resource group and has a single optional parameter. GetVirtualMachineInResourceGroupParamSet 用于指定要返回的虚拟机,有两个必需参数和一个可选参数。The GetVirtualMachineInResourceGroupParamSet is for specifying the virtual machine you want to return and has two mandatory 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 depends 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 is only available for the following data types: Int32, Int64, String,Boolean,DateTime,Switch.
活动输出Activity Output 工作流中某个位于当前活动前面的活动的输出。Output from an activity that precedes the current activity in the workflow. 将列出所有有效的活动。All valid activities are listed. 只选择要将其输出用于参数值的活动。Select just the activity to use its output for the parameter value. 如果该活动输出的对象具有多个属性,可以在选择活动之后键入属性的名称。If the activity outputs an object with multiple properties, then you can type in the name of the property after selecting the activity.
Runbook 输入Runbook Input 选择一个 Runbook 输入参数作为活动参数的输入。Select a runbook input parameter as input to 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 simple PowerShell expression. 对表达式的计算将先于用于参数值的活动和结果之前。The expression is evaluated before the activity and the result 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 清除以前配置的任何值。Clears 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 语法提供参数。You are presented with a text box where you can provide parameters using PowerShell syntax. 例如,要使用 Verbose 通用参数,可以指定 "-Verbose:$True"For example, to use the Verbose common parameter, you would specify "-Verbose:$True".

重试活动Retry activity

重试行为 可让活动运行多次,直到满足特定的条件为止,很像一个循环。Retry Behavior allows an activity 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, and may 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 no delay is specified, then the activity will run again immediately after it completes.

活动重试延迟

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

活动重试延迟

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

属性Property 说明Description
NumberOfAttemptsNumberOfAttempts 活动已运行的次数。Number of times that the activity has been run.
输出Output 活动上次运行的输出。Output from the last run of the activity.
TotalDurationTotalDuration 活动首次开始之后的经过时间。Timed elapsed since the activity was started the first time.
StartedAtStartedAt 活动首次开始的时间(UTC 格式)。Time in UTC format the activity was first started.

下面是活动重试条件的示例。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 when you review the configuration of the activity.

活动重试可视指示符

工作流脚本控件Workflow Script control

代码控件是一种特殊的活动,该活动根据图形 Runbook 的类型来接受 PowerShell 或 PowerShell 工作流脚本,而图形 Runbook 在经过授权后可以提供不授权就无法提供的功能。A Code control is a special activity that accepts PowerShell or PowerShell Workflow script depending on the type of graphical runbook being authored in order to provide functionality that may otherwise not be available. 它不能接受参数,但可以使用针对活动输出和 Runbook 输入参数的变量。It cannot accept parameters, but it can use variables for activity output and runbook input parameters. 活动的任何输出都将添加到数据总线中,除非它没有传出链接,这种情况下,会将它添加到 Runbook 的输出中。Any output of the activity is added to the databus unless it has no outgoing link in which case it is added to the output of the runbook.

例如,以下代码使用称为 $NumberOfDays 的 Runbook 输入变量执行日期计算。For example, the following code performs date calculations using a runbook input variable called $NumberOfDays. 然后,它会将计算所得的日期时间作为输出发送,供 Runbook 中的后续活动使用。It then sends a calculated date time as output to be used by subsequent activities in the runbook.

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

图形 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.

在两个活动之间创建链接,方法是:先选择源活动,然后单击图形底部的圆圈。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. 这种情况下,会包括下表中描述的链接类型。This includes the link type, which is described in the following table:

链接类型Link Type 说明Description
管道Pipeline 对于源活动中的每个对象输出,目标活动都会运行一次。The destination activity is run 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. 它会接收来自源活动的对象数组。It receives an array of objects from the source activity. 源活动的输出可用作对象数组。Output from the source activity is available as an array of objects.

启动活动Starting activity

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

ConditionsConditions

指定链接的条件时,目标活动仅在该条件解析为 true 的情况下运行。When you specify a condition on a link, the destination activity is only run 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 specify a condition for a single object, and the condition is evaluated for each object output by the source activity. 然后会针对符合条件的每个对象运行目标活动。The destination activity is then run for each object that satisfies the condition. 例如,通过 Get-AzureRmVm 源活动,可以将以下语法用于条件管道链接,以便只检索名为 Group1的资源组中的虚拟机。For example, with a source activity of Get-AzureRmVm, the following syntax could be used for a conditional pipeline link to retrieve only virtual machines in the resource group named Group1.

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

对于序列链接,只会对条件评估一次,因为会返回单个包含源活动中所有对象输出的数组。For a sequence link, the condition is only evaluated once since a single array is returned containing all objects output from the source activity. 因此,不能使用序列链接来进行管道链接这样的筛选,但可以通过该链接来直接确定是否会运行下一活动。Because of this, a sequence link cannot be used for filtering like a pipeline link but will simply determine whether or not the next activity is run. 以下面的活动集(位于 Start VM Runbook 中)为例。Take for example the following set of activities in our Start VM runbook.

使用序列的条件链接

共有三个不同的序列链接,用于确保已将值提供给两个代表 VM 名称和资源组名称的 Runbook 输入参数,以便确定所要采取的适当操作:启动单个 VM、启动资源组中的所有 VM,或者启动订阅中的所有 VM。There are three different sequence links that are verifying values were provided to two runbook input parameters representing VM name and Resource Group name in order to determine which is the appropriate action to take - start a single VM, start all VMs in the resource group, or all VMs in a subscription. 对于“连接到 Azure”和“获取单个 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, then the data available to activities in each branch depend on the condition in the link connecting to that branch.

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

条件链接示例

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

交接点Junctions

一个交接点是一个特殊活动,它会一直等到所有传入分支完成为止。A junction is a special activity that waits until all incoming branches have completed. 这可以让你并行运行多个活动,并确保在所有活动完成后再继续。This allows you 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, not more than one of those links can be a pipeline. 传入序列链接的数目不受限制。The number of incoming sequence links is not constrained. 系统允许创建具有多个传入管道链接的结合点并保存 Runbook,但在运行时会失败。You are allowed to create the junction with multiple incoming pipeline links and save the runbook, but it fails 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 继续之前两个进程都已完成。A junction is used to ensure that both processes are completed before the runbook continues.

交接点

周期Cycles

一个周期是指目标活动需要多长时间才能通过链接回到其源活动,或者回到最终会通过链接回到其源活动的其他活动。A cycle is when a destination activity links back to its source activity or to another activity that eventually links back to its source. 目前不允许使用周期来进行图形创作。Cycles are currently not allowed in graphical authoring. 如果 Runbook 有一个周期,可以正常保存它,但在运行它时会收到一个错误。If your runbook has a cycle, it saves properly but receives an error when it runs.

周期

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

由活动通过传出链接输出的任何数据都会写入 Runbook 的 数据总线Any data that is output by an activity 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, the data is output as multiples 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.

可以使用两种方法之一访问数据总线上的数据。You can access data on the databus using one of two methods. 第一种方法是使用 活动输出 数据源来填充另一活动的参数。First is using an Activity Output data source to populate a parameter of another activity. 如果输出是一个对象,可以指定单个属性。If the output is an object, you can specify a single property.

活动输出

还可以在 PowerShell 表达式数据源中检索活动的输出,或者使用 ActivityOutput 变量通过工作流脚本活动这样做。You can also retrieve the output of an activity in a PowerShell Expression data source or from a Workflow Script activity with an ActivityOutput variable. 如果输出是一个对象,可以指定单个属性。If the output is an object, you can specify a single property. ActivityOutput 变量使用以下语法。ActivityOutput variables use the following syntax.

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

检查点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, it is not available in Graphical runbooks. 如果 Runbook 使用 Azure cmdlet,当 Runbook 暂停并且在不同的辅助角色上从此检查点重新开始时,应使用 Connect-AzureRmAccount 遵循任何检查点活动。If the runbook uses Azure cmdlets, you should follow any checkpointed activity with an Connect-AzureRmAccount in case the runbook is suspended and restarts from this checkpoint on a different worker.

通过 Azure 资源进行身份验证Authenticating 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 method to access Azure Resource Manager resources in your subscription with Automation runbooks. 可以将此功能添加到图形 Runbook,只需向画布添加 AzureRunAsConnection 连接资产即可,该资产使用 PowerShell Get-AutomationConnection cmdlet 和 Connect-AzureRmAccount cmdlet。You can add this functionality to a graphical runbook by adding the AzureRunAsConnection Connection asset, which is using the PowerShell Get-AutomationConnection cmdlet, and Connect-AzureRmAccount cmdlet to the canvas. 以下示例对此进行了演示:This is illustrated in the following example:

运行方式身份验证活动

“获取运行方式连接”活动(即 Get-AutomationConnection)配置了名为 AzureRunAsConnection 的常量值数据源。The Get Run As Connection activity (that is, Get-AutomationConnection), is configured with a constant value data source named AzureRunAsConnection.

运行方式连接配置

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

Connect-AzureRmAccount 参数集

Important

Add-AzureRmAccount 现在是 Connect-AzureRMAccount 的别名。Add-AzureRmAccount is now an alias for Connect-AzureRMAccount. 搜索库项时,如果未看到 Connect-AzureRMAccount,可以使用 Add-AzureRmAccount,或更新自动化帐户中的模块。When searching your library items, if you do not see Connect-AzureRMAccount, you can use Add-AzureRmAccount, or you can update your modules in your Automation Account.

对于参数 APPLICATIONIDCERTIFICATETHUMBPRINTTENANTID,需指定字段路径的属性的名称,因为该活动所输出的对象具有多个属性。For the parameters APPLICATIONID, CERTIFICATETHUMBPRINT, and TENANTID you need to specify the name of the property for the Field path because the activity outputs an object with multiple properties. 否则,执行 Runbook 时,将无法进行身份验证。Otherwise when you execute the runbook, it fails 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 资源管理器资源,身份验证的方法是带有凭据资产的 Add-AzureAccount cmdlet,该资产代表具有 Azure 帐户访问权限的 Active Directory 用户。To maintain backwards compatibility for subscribers who have created an Automation account using an Azure AD User account to manage Azure classic deployment or for Azure Resource Manager resources, the method to authenticate is the Add-AzureAccount cmdlet with a credential asset that represents an Active Directory user with access to the Azure account.

用户可以将此功能添加到图形 Runbook,只需将凭据资产添加到画布,并完成 Add-AzureAccount 活动即可。You can add this functionality to a graphical runbook by adding a credential asset to the canvas followed by an Add-AzureAccount activity. Add-AzureAccount 使用凭据活动作为其输入。Add-AzureAccount uses the credential activity for its input. 以下示例对此进行了演示:This is illustrated in the following example:

身份验证活动

必须在 Runbook 启动时以及每个检查点之后进行身份验证。You have to authenticate at the start of the runbook and after each checkpoint. 这意味着在每个 Checkpoint-Workflow 活动之后添加一个表示添加凭据的 Add-AzureAccount 活动。This means adding an addition Add-AzureAccount activity after any Checkpoint-Workflow activity. 不需要添加凭据活动,因为可以使用相同的You do not need an addition credential activity since you can use the same

活动输出

Runbook 输入和输出Runbook input and output

Runbook 输入Runbook input

Runbook 可能会要求用户提供输入(如果该用户是通过 Azure 门户启动的该 Runbook),或者会要求另一 Runbook 提供输入(如果当前的 Runbook 是小孩在用)。A runbook may require input either from a user when they start the runbook through the Azure portal or from another runbook if the current one is used as a child. 例如,如果使用 Runbook 创建虚拟机,则每次启动 Runbook 时,可能需要提供虚拟机名称、其他属性等信息。For example, if you have a runbook that creates a virtual machine, you may need to provide information such as the name of the virtual machine and other properties each time you start the runbook.

可以通过定义一个或多个输入参数来接受 Runbook 的输入。You accept input for a runbook by defining one or more input parameters. 每次启动 Runbook 时,都需要为这些参数提供值。You provide values for these parameters each time the runbook is started. 通过 Azure 门户启动 Runbook 时,该门户会提示为每个 Runbook 的输入参数提供值。When you start a runbook with the Azure portal, it prompts you to provide values for each of the runbook's input parameters.

可以通过单击 Runbook 工具栏上的“输入和输出”按钮来访问 Runbook 的输入参数。You can access input parameters for a runbook by clicking the Input and output button 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:

属性Property 说明Description
名称Name 参数的唯一名称。The unique name of the parameter. 此项只能包含字母数字字符,不能包含空格。This can only contain alpha numeric characters and cannot contain a space.
说明Description 针对输入参数的可选说明。An optional description for the input parameter.
类型Type 参数值应有的数据类型。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.
必需Mandatory 指定是否必须为该参数提供值。Specifies whether a value must be provided for the parameter. 如果没有为每个没有定义默认值的必需参数提供值,则无法启动 Runbook。The runbook cannot be started if you do not provide a value for each mandatory parameter that does not have a default value defined.
默认值Default Value 指定在未提供值的情况下,对参数使用什么值。Specifies what value is used for the parameter if one is not provided. 此项可以为 Null 或特定值。This can either be Null or a specific value.

Runbook 输出Runbook output

由任何没有传出链接的活动创建的数据将保存到 Runbook 的输出Data created by any activity that does not have an outgoing link is saved 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 表达式PowerShell expressions

图形编写的优点之一是提供以 PowerShell 的基本知识创建 Runbook 的能力。One of the advantages of graphical authoring is providing you with the ability to build a runbook with minimal knowledge of PowerShell. 目前,确实需要熟悉一下 PowerShell 才能填充某些参数值和设置链接条件Currently, you do need to know a bit of PowerShell though for populating certain parameter values and for setting link conditions. 本部分提供 PowerShell 表达式的快速简介供不熟悉的用户参考。This section provides a quick introduction to PowerShell expressions for those users who may not be familiar with it. 使用 Windows PowerShell 编写脚本中提供了 PowerShell 的完整详细信息。Full details of PowerShell are available at Scripting with Windows PowerShell.

PowerShell 表达式数据源PowerShell expression 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 some PowerShell code. 这可以是执行某个简单函数的单行代码,或执行某个复杂逻辑的多行代码。This could be a single line of code that performs some 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 would output the current date.

Get-Date

以下命令将从当前日期创建字符串并将它分配给变量。The following commands build a string from the current date and assign it to a variable. 然后将变量的内容发送到输出The contents of the variable are then sent to the output

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

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

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

活动输出Activity output

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

$ActivityOutput['Activity Label'].PropertyName

例如,可能某个活动具有需要虚拟机名称的属性,在此情况下可以使用以下表达式:For example, you may have an activity with a property that requires the name of a virtual machine in which case you could use the following expression:

$ActivityOutput['Get-AzureVm'].Name

如果是需要虚拟机对象的属性而不只是单纯的属性,则要使用以下语法返回整个对象。If the property that required the virtual machine object instead of just a property, then you would return the entire object using the following syntax.

$ActivityOutput['Get-AzureVm']

也可以在更复杂的表达式中使用活动的输出,例如将文本串联到虚拟机名称的以下表达式。You can also use the output of an activity in a more complex expression such as the following that concatenates text to the virtual machine name.

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

ConditionsConditions

使用比较运算符来比较值或确定值是否与指定的模式匹配。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 whether the virtual machine from an activity named Get-AzureVM is currently stopped.

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

以下条件检查同一虚拟机是否处于“已停止”以外的任何状态。The following condition checks whether the same virtual machine is in any state other than stopped.

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

可以使用逻辑运算符(例如 -and-or)添加多个条件。You can join multiple conditions using a logical operator such as -and or -or. 例如,以下条件会检查上述示例中同一虚拟机的状态是“已停止”还是“正在停止”。For example, the following condition checks whether the same 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")

哈希表Hashtables

哈希表是返回一组值时很有用的名称/值对。Hashtables are name/value pairs that are useful for returning a set of values. 某些活动的属性可能是哈希表而不是简单值。Properties for certain activities may expect a hashtable instead of a simple value. 哈希表有时也称为字典。You may also see as hashtable referred to as a dictionary.

使用以下语法创建哈希表。You create a hashtable with the following syntax. 哈希表可以包含任意数目的条目,但每个条目由一个名称和值定义。A hashtable 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 in the data source for an activity parameter that expected a hashtable with values for an internet search.

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

以下示例使用名为 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}

后续步骤Next Steps