在 Azure 自动化中编辑文本 runbookEdit textual runbooks in Azure Automation

可以使用 Azure 自动化中的文本编辑器来编辑 PowerShell runbookPowerShell 工作流 runbookYou can use the textual editor in Azure Automation to edit PowerShell runbooks and PowerShell Workflow runbooks. 此编辑器具有其他代码编辑器(如 IntelliSense)的典型功能。This editor has the typical features of other code editors, such as IntelliSense. 它还使用带有其他特殊功能的颜色编码来帮助你访问 runbook 中常用的资源。It also uses color coding with additional special features to assist you in accessing resources common to runbooks.

该文本编辑器包含的一项功能是将 cmdlet、资产和子 Runbook 的代码插入 Runbook 中。The textual editor includes a feature to insert code for cmdlets, assets, and child runbooks into a runbook. 不需要亲自键入代码,只需从可用资源列表中进行选择,编辑器就会将相应代码插入 runbook 中。Instead of typing in the code yourself, you can select from a list of available resources and the editor inserts the appropriate code into the runbook.

Azure 自动化中的每个 Runbook 都有两个版本:草稿版和已发布版。Each runbook in Azure Automation has two versions, Draft and Published. 先对 Runbook 的草稿版进行编辑,然后将其发布,这样便可以执行了。You edit the Draft version of the runbook and then publish it so it can be executed. 无法编辑已发布版本。The Published version cannot be edited. 有关详细信息,请参阅发布 runbookFor more information, see Publish a runbook.

本文提供了使用该编辑器执行不同功能的详细步骤。This article provides detailed steps for performing different functions with this editor. 它们不适用于图形 runbookThese are not applicable to graphical runbooks. 若要使用这些 runbook,请参阅 Azure 自动化中的图形创作To work with these runbooks, see Graphical authoring in Azure Automation.

使用 Azure 门户编辑 runbookEdit a runbook with the Azure portal

  1. 在 Azure 门户中,选择自动化帐户。In the Azure portal, select your Automation account.
  2. 在“流程自动化”下选择“Runbook”,打开 Runbook 的列表。 Under PROCESS AUTOMATION, select Runbooks to open the list of runbooks.
  3. 选择要编辑的 runbook,然后单击“编辑”。Choose the runbook to edit and then click Edit.
  4. 编辑 runbook。Edit the runbook.
  5. 完成编辑后,单击“保存”。Click Save when your edits are complete.
  6. 若要发布最新的 runbook 草稿版,请单击“发布”。Click Publish if you want to publish the latest draft version of the runbook.

将 cmdlet 插入 runbookInsert a cmdlet into a runbook

  1. 在文本编辑器的“画布”中,将光标置于要放置该 cmdlet 的地方。In the canvas of the textual editor, position the cursor where you want to place the cmdlet.
  2. 展开“库”控件中的“Cmdlet”节点。Expand the Cmdlets node in the Library control.
  3. 展开包含要使用的 cmdlet 的模块。Expand the module containing the cmdlet to use.
  4. 右键单击要插入的 cmdlet 名称,然后选择“添加到画布”。Right-click the cmdlet name to insert and select Add to canvas. 如果 cmdlet 具有多个参数集,编辑器将添加默认集。If the cmdlet has more than one parameter set, the editor adds the default set. 还可以展开 cmdlet 来选择不同的参数集。You can also expand the cmdlet to select a different parameter set.
  5. 请注意,该 cmdlet 的代码在插入时附带其所有参数。Note that the code for the cmdlet is inserted with its entire list of parameters.
  6. 对于任何必需的参数,请提供合适的值来替换尖括号 (<>) 中的值。Provide an appropriate value in place of the value surrounded by angle brackets (<>) for any required parameters. 请删除所有不需要的参数。Remove any parameters that you don't need.

将子 runbook 的代码插入到 runbook 中Insert code for a child runbook into a runbook

  1. 在文本编辑器的“画布”中,将光标置于要放置子 runbook 代码的位置。In the canvas of the textual editor, position the cursor where you want to place the code for the child runbook.
  2. 展开“库”控件中的“Runbook”节点。Expand the Runbooks node in the Library control.
  3. 右键单击要插入的 runbook,并选择“添加到画布”。Right-click the runbook to insert and select Add to canvas.
  4. 此时会插入带 Runbook 参数占位符的子 Runbook 的代码。The code for the child runbook is inserted with any placeholders for any runbook parameters.
  5. 将占位符替换为每个参数的相应值。Replace the placeholders with appropriate values for each parameter.

将资产插入 runbookInsert an asset into a runbook

  1. 在文本编辑器的“画布”控件中,将光标置于要放置子 runbook 代码的位置。In the Canvas control of the textual editor, position the cursor where you want to place the code for the child runbook.
  2. 展开“库”控件中的“资产”节点。Expand the Assets node in the Library control.
  3. 展开所需资产类型的节点。Expand the node for the desired asset type.
  4. 右键单击要插入的资产名称,并选择“添加到画布”。Right-click the asset name to insert and select Add to canvas. 对于变量资产,请选择“将‘获取变量’添加到画布”或“将‘设置变量’添加到画布”,具体取决于是要获取变量还是要设置变量 。For variable assets, select either Add "Get Variable" to canvas or Add "Set Variable" to canvas, depending on whether you want to get or set the variable.
  5. 请注意,此时会将资产的代码插入到 runbook 中。Note that the code for the asset is inserted into the runbook.

使用 Windows PowerShell 编辑 Azure 自动化 runbookEdit an Azure Automation runbook using Windows PowerShell

若要使用 Windows PowerShell 编辑 runbook,请使用所选编辑器进行操作,然后将其保存到 .ps1 文件。To edit a runbook with Windows PowerShell, use the editor of your choice and save the runbook to a .ps1 file. 可使用 Export-AzAutomationRunbook cmdlet 检索 runbook 的内容。You can use the Export-AzAutomationRunbook cmdlet to retrieve the contents of the runbook. 可使用 Import-AzAutomationRunbook cmdlet 将现有的草稿 runbook 替换为已修改的 runbook。You can use the Import-AzAutomationRunbook cmdlet to replace the existing draft runbook with the modified one.

使用 Windows PowerShell 检索 runbook 的内容Retrieve the contents of a runbook using Windows PowerShell

以下示例命令演示了如何检索 Runbook 的脚本并将其保存到脚本文件。The following sample commands show how to retrieve the script for a runbook and save it to a script file. 在此示例中,检索的是草稿版本。In this example, the Draft version is retrieved. 也可以检索 runbook 的已发布版本,不过不能更改此版本。It's also possible to retrieve the Published version of the runbook, although this version can't be changed.

$resourceGroupName = "MyResourceGroup"
$automationAccountName = "MyAutomationAccount"
$runbookName = "Hello-World"
$scriptFolder = "c:\runbooks"

Export-AzAutomationRunbook -Name $runbookName -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName -OutputFolder $scriptFolder -Slot Draft

使用 Windows PowerShell 更改 runbook 的内容Change the contents of a runbook using Windows PowerShell

以下示例命令演示了如何使用脚本文件的内容替换 Runbook 的现有内容。The following sample commands show how to replace the existing contents of a runbook with the contents of a script file.

$resourceGroupName = "MyResourceGroup"
$automationAccountName = "MyAutomationAccount"
$runbookName = "Hello-World"
$scriptFolder = "c:\runbooks"

Import-AzAutomationRunbook -Path "$scriptfolder\Hello-World.ps1" -Name $runbookName -Type PowerShell -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName -Force
Publish-AzAutomationRunbook -Name $runbookName -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroupName

后续步骤Next steps