教程:创建图形 RunbookTutorial: Create a graphical runbook

本教程指导在 Azure 自动化中创建 图形 RunbookThis tutorial walks you through the creation of a graphical runbook in Azure Automation. 可以在 Azure 门户中使用图形编辑器创建和编辑图形 Runbook 与图形 PowerShell 工作流 Runbook。You can create and edit graphical and graphical PowerShell Workflow runbooks using the graphical editor in the Azure portal.

在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 创建简单的图形 RunbookCreate a simple graphical runbook
  • 测试并发布 RunbookTest and publish the runbook
  • 运行 Runbook 作业并跟踪其状态Run and track the status of the runbook job
  • 更新 Runbook 以使用 Runbook 参数和条件链接启动 Azure 虚拟机Update the runbook to start an Azure virtual machine, with runbook parameters and conditional links

先决条件Prerequisites

要完成本教程,需要以下各项:To complete this tutorial, you need the following:

  • Azure 订阅。Azure subscription. 如果没有 Azure 订阅,可在开始之前创建一个 1 元试用帐户。If you don't have one yet, you can create a 1rmb trial account before you begin.
  • 自动化帐户 ,用来保存 Runbook 以及向 Azure 资源进行身份验证。Automation account to hold the runbook and authenticate to Azure resources. 此帐户必须有权启动和停止虚拟机。This account must have permission to start and stop the virtual machine.
  • Azure 虚拟机。An Azure virtual machine. 由于需要停止并启动此虚拟机,因此它不应当是生产 VM。Since you stop and start this machine, it shouldn't be a production VM.
  • 如果需要,请导入 Azure 模块更新模块,具体取决于所用的 cmdlet。If necessary, import Azure modules or update modules based on the cmdlets that you use.

步骤 1 - 创建 RunbookStep 1 - Create runbook

首先创建一个将输出文本 Hello World 的简单 Runbook。Start by creating a simple runbook that outputs the text Hello World.

  1. 在 Azure 门户中,打开自动化帐户。In the Azure portal, open your Automation account.

    通过自动化帐户页面可快速查看此帐户中的资源。The Automation account page gives you a quick view of the resources in this account. 应该已拥有某些资产。You should already have some assets. 大多数此类资产是自动包括在新的自动化帐户中的模块。Most of those assets are the modules automatically included in a new Automation account. 你还应该有与你的订阅关联的凭据资产。You should also have the Credential asset associated with your subscription.

  2. 在“流程自动化”下选择“Runbook”,以打开 Runbook 的列表。Select Runbooks under Process Automation to open the list of runbooks.

  3. 选择“创建 Runbook”,创建一个新的 Runbook。Create a new runbook by selecting Create a runbook.

  4. 将该 Runbook 命名为 MyFirstRunbook-GraphicalGive the runbook the name MyFirstRunbook-Graphical.

  5. 在本例中,你将创建一个图形 RunbookIn this case, you're going to create a graphical runbook. 选择“图形”作为“Runbook 类型”。Select Graphical for Runbook type.
    新建 RunbookNew runbook

  6. 单击“创建”以创建 Runbook 并打开图形编辑器 。Click Create to create the runbook and open the graphical editor.

步骤 2 - 添加活动Step 2 - Add activities

编辑器左侧的库控件允许选择要添加到 Runbook 的活动。The Library control on the left side of the editor allows you to select activities to add to your runbook. 你将添加 Write-Output cmdlet,以便从 Runbook 输出文本。You're going to add a Write-Output cmdlet to output text from the runbook.

  1. 在“库”控件中,单击搜索字段并键入 write-outputIn the Library control, click in the search field and type write-output. 下图显示了搜索结果。Search results are shown in the following image.
    Microsoft.PowerShell.UtilityMicrosoft.PowerShell.Utility

  2. 向下滚动到列表的底部。Scroll down to the bottom of the list. 右键单击“Write-Output”并选择“添加到画布”。Right-click Write-Output and select Add to canvas. 或者,可以单击 cmdlet 名称旁边的省略号图标 (...),然后选择“添加到画布”。Alternatively, you can click the ellipsis (...) next to the cmdlet name and then select Add to canvas.

  3. 单击画布上的 Write-Output 活动。Click the Write-Output activity on the canvas. 此操作会打开可用于配置活动的“配置控件”页。This action opens the Configuration control page, which allows you to configure the activity.

  4. “标签”字段默认为该 cmdlet 的名称,但可将其更改为更易记的名称。The Label field defaults to the name of the cmdlet, but you can change it to something more friendly. 将其更改为 Write Hello World to outputChange it to Write Hello World to output.

  5. 单击“参数”为 cmdlet 参数提供值。Click Parameters to provide values for the cmdlet's parameters.

    某些 cmdlet 有多个参数集,你需要选择要使用的参数集。Some cmdlets have multiple parameter sets, and you need to select which one to use. 在本例中,Write-Output 仅有一个参数集。In this case, Write-Output has only one parameter set.

  6. 选择 InputObject 参数。Select the InputObject parameter. 这是可用于指定发送到输出流的文本的参数。This is the parameter that you use to specify the text to send to the output stream.

  7. “数据源”下拉菜单中提供了可用于填充参数值的源。The Data source dropdown menu provides sources that you can use to populate a parameter value. 在此菜单中选择“PowerShell 表达式”。In this menu, select PowerShell expression.

    可以使用来自另一个活动、自动化资产或 PowerShell 表达式之类的源的输出。You can use output from such sources as another activity, an Automation asset, or a PowerShell expression. 在本例中,输出内容只是 Hello WorldIn this case, the output is just Hello World. 可以使用 PowerShell 表达式并指定一个字符串。You can use a PowerShell expression and specify a string.

  8. 在“表达式”字段中键入 Hello World,然后单击“确定”两次以返回到画布。In the Expression field, type Hello World and then click OK twice to return to the canvas.

  9. 通过单击“保存” 保存 Runbook。Save the runbook by clicking Save.

步骤 3 - 测试 RunbookStep 3 - Test the runbook

在发布 Runbook 使其可在生产中使用之前,应对其进行测试,确保其能正常运行。Before you publish the runbook to make it available in production, you should test it to make sure that it works properly. 测试 Runbook 时将运行其“草稿”版本,你可以通过交互方式查看其输出。Testing a runbook runs its Draft version and allows you to view its output interactively.

  1. 选择“测试窗格”以打开“测试”窗格。Select Test pane to open the Test pane.

  2. 单击“启动”以启动测试 。Click Start to start the test. 这应该是唯一的已启用选项。This should be the only enabled option.

  3. 请注意,此时会创建一个 Runbook 作业并在窗格中显示其状态。Note that a runbook job is created and its status is displayed in the pane.

    作业状态一开始为 Queued,表明作业正在等待云中的 Runbook 辅助角色变为可用状态。The job status starts as Queued, indicating that the job is waiting for a runbook worker in the cloud to become available. 在某个辅助角色认领该作业后,其状态会变为 StartingThe status changes to Starting when a worker claims the job. 最后,当 Runbook 开始实际运行时,其状态会变为 RunningFinally, the status becomes Running when the runbook actually starts to run.

  4. Runbook 作业完成后,“测试”窗格会显示其输出。When the runbook job completes, the Test pane displays its output. 在本例中,你会看到 Hello WorldIn this case, you see Hello World.

    Hello World

  5. 关闭测试窗格以返回到画布。Close the Test pane to return to the canvas.

步骤 4 - 发布和启动 RunbookStep 4 - Publish and start the runbook

创建的 Runbook 仍处于“草稿”模式。The runbook that you have created is still in Draft mode. 必须先将它发布,然后才能在生产环境中运行它。It needs to be published before you can run it in production. 当发布 Runbook 时,可以用草稿版本覆盖现有的已发布版本。When you publish a runbook, you overwrite the existing Published version with the Draft version. 在本例中,还没有已发布版本,因为刚刚创建 Runbook。In this case, you don't have a Published version yet because you just created the runbook.

  1. 选择“发布”以发布该 Runbook,并在出现提示时选择“是” 。Select Publish to publish the runbook and then Yes when prompted.

  2. 在“Runbook”页中向左滚动以查看该 Runbook,请注意,“创作状态”值设置为“已发布” 。Scroll left to view the runbook on the Runbooks page, and note that the Authoring Status value is set to Published.

  3. 往回向右滚动查看“MyFirstRunbook-Graphical”页。Scroll back to the right to view the page for MyFirstRunbook-Graphical.

    顶部的选项现在可用于启动 Runbook、计划将来的启动时间,或创建一个 webhook 以便通过 HTTP 调用来启动 Runbook。The options across the top allow you to start the runbook now, schedule a future start time, or create a webhook so that the runbook can be started through an HTTP call.

  4. 选择“启动”,并在出现提示时选择“是” 启动 runbook。Select Start and then Yes when prompted to start the runbook.

  5. 此时会为已创建的 Runbook 作业打开一个“作业”窗格。A Job pane is opened for the runbook job that has been created. 验证“作业状态”字段是否显示“已完成”。Verify that the Job status field shows Completed.

  6. 单击“输出”打开“输出”页,在其中可以看到显示了 Hello WorldClick Output to open the Output page, where you can see Hello World displayed.

  7. 关闭“输出”页。Close the Output page.

  8. 单击“所有日志”打开 Runbook 作业的“流”窗格。Click All Logs to open the Streams pane for the runbook job. 在输出流中应该只会看到 Hello WorldYou should only see Hello World in the output stream.

    请注意,“流”窗格可以显示 Runbook 作业的其他流,例如,详细流和错误流(如果 Runbook 向其写入内容)。Note that the Streams pane can show other streams for a runbook job, such as Verbose and Error streams, if the runbook writes to them.

  9. 关闭“流”窗格和“作业”窗格以返回到“MyFirstRunbook-Graphical”页。Close the Streams pane and the Job pane to return to the MyFirstRunbook-Graphical page.

  10. 若要查看 Runbook 的所有作业,请在“资源”下选择“作业”。To view all the jobs for the runbook, select Jobs under Resources. “作业”页列出了你的 Runbook 创建的所有作业。The Jobs page lists all the jobs created by your runbook. 由于只将该作业运行了一次,应该只会看到一个列出的作业。You should see only one job listed, since you have only run the job once.

  11. 单击作业名称,以打开你在启动 Runbook 时查看过的同一“作业”窗格。Click the job name to open the same Job pane that you viewed when you started the runbook. 使用此窗格可查看为 Runbook 创建的任何作业的详细信息。Use this pane to view the details of any job created for the runbook.

步骤 5 - 创建变量资产Step 5 - Create variable assets

现已测试并发布了 Runbook,但到目前为止尚未执行任何有用的操作来管理 Azure 资源。You've tested and published your runbook, but so far it doesn't do anything useful to manage Azure resources. 在配置用于身份验证的 Runbook 之前,必须创建一个变量来保存订阅 ID,设置一个活动来进行身份验证,然后引用该变量。Before configuring the runbook to authenticate, you must create a variable to hold the subscription ID, set up an activity to authenticate, and then reference the variable. 包括对订阅上下文的引用,这样就可以轻松地处理多个订阅。Including a reference to the subscription context allows you to easily work with multiple subscriptions.

  1. 从导航窗格上的“订阅”选项中复制你的订阅 ID。Copy your subscription ID from the Subscriptions option on the Navigation pane.

  2. 在“自动化帐户”页的“共享资源”下选择“变量”。In the Automation Accounts page, select Variables under Shared Resources.

  3. 选择“添加变量”。Select Add a variable.

  4. 在“新建变量”页上提供的字段中进行以下设置。On the New variable page, make the following settings in the fields provided.

    • 名称 -- 输入 AzureSubscriptionIdName -- enter AzureSubscriptionId.
    • -- 输入你的订阅 ID。Value -- enter your subscription ID.
    • 类型 -- 保留选择的字符串。Type -- keep string selected.
    • 加密 -- 使用默认值。Encryption -- use the default value.
  5. 单击“创建”以创建该变量。Click Create to create the variable.

步骤 6 - 添加身份验证Step 6 - Add authentication

现已创建一个变量来保存订阅 ID,接下来可以配置 Runbook,以使用订阅的运行方式凭据进行身份验证。Now that you have a variable to hold the subscription ID, you can configure the runbook to authenticate with the Run As credentials for your subscription. 为此,可将 Azure 运行方式连接添加为资产。Do this by adding the Azure Run As connection as an asset. 此外,必须将 Connect-AzAccount cmdlet 和 Set-AzContext cmdlet 添加到画布。You also must add the Connect-AzAccount cmdlet and the Set-AzContext cmdlet to the canvas.

备注

对于 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-AzAccountitself.

  1. 导航到 Runbook,在“MyFirstRunbook-Graphical”页上选择“编辑”。Navigate to your runbook and select Edit on the MyFirstRunbook-Graphical page.

  2. 不再需要 Write Hello World to output 条目。You don't need the Write Hello World to output entry any more. 只需单击省略号图标,然后选择“删除”即可。Just click the ellipsis and select Delete.

  3. 在“库”控件中展开“资产”,然后展开“连接”。In the Library control, expand ASSETS, then Connections. 选择“添加到画布”,将 AzureRunAsConnection 添加到画布。Add AzureRunAsConnection to the canvas by selecting Add to canvas.

  4. AzureRunAsConnection 重命名为 Get Run As ConnectionRename AzureRunAsConnection to Get Run As Connection.

  5. 在“库”控件的搜索字段中,键入 Connect-AzAccountIn the Library control, type Connect-AzAccount in the search field.

  6. Connect-AzAccount 添加到画布。Add Connect-AzAccount to the canvas.

  7. 将鼠标悬停在 Get Run As Connection 上方,直到在该形状的底部显示一个圆圈。Hover over Get Run As Connection until a circle appears on the bottom of the shape. 单击该圆圈并将箭头拖到 Connect-AzAccount,以构成一个链接。Click the circle and drag the arrow to Connect-AzAccount to form a link. Runbook 首先运行“Get Run As Connection”,然后运行 Connect-AzAccountThe runbook starts with Get Run As Connection and then runs Connect-AzAccount.
    创建活动之间的链接Create link between activities

  8. 在画布上选择 Connect-AzAccountOn the canvas, select Connect-AzAccount. 在“配置”控件窗格的“标签”字段中键入“登录到 Azure”。In the Configuration control pane, type Login to Azure in the Label field.

  9. 单击“参数”,此时会显示“活动参数配置”页。Click Parameters, and the Activity Parameter Configuration page appears.

  10. Connect-AzAccount cmdlet 有多个参数集,需要选择其中一个,然后提供参数值。The Connect-AzAccount cmdlet has multiple parameter sets, and you need to select one before providing parameter values. 单击“参数集”,然后选择“ServicePrincipalCertificateWithSubscriptionId” 。Click Parameter Set and then select ServicePrincipalCertificateWithSubscriptionId.

  11. 此参数集的参数将显示在“活动参数配置”页上。The parameters for this parameter set are displayed on the Activity Parameter Configuration page. 单击“APPLICATIONID”。Click APPLICATIONID.
    添加 Azure 帐户参数Add Azure account parameters

  12. 在“参数值”页上进行以下设置,然后单击“确定”。On the Parameter Value page, make the following settings and then click OK.

  • 数据源 -- 选择“活动输出”。Data source -- select Activity output.
  • 数据源列表 -- 选择“获取自动化连接”。Data source list -- select Get Automation Connection.
  • 字段路径 -- 键入 ApplicationIdField path -- type ApplicationId. 需要指定字段路径的属性的名称,因为活动所输出的对象包含多个属性。You're specifying the name of the property for the field path because the activity outputs an object with multiple properties.
  1. 单击“CERTIFICATETHUMBPRINT”,在“参数值”页上进行以下设置,然后单击“确定”。Click CERTIFICATETHUMBPRINT, and on the Parameter Value page, make the following settings and then click OK.

    • 数据源 -- 选择“活动输出”。Data source -- select Activity output.
    • 数据源列表 -- 选择“获取自动化连接”。Data source list -- select Get Automation Connection.
    • 字段路径 -- 键入 CertificateThumbprintField path -- type CertificateThumbprint.
  2. 单击“SERVICEPRINCIPAL”,在“参数值”页上为“数据源”字段选择“ConstantValue”,单击选项“True”;然后单击“确定” 。Click SERVICEPRINCIPAL, and on the Parameter Value page, select ConstantValue for the Data source field; click the option True; and then click OK.

  3. 单击“ENVIRONMENT”,在“参数值”页面中选择“ConstantValue”作为数据源,在文本框中输入“AzureChinaCloud”,然后单击“确定” 。Click ENVIRONMENT, and in the Parameter Value page, select ConstantValue for the Data source, Write "AzureChinaCloud" into the textbox, and then click OK.

  4. 单击“TENANTID”,在“参数值”页上进行以下设置。Click TENANTID, and make the following settings on the Parameter Value page. 完成后,单击“确定”两次。When finished, click OK twice.

    • 数据源 -- 选择“活动输出”。Data source -- select Activity output.
    • 数据源列表 -- 选择“获取自动化连接”。Data source list -- select Get Automation Connection.
    • 字段路径 -- 键入 TenantIdField path -- type TenantId.
  5. 在“库”控件的搜索字段中,键入 Set-AzContextIn the Library control, type Set-AzContext in the search field.

  6. Set-AzContext 添加到画布。Add Set-AzContext to the canvas.

  7. 在画布上选择 Set-AzContextSelect Set-AzContext on the canvas. 在“配置”控件窗格的“标签”字段中输入 Specify Subscription IdIn the Configuration control pane, enter Specify Subscription Id in the Label field.

  8. 单击“参数”,此时会显示“活动参数配置”页。Click Parameters and the Activity Parameter Configuration page appears.

  9. Set-AzContext cmdlet 有多个参数集,需要选择其中一个,然后提供参数值。The Set-AzContext cmdlet has multiple parameter sets, and you need to select one before providing parameter values. 单击“参数集”,然后选择“SubscriptionId”。Click Parameter Set and then select SubscriptionId.

  10. 此参数集的参数会显示在“活动参数配置”页上。The parameters for this parameter set are displayed on the Activity Parameter Configuration page. 单击“SubscriptionID”。Click SubscriptionID.

  11. 在“参数值”页上,为“数据源”字段选择“变量资产”,然后从源列表中选择“AzureSubscriptionId”。On the Parameter Value page, select Variable Asset for the Data source field and select AzureSubscriptionId from the source list. 完成后,单击“确定”两次。When finished, click OK twice.

  12. 将鼠标悬停在 Login to Azure 上方,直到在该形状的底部显示一个圆圈。Hover over Login to Azure until a circle appears on the bottom of the shape. 单击圆圈并将箭头拖到 Specify Subscription IdClick the circle and drag the arrow to Specify Subscription Id. 此时,Runbook 看起来应该如下所示。Your runbook should look like the following at this point.

    Runbook 身份验证配置

步骤 7 - 添加用于启动虚拟机的活动Step 7 - Add activity to start a virtual machine

现在,必须添加一个 Start-AzVM 活动来启动虚拟机。Now you must add a Start-AzVM activity to start a virtual machine. 可以选取 Azure 订阅中的任何 VM,但暂时请在 Start-AzVM cmdlet 中对 VM 名称进行硬编码。You can pick any VM in your Azure subscription, and for now you are hardcoding its name into the Start-AzVM cmdlet.

  1. 在“库”控件的搜索字段中,键入 Start-AzIn the Library control, type Start-Az in the search field.

  2. Start-AzVM 添加到画布,然后将其单击拖放到 Specify Subscription Id 下面。Add Start-AzVM to the canvas and then click and drag it underneath Specify Subscription Id.

  3. 将鼠标悬停在 Specify Subscription Id 上方,直到在该形状的底部显示一个圆圈。Hover over Specify Subscription Id until a circle appears on the bottom of the shape. 单击圆圈并将箭头拖到 Start-AzVMClick the circle and drag the arrow to Start-AzVM.

  4. 选择 Start-AzVMSelect Start-AzVM. 单击“参数”,然后单击“参数集”查看活动集。Click Parameters and then Parameter Set to view the sets for the activity.

  5. 选择“ResourceGroupNameParameterSetName”作为参数集。Select ResourceGroupNameParameterSetName for the parameter set. “ResourceGroupName”和“Name”字段旁边出现了感叹号,表示它们是必需的参数。The fields ResourceGroupName and Name have exclamation marks next to them to indicate that they are required parameters. 请注意,这两个字段都需要字符串值。Note that both fields expect string values.

  6. 选择“名称”。Select Name. 为“数据源”字段选择“PowerShell 表达式”。Choose PowerShell expression for the Data source field. 对于用于启动此 Runbook 的 VM,请键入带双引号的计算机名称。For the VM that you use to start this runbook, type in the machine name surrounded with double quotes. 单击“确定”。Click OK.

  7. 选择“ResourceGroupName”。Select ResourceGroupName. 对“数据源”字段使用值“PowerShell 表达式”,并键入带双引号的资源组名称 。Use the value PowerShell expression for the Data source field, and type in the name of the resource group surrounded with double quotes. 单击“确定”。Click OK.

  8. 单击“测试”窗格,以便测试 Runbook。Click Test pane so that you can test the runbook.

  9. 单击“启动”,开始测试。Click Start to begin the test. 完成后,请确保该 VM 已启动。Once it completes, make sure that the VM has started. 此时,Runbook 看起来应该如下所示。Your runbook should look like the following at this point.

    Runbook 身份验证配置

步骤 8 - 添加其他输入参数Step 8 - Add additional input parameters

Runbook 当前会启动资源组中为 Start-AzVM cmdlet 指定的 VM。Your runbook currently starts the VM in the resource group that you specified for the Start-AzVM cmdlet. 如果在 Runbook 启动时指定了 VM 名称和资源组,则该 Runbook 会更有用。The runbook will be more useful if you specify both name and resource group when the runbook is started. 让我们向 Runbook 中添加输入参数,以提供该功能。Let's add input parameters to the runbook to provide that functionality.

  1. 在“MyFirstRunbook-Graphical”页上单击“编辑”,打开图形编辑器。Open the graphical editor by clicking Edit on the MyFirstRunbook-Graphical page.

  2. 依次选择“输入和输出”和“添加输入”,打开“Runbook 输入参数”窗格 。Select Input and output and then Add input to open the Runbook Input Parameter pane.

  3. 在提供的字段中进行以下设置,然后单击“确定”。Make the following settings in the provided fields and then click OK.

    • 名称 -- 指定 VMNameName -- specify VMName.
    • 类型 -- 保留字符串设置。Type -- keep the string setting.
    • 必需 -- 将值更改为“是”。Mandatory -- change the value to Yes.
  4. 创建名为 ResourceGroupName 的另一个必需输入参数,然后单击“确定”以关闭“输入和输出”窗格。Create a second mandatory input parameter called ResourceGroupName and then click OK to close the Input and Output pane.
    Runbook 输入参数Runbook Input Parameters

  5. 选择 Start-AzVM 活动,然后单击“参数”。Select the Start-AzVM activity and then click Parameters.

  6. 将“名称”对应的“数据源”字段更改为“Runbook 输入”。Change the Data source field for Name to Runbook input. 然后选择“VMName”。Then select VMName.

  7. 将“ResourceGroupName”对应的“数据源”字段更改为“Runbook 输入”,然后选择“ResourceGroupName” 。Change the Data source field for ResourceGroupName to Runbook input and then select ResourceGroupName.
    Start-AzVM 参数Start-AzVM Parameters

  8. 保存 Runbook 并打开“测试”窗格。Save the runbook and open the Test pane. 现在可以为在测试中使用的两个输入变量提供值。You can now provide values for the two input variables that you use in the test.

  9. 关闭“测试”窗格。Close the Test pane.

  10. 单击“发布”以发布 Runbook 的新版本 。Click Publish to publish the new version of the runbook.

  11. 停止前面启动的 VM。Stop the VM that you started previously.

  12. 单击“启动”以启动 Runbook ResourceGroupNameClick Start to start the runbook. 键入要启动的 VM 的 VMNameResourceGroupName 值。Type in the values for VMName and ResourceGroupName for the VM that you're going to start.

  13. Runbook 完成后,请确保该 VM 已启动。When the runbook completes, ensure that the VM has been started.

现在可以修改该 Runbook,使之仅在 VM 尚未启动的情况下尝试启动该 VM。You can now modify the runbook so that it only attempts to start the VM if it is not already started. 为此,可以添加一个 Get-AzVM cmdlet 来检索 VM 的实例级状态。Do this by adding a Get-AzVM cmdlet that retrieves the instance-level status of the VM. 然后,可以添加名为 Get Status 的 PowerShell 工作流代码模块,并使用 PowerShell 代码片段来确定 VM 状态是“正在运行”还是“已停止”。Then you can add a PowerShell Workflow code module called Get Status with a snippet of PowerShell code to determine if the VM state is running or stopped. 仅当当前运行状态为“已停止”时,Get Status 模块中的条件链接才运行 Start-AzVMA conditional link from the Get Status module only runs Start-AzVM if the current running state is stopped. 在此过程的最后,Runbook 将使用 Write-Output cmdlet 输出一条消息,告知 VM 是否已成功启动。At the end of this procedure, your runbook uses the Write-Output cmdlet to output a message to inform you if the VM was successfully started.

  1. 在图形编辑器中打开“MyFirstRunbook-Graphical”。Open MyFirstRunbook-Graphical in the graphical editor.

  2. 单击“Specify Subscription Id”和“Start-AzVM”之间的链接,然后按“删除”将其删除。Remove the link between Specify Subscription Id and Start-AzVM by clicking on it and then pressing Delete.

  3. 在“库”控件的搜索字段中,键入 Get-AzIn the Library control, type Get-Az in the search field.

  4. Get-AzVM 添加到画布。Add Get-AzVM to the canvas.

  5. 选择 Get-AzVM,然后单击“参数集”以查看 cmdlet 集。Select Get-AzVM and then click Parameter Set to view the sets for the cmdlet.

  6. GetVirtualMachineInResourceGroupNameParamSet 参数集。Select the GetVirtualMachineInResourceGroupNameParamSet parameter set. “ResourceGroupName”和“Name”字段旁边出现了感叹号,表示它们指定了必需的参数。The ResourceGroupName and Name fields have exclamation marks next to them, indicating that they specify required parameters. 请注意,这两个字段都需要字符串值。Note that both fields expect string values.

  7. 在“Name”的“数据源”下选择“Runbook 输入”,然后选择“VMName” 。Under Data source for Name, select Runbook input, then VMName. 单击“确定”。Click OK.

  8. 在“ResourceGroupName”的“数据源”下选择“Runbook 输入”,然后选择“ResourceGroupName” 。Under Data source for ResourceGroupName, select Runbook input, then ResourceGroupName. 单击“确定”。Click OK.

  9. 在“Status”的“数据源”下选择“常量值”,然后选择“True” 。Under Data source for Status, select Constant value, then True. 单击“确定”。Click OK.

  10. 创建从 Specify Subscription IdGet-AzVM 的链接。Create a link from Specify Subscription Id to Get-AzVM.

  11. 在“库”控件中,展开“Runbook 控件”,然后将“Code”添加到画布 。In the Library control, expand Runbook Control and add Code to the canvas.

  12. 创建从 Get-AzVMCode 的链接。Create a link from Get-AzVM to Code.

  13. 单击 Code,然后在“配置”窗格中将标签更改为“获取状态”。Click Code and, in the Configuration pane, change the label to Get Status.

  14. 选择 Code,此时会显示“代码编辑器”页。Select Code and the Code Editor page appears.

  15. 将以下代码片段粘贴到编辑器页中。Paste the following code snippet into the editor page.

     $StatusesJson = $ActivityOutput['Get-AzVM'].StatusesText
     $Statuses = ConvertFrom-Json $StatusesJson
     $StatusOut =""
     foreach ($Status in $Statuses){
     if($Status.Code -eq "Powerstate/running"){$StatusOut = "running"}
     elseif ($Status.Code -eq "Powerstate/deallocated") {$StatusOut = "stopped"}
     }
     $StatusOut
    
  16. 创建从 Get StatusStart-AzVM 的链接。Create a link from Get Status to Start-AzVM.

    使用代码模块的 Runbook

  17. 选择该链接,然后在“配置”窗格中将“应用条件”更改为“是” 。Select the link and, in the Configuration pane, change Apply condition to Yes. 请注意,该链接将变为虚线,表示仅在条件解析为 True 时才会运行目标活动。Note that the link becomes a dashed line, indicating that the target activity only runs if the condition resolves to true.

  18. 对于“条件表达式”,请键入 $ActivityOutput['Get Status'] -eq "Stopped"For Condition expression, type $ActivityOutput['Get Status'] -eq "Stopped". 现在,仅当 VM 已停止时,Start-AzVM 才会运行。Start-AzVM now only runs if the VM is stopped.

  19. 在库控件中,展开 Cmdlet 节点,并展开 Microsoft.PowerShell.UtilityIn the Library control, expand Cmdlets and then Microsoft.PowerShell.Utility.

  20. Write-Output 添加到画布两次。Add Write-Output to the canvas twice.

  21. 对于第一个 Write-Output 控件,请单击“参数”并将“标签”值更改为“通知 VM 已启动” 。For the first Write-Output control, click Parameters and change the Label value to Notify VM Started.

  22. 对于“InputObject”,请将“数据源”更改为“PowerShell 表达式”,并键入表达式 $VMName successfully started.For InputObject, change Data source to PowerShell expression, and type in the expression $VMName successfully started..

  23. 在第二个 Write-Output 控件中,单击“参数”并将“标签”值更改为“通知 VM 启动失败”。On the second Write-Output control, click Parameters and change the Label value to Notify VM Start Failed.

  24. 对于“InputObject”,请将“数据源”更改为“PowerShell 表达式”,并键入表达式 $VMName could not startFor InputObject, change Data source to PowerShell expression, and type in the expression $VMName could not start.

  25. 创建从 Start-AzVMNotify VM StartedNotify VM Start Failed 的链接。Create links from Start-AzVM to Notify VM Started and Notify VM Start Failed.

  26. 选择“Notify VM Started”的链接并将“应用条件”更改为 true。Select the link to Notify VM Started and change Apply condition to true.

  27. 对于“条件表达式”,请键入 $ActivityOutput['Start-AzVM'].IsSuccessStatusCode -eq $trueFor the Condition expression, type $ActivityOutput['Start-AzVM'].IsSuccessStatusCode -eq $true. 现在,只有在 VM 已成功启动的情况下,此 Write-Output 控件才会运行。This Write-Output control now only runs if the VM starts successfully.

  28. 选择“Notify VM Start Failed”的链接并将“应用条件”更改为 true。Select the link to Notify VM Start Failed and change Apply condition to true.

  29. 对于“条件表达式”字段,请键入 $ActivityOutput['Start-AzVM'].IsSuccessStatusCode -ne $trueFor the Condition expression field, type $ActivityOutput['Start-AzVM'].IsSuccessStatusCode -ne $true. 现在,只有在 VM 未成功启动的情况下,此 Write-Output 控件才会运行。This Write-Output control now only runs if the VM is not successfully started. Runbook 看起来应如下图所示。Your runbook should look like the following image.

    使用 Write-Output 的 Runbook

  30. 保存 Runbook 并打开“测试”窗格。Save the runbook and open the Test pane.

  31. 在 VM 已停止的情况下启动该 Runbook,此时该计算机应会启动。Start the runbook with the VM stopped, and the machine should start.

后续步骤Next steps