我的第一个图形 RunbookMy first graphical runbook

本教程指导在 Azure 自动化中创建 图形 RunbookThis tutorial walks you through the creation of a graphical runbook in Azure Automation. 从一个简单的 Runbook 开始,目的是测试和发布该 Runbook,同时了解如何跟踪 Runbook 作业的状态。You start with a simple runbook that tests and publishes while learning how to track the status of the runbook job. 然后,通过修改 Runbook 来实际管理 Azure 资源,这种情况下会启动 Azure 虚拟机。Then you modify the runbook to actually manage Azure resources, in this case starting an Azure virtual machine. 然后添加 Runbook 参数和条件链接,使该 Runbook 更稳健,从而完成本教程。Then you complete the tutorial by making the runbook more robust by adding 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。You stop and start this machine so it should not be a production VM.

创建 RunbookCreate 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 that are automatically included in a new Automation account. 还应具有在“先决条件”中提到的凭证资产。You should also have the Credential asset that's mentioned in the prerequisites.

  2. 在“进程管理”下选择“Runbook”,打开 Runbook 的列表。 Select Runbooks under PROCESS MANAGEMENT to open the list of runbooks.

  3. 通过选择“+ 添加 Runbook”,并单击“创建新 Runbook”,创建一个新 Runbook 。Create a new runbook by selecting + Add a runbook, then click Create a new runbook.

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

  5. 在本示例中,需要创建图形 Runbook,因此应选择“图形”作为“Runbook 类型” 。In this case, you're going to create a graphical runbook so select Graphical for Runbook type.
    新建 RunbookNew runbook

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

添加活动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 textbox and type Write-Output. 搜索结果显示在下图中:The search results are shown in the following image:
    Microsoft.PowerShell.UtilityMicrosoft.PowerShell.Utility

  2. 向下滚动到列表的底部。Scroll down to the bottom of the list. 可以先右键单击“Write-Output” ,然后选择“添加到画布”,也可以先单击 cmdlet 旁边的省略号,然后选择“添加到画布” 。You can either right-click Write-Output and select Add to canvas, or click the ellipses next to the cmdlet 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 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 you one to use. 在本示例中,Write-Output 只有一个参数集,因此无需选择一个。In this case, Write-Output has only one parameter set, so you don't need to select one.

  6. 选择 InputObject 参数。Select the InputObject parameter. 这是一个可以在其中指定发送到输出流的文本的参数。This is the parameter where you specify the text to send to the output stream.

  7. 在“数据源”下拉列表中,选择“PowerShell 表达式” 。In the Data source dropdown, select PowerShell expression. “数据源”下拉列表中提供了用于填充参数值的不同的源 。The Data source dropdown provides different sources that you use to populate a parameter value.

    可以使用来自诸如另一个活动、自动化资产或 PowerShell 表达式等类型源的输出。You can use output from such sources such 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 box, type "Hello World" and then click OK twice to return to the canvas.

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

测试 RunbookTest the runbook

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

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

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

  3. 会创建一个 “Runbook 作业” 并且在窗格中显示其状态 。A runbook job is created and its status displayed in the pane.

    作业状态一开始为“排队”,表示正在等待云中的 Runbook 辅助角色变为可用状态 。The job status starts as Queued indicating that it is waiting for a runbook worker in the cloud to become available. 在某个辅助角色认领该作业后,该作业状态将变为“正在启动”,然后当 Runbook 实际开始运行时,该作业状态将变为“正在运行” 。It then moves to Starting when a worker claims the job, and then Running when the runbook actually starts running.

  4. Runbook 作业完成后,会显示其输出。When the runbook job completes, its output is displayed. 在这种情况下,会看到 Hello WorldIn this case, you see Hello World.
    Hello WorldHello World

  5. 关闭“测试”页返回到画布。Close the Test page to return to the canvas.

发布和启动 RunbookPublish and start the runbook

创建的 Runbook 仍处于“草稿”模式。The runbook that you 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,它会显示“已发布”的“创作状态” 。If you scroll left to view the runbook in the Runbooks page, it shows an Authoring Status of Published.

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

    顶部的选项允许我们启动 Runbook,计划其在将来的某个时刻启动,或创建 webhook 以使其可以通过 HTTP 调用启动。The options across the top allow us to start the runbook, schedule it to start at some time in the future, or create a webhook so it can be started through an HTTP call.

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

  5. 此时将打开已创建的 runbook 作业的作业页。A job page is opened for the runbook job that was created. 验证“作业状态”是否显示“已完成”。 Verify that the Job status shows Completed.

  6. 一旦此 Runbook 状态显示“已完成”,单击“输出” 。Once the runbook status shows Completed, click Output. 此时会打开“输出”页,可以在窗格中看到 Hello WorldThe Output page is opened, and you can see the Hello World in the pane.

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

  8. 单击“所有日志”打开 Runbook 作业的“流”页 。Click All Logs to open the Streams page for the runbook job. 应该只会在输出流中看到 Hello World,但此窗格也可以显示 Runbook 作业的其他流,例如,“详细”和“错误”(如果 Runbook 向其写入)。You should only see Hello World in the output stream, but this can show other streams for a runbook job such as Verbose and Error if the runbook writes to them.

  9. 关闭“所有日志”页和“作业”页,返回到“MyFirstRunbook-Graphical”页。Close the All Logs page and the Job page to return to the MyFirstRunbook-Graphical page.

  10. 若要查看 Runbook 的所有作业,请关闭“作业”页,并在“资源”下选择“作业”。 To view all the jobs for the runbook close the Job page and select Jobs under RESOURCES. 这会列出此 Runbook 创建的所有作业。This lists all the jobs created by this runbook. 由于只运行该作业一次,应该只会看到一个列出的作业。You should only see one job listed since you only ran the job once.

  11. 可以单击此作业,打开在启动 Runbook 时查看过的“作业”窗格。You can click this job to open the same Job pane that you viewed when you started the runbook. 这样便可以回溯并查看为特定 Runbook 创建的任何作业的详细信息。This allows you to go back in time and view the details of any job that was created for a particular runbook.

创建变量资产Create variable assets

已经测试并发布 Runbook,但到目前为止它不执行任何有用的操作。You've tested and published your runbook, but so far it doesn't do anything useful. 需要让其管理 Azure 资源。You want to have it manage Azure resources. 配置要进行身份验证的 Runbook 之前,先设置下面步骤 6 中要进行身份验证的活动,然后创建一个变量以保存订阅 ID 并引用它。Before you configure the runbook to authenticate, you create a variable to hold the subscription ID and reference it after you set up the activity to authenticate in step 6 below. 包括对订阅上下文的引用,可轻松地在多个订阅之间切换。Including a reference to the subscription context allows you to easily work between multiple subscriptions. 在继续操作之前,从“导航”窗格的“订阅”选项中复制订阅 ID。Before proceeding, copy your subscription ID from the Subscriptions option off the Navigation pane.

  1. 在“自动化帐户”页的“共享资源”下选择“变量”。 In the Automation Accounts page, select Variables under SHARED RESOURCES.
  2. 选择“添加变量” 。Select Add a variable.
  3. 在“新建变量”页中,在“名称”框中输入“AzureSubscriptionId”,在“值”框中输入订阅 ID 。In the New variable page, in the Name box, enter AzureSubscriptionId and in the Value box enter your Subscription ID. 对于“类型”,请保留“字符串”;对于“加密”,请保留默认值 。Keep string for the Type and the default value for Encryption.
  4. 单击“创建”以创建该变量 。Click Create to create the variable.

添加身份验证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 that are referred to in the prerequisites. 为此,可将 Azure 运行方式连接资产和 Connect-AzureRmAccount cmdlet 添加到画布中 。You do that by adding the Azure Run As connection Asset and Connect-AzureRmAccount cmdlet to the canvas.

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

  2. 由于不再需要将 Hello World 写入到输出,因此请单击省略号 (...),然后选择“删除” 。You don't need the Write Hello World to output anymore, so click the ellipses (...) and select Delete.

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

  4. 在“库”控件的搜索文本框中,键入“Add-AzureRmAccount” 。In the Library control, type Add-AzureRmAccount in the search textbox.

  5. Add-AzureRmAccount 添加到画布。Add Add-AzureRmAccount to the canvas.

  6. 将鼠标悬停在“获取运行方式连接”上方,直到在该形状的底部显示一个圆圈 。Hover over Get Run As Connection until a circle appears on the bottom of the shape. 单击该圆圈并将箭头拖至Add-AzureRmAccountClick the circle and drag the arrow to Add-AzureRmAccount. 创建的箭头是链接The arrow that you created is a link. 该 Runbook 一开始会运行“获取运行方式连接”,并运行 Add-AzureRmAccountThe runbook starts with Get Run As Connection and then run Add-AzureRmAccount.
    创建活动之间的链接Create link between activities

  7. 在画布上选择“Add-AzureRmAccount”,并在“配置控件”窗格的“标签”文本框中键入“登录到 Azure” 。On the canvas, select Add-AzureRmAccount and in the Configuration control pane type Login to Azure in the Label textbox.

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

  9. Add-AzureRmAccount 有多个参数集,因此需要选择其中一个,才能提供参数值。Add-AzureRmAccount has multiple parameter sets, so you need to select one before you can provide parameter values. 单击“参数集”,并选择“ServicePrincipalCertificate”参数集 。Click Parameter Set and then select the ServicePrincipalCertificate parameter set.

  10. 选择参数集后,这些参数会显示在“活动参数配置”页中。Once you select the parameter set, the parameters are displayed in the Activity Parameter Configuration page. 单击“APPLICATIONID” 。Click APPLICATIONID.
    添加 Azure RM 帐户参数Add Azure RM account parameters

  11. 在“参数值”页中,选择“活动输出”作为“数据源”,并从列表中选择“获取运行方式连接”,在“字段路径”文本框中键入“ApplicationId”,单击“确定” 。In the Parameter Value page, select Activity output for the Data source and select Get Run As Connection from the list, in the Field path textbox type ApplicationId, and then click OK. 需指定“字段路径”的属性的名称,因为活动所输出的对象包含多个属性。You are specifying the name of the property for the Field path because the activity outputs an object with multiple properties.

  12. 单击“CERTIFICATETHUMBPRINT”,并在“参数值”页中,选择“活动输出”作为“数据源” 。Click CERTIFICATETHUMBPRINT, and in the Parameter Value page, select Activity output for the Data source. 从列表中选择“获取运行方式连接”,在“字段路径”文本框中键入“CertificateThumbprint”,并单击“确定” 。Select Get Run As Connection from the list, in the Field path textbox type CertificateThumbprint, and then click OK.

  13. 单击“SERVICEPRINCIPAL”,在“参数值”页中选择“ConstantValue”作为“数据源”,单击选项“True”,并单击“确定” 。Click SERVICEPRINCIPAL, and in the Parameter Value page, select ConstantValue for the Data source, click the option True, and then click OK.

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

  15. 单击“TENANTID”,并在“参数值”页中,选择“活动输出”作为“数据源” 。Click TENANTID, and in the Parameter Value page, select Activity output for the Data source. 从列表中选择“获取运行方式连接”,在“字段路径”文本框中键入“TenantId”,并单击“确定”两次 。Select Get Run As Connection from the list, in the Field path textbox type TenantId, and then click OK twice.

  16. 在“库”控件的搜索文本框中,键入“Set-AzureRmContext” 。In the Library control, type Set-AzureRmContext in the search textbox.

  17. Set-AzureRmContext 添加到画布。Add Set-AzureRmContext to the canvas.

  18. 在画布上选择“Set-AzureRmContext”,并在“配置控件”窗格的“标签”文本框中键入“指定订阅 ID” 。On the canvas, select Set-AzureRmContext and in the Configuration control pane type Specify Subscription Id in the Label textbox.

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

  20. Set-AzureRmContext 有多个参数集,因此需要选择其中一个,才能提供参数值。Set-AzureRmContext has multiple parameter sets, so you need to select one before you can provide parameter values. 单击“参数集”,并选择“SubscriptionId”参数集 。Click Parameter Set and then select the SubscriptionId parameter set.

  21. 选择参数集后,这些参数会显示在“活动参数配置”页中。Once you select the parameter set, the parameters are displayed in the Activity Parameter Configuration page. 单击 SubscriptionIDClick SubscriptionID

  22. 在“参数值”页中,选择“ConstantValue”作为“数据源”,将 SubscriptionId 写入文本框,然后单击“确定”两次 。In the Parameter Value page, select ConstantValue for the Data source , write your SubscriptionId into the textbox and then click OK twice.

  23. 将鼠标悬停在“登录到 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 身份验证配置

添加用于启动 VM 的活动Add activity to start a VM

现在将添加 Start-AzureRmVM 活动来启动虚拟机。Here you add a Start-AzureRmVM activity to start a virtual machine. 可以在 Azure 订阅中选取任何虚拟机。现在,请将该名称硬编码到该 cmdlet 中。You can pick any virtual machine in your Azure subscription, and for now you hardcode that name into the cmdlet.

  1. 在“库”控件的搜索文本框中,键入“Start-AzureRm” 。In the Library control, type Start-AzureRm in the search textbox.
  2. Start-AzureRmVM 添加到画布,然后单击并将其拖放到“指定订阅 ID”下面 。Add Start-AzureRmVM 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-AzureRmVMClick the circle and drag the arrow to Start-AzureRmVM.
  4. Start-AzureRmVMSelect Start-AzureRmVM. 单击“参数”,并单击“参数集”查看 Start-AzureRmVM 的参数集 。Click Parameters and then Parameter Set to view the sets for Start-AzureRmVM. ResourceGroupNameParameterSetName 参数集。Select the ResourceGroupNameParameterSetName parameter set. ResourceGroupName 和“名称”旁边有感叹号 。ResourceGroupName and Name have exclamation points next them. 这表示它们是必需的参数。This indicates that they are required parameters. 另请注意,两者都需要字符串值。Also note both expect string values.
  5. 选择“名称” 。Select Name. 选择“PowerShell 表达式”作为“数据源”,然后键入带双引号的虚拟机名称(一开始用于此 Runbook) 。Select PowerShell expression for the Data source and type in the name of the virtual machine surrounded with double quotes that you start with this runbook. 单击 “确定”Click OK.
  6. 选择“ResourceGroupName” 。Select ResourceGroupName. 使用“PowerShell 表达式”作为“数据源”,并键入带双引号的资源组名称 。Use PowerShell expression for the Data source and type in the name of the resource group surrounded with double quotes. 单击 “确定”Click OK.
  7. 单击“测试”窗格,以便测试 Runbook。Click Test pane so that you can test the runbook.
  8. 单击“启动”以启动测试 。Click Start to start the test. 一旦测试完成后,检查已启动的虚拟机。Once it completes, check that the virtual machine was started.

此时,Runbook 看起来将如下所示:Your runbook should look like the following at this point:
Runbook 身份验证配置

添加其他输入参数Add additional input parameters

Runbook 目前在资源组中启动虚拟机,该虚拟机已在 Start-AzureRmVM cmdlet 中指定。Our runbook currently starts the virtual machine in the resource group that you specified in the Start-AzureRmVM cmdlet. 如果在 Runbook 启动时指定这二者,则该 Runbook 会更有用。The runbook would be more useful if we could specify both when the runbook is started. 现在请将输入参数添加到 Runbook,以提供该功能。You now add input parameters to the runbook to provide that functionality.

  1. 在“MyFirstRunbook-Graphical”窗格上单击“编辑”,打开图形编辑器 。Open the graphical editor by clicking Edit on the MyFirstRunbook-Graphical pane.
  2. 依次选择“输入和输出”和“添加输入”,打开“Runbook 输入参数”窗格 。Select Input and output and then Add input to open the Runbook Input Parameter pane.
  3. 指定 VMName 作为名称Specify VMName for the Name. 保留“字符串”作为“类型”,但将“必需”更改为“是” 。Keep string for the Type, but change Mandatory to Yes. 单击 “确定”Click OK.
  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-AzureRmVM 活动,并单击“参数” 。Select the Start-AzureRmVM activity and then click Parameters.
  6. 将“名称”的“数据源”更改为“Runbook 输入”,然后选择 VMNameChange the Data source for Name to Runbook input and then select VMName.
  7. 将“ResourceGroupName”的“数据源”更改为“Runbook 输入”,然后选择“ResourceGroupName” 。Change the Data source for ResourceGroupName to Runbook input and then select ResourceGroupName.
    Start-AzureVM 参数Start-AzureVM 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. 停止在上一步中启动的虚拟机。Stop the virtual machine that you started in the previous step.
  12. 单击“启动”以启动 Runbook ResourceGroupNameClick Start to start the runbook. 键入要启动的虚拟机的 VMNameResourceGroupNameType in the VMName and ResourceGroupName for the virtual machine that you're going to start.
  13. 一旦 Runbook 完成后,检查已启动的虚拟机。When the runbook completes, check that the virtual machine was started.

现在将修改该 Runbook,使之仅在虚拟机尚未启动的情况下尝试启动虚拟机。You now modify the runbook so that it only attempts to start the virtual machine if it is not already started. 为此,需要将 Get-AzureRmVM cmdlet 添加到 Runbook,以便获取虚拟机的实例级状态。You do this by adding a Get-AzureRmVM cmdlet to the runbook that gets the instance level status of the virtual machine. 然后,需要添加名为“获取状态”的 PowerShell 工作流代码模块,并使用 PowerShell 代码片段来确定虚拟机状态是“正在运行”还是“已停止” 。Then you add a PowerShell Workflow code module called Get Status with a snippet of PowerShell code to determine if the virtual machine state is running or stopped. 如果当前的运行状态为“已停止”,则“获取状态”模块中的条件链接只运行 Start-AzureRmVMA conditional link from the Get Status module only runs Start-AzureRmVM if the current running state is stopped. 最后,使用 PowerShell Write-Output cmdlet 输出一条消息,告知 VM 是否已启动成功。Finally, You output a message to inform you if the VM was successfully started or not using the PowerShell Write-Output cmdlet.

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

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

  3. 在“库”控件的搜索文本框中,键入“Get-AzureRm” 。In the Library control, type Get-AzureRm in the search textbox.

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

  5. 选择“Get-AzureRmVM”,并选择“参数集”以查看 Get-AzureRmVM 的参数集 。Select Get-AzureRmVM and then Parameter Set to view the sets for Get-AzureRmVM. GetVirtualMachineInResourceGroupNameParamSet 参数集。Select the GetVirtualMachineInResourceGroupNameParamSet parameter set. ResourceGroupName 和“名称”旁边有感叹号 。ResourceGroupName and Name have exclamation points next them. 这表示它们是必需的参数。This indicates that they are required parameters. 另请注意,两者都需要字符串值。Also note both expect string values.

  6. 在“名称”的“数据源”下,选择“Runbook 输入”,并选择 VMNameUnder Data source for Name, select Runbook input and then select VMName. 单击 “确定”Click OK.

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

  8. 在“状态”的“数据源”下,选择“常数值”,并单击“True” 。Under Data source for Status, select Constant value and then click True. 单击 “确定”Click OK.

  9. 创建从“指定订阅 ID” 到 Get-AzureRmVM 的链接。Create a link from Specify Subscription Id to Get-AzureRmVM.

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

  11. 创建从“Get-AzureRmVM”到“Code”的链接 。Create a link from Get-AzureRmVM to Code.

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

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

  14. 在代码编辑器中,粘贴以下代码片段:In the code editor, paste the following snippet of code:

     $StatusesJson = $ActivityOutput['Get-AzureRmVM'].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
    
  15. 创建从“获取状态”到“Start-AzureRmVM”的链接 。Create a link from Get Status to Start-AzureRmVM.
    使用代码模块的 RunbookRunbook with Code Module

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

  17. 对于“条件表达式”,请键入 $ActivityOutput['Get Status'] -eq "Stopped"For the Condition expression, type $ActivityOutput['Get Status'] -eq "Stopped". Start-AzureRmVM 现在仅在虚拟机停止的情况下才会运行。Start-AzureRmVM now only runs if the virtual machine is stopped.

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

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

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

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

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

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

  24. 创建从 Start-AzureRmVM 到“通知 VM 已启动”和“通知 VM 启动失败”的链接 。Create a link from Start-AzureRmVM to Notify VM Started and Notify VM Start Failed.

  25. 选择到“通知 VM 已启动”的链接,并将”应用条件“更改为 TrueSelect the link to Notify VM Started and change Apply condition to True.

  26. 对于“ 条件表达式”,键入 $ActivityOutput['Start-AzureRmVM'].IsSuccessStatusCode -eq $trueFor the Condition expression, type $ActivityOutput['Start-AzureRmVM'].IsSuccessStatusCode -eq $true. 此 Write-Output 控件现在将仅在虚拟机已成功启动的情况下运行。This Write-Output control now only runs if the virtual machine is successfully started.

  27. 选择到“通知 VM 启动失败”的链接,并将”应用条件“更改为 TrueSelect the link to Notify VM Start Failed and change Apply condition to True.

  28. 对于“ 条件表达式”,键入 $ActivityOutput['Start-AzureRmVM'].IsSuccessStatusCode -ne $trueFor the Condition expression, type $ActivityOutput['Start-AzureRmVM'].IsSuccessStatusCode -ne $true. 此 Write-Output 控件现在将仅在虚拟机未成功启动的情况下运行。This Write-Output control now only runs if the virtual machine is not successfully started. Runbook 应看起来如下图所示:Your runbook should look like the following image:
    使用 Write-Output 的 RunbookRunbook with Write-Output

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

  30. 如果在虚拟机停止时启动 Runbook,那么虚拟机将启动。Start the runbook with the virtual machine stopped, and it should start.

后续步骤Next steps