Runbook 输入参数Runbook input parameters

Runbook 输入参数允许在启动 Runbook 时向它传递数据,增加 Runbook 的灵活性。Runbook input parameters increase the flexibility of runbooks by allowing you to pass data to it when it's started. 这些参数可让 Runbook 操作以特定方案和环境为目标。The parameters allow the runbook actions to be targeted for specific scenarios and environments. 本文将介绍在 Runbook 中使用输入参数的不同方案。In this article, you walk through different scenarios where input parameters are used in runbooks.

配置输入参数Configure input parameters

可以在 PowerShell、PowerShell 工作流、Python 和图形 Runbook 中配置输入参数。Input parameters can be configured in PowerShell, PowerShell Workflow, Python, and graphical runbooks. 一个 Runbook 可以包含具有不同数据类型的多个参数,或者不包含任何参数。A runbook can have multiple parameters with different data types, or no parameters at all. 输入参数可以是必需的或可选的,可以为可选参数分配默认值。Input parameters can be mandatory or optional, and you can have a default value for optional parameters. 在通过某种可用方法启动 Runbook 时分配 Runbook 的输入参数值。You assign values to the input parameters for a runbook when you start it through one of the available methods. 这些方法包括从 Azure 门户、Web 服务或 PowerShell 启动 Runbook。These methods include starting a runbook from the Azure portal, a web service, or PowerShell. 还可以启动一个 Runbook 作为另一个 Runbook 中内联调用的子 Runbook。You can also start one as a child runbook that is called inline in another runbook.

在 PowerShell Runbook 中配置输入参数Configure input parameters in PowerShell runbooks

Azure 自动化中的 PowerShell 和 PowerShell 工作流 Runbook 支持通过以下属性定义的输入参数:PowerShell and PowerShell Workflow runbooks in Azure Automation support input parameters that are defined through the following attributes:

属性Property 说明Description
Type 必需。Required. 参数值所需的数据类型。The data type expected for the parameter value. 任何 .NET 类型均有效。Any .NET type is valid.
Name 必需。Required. 参数的名称。The name of the parameter. 在 Runbook 中必须唯一,并且只能包含字母、数字或下划线字符。This must be unique within the runbook, and can contain only letters, numbers, or underscore characters. 必须以字母开头。It must start with a letter.
Mandatory 可选。Optional. 指定是否必须为该参数提供值。Specifies whether a value must be provided for the parameter. 如果将此项设置为 $true,则启动 Runbook 时必须提供一个值。If you set this to $true, then a value must be provided when the runbook is started. 如果将此项设置为 $false,则值是可选的。If you set this to $false, then a value is optional.
Default value 可选。Optional. 指定在启动 Runbook 时未传入值的情况下要用于参数的值。Specifies a value that is used for the parameter if a value is not passed in when the runbook is started. 可为任何参数设置默认值,此值会使参数自动成为可选,而不管 Mandatory 设置为何。A default value can be set for any parameter and will automatically make the parameter optional regardless of the Mandatory setting.

Windows PowerShell 支持的输入参数属性比此处所列的多,例如验证、别名和参数集。Windows PowerShell supports more attributes of input parameters than those listed here, like validation, aliases, and parameter sets. 但是,Azure 自动化目前仅支持上述输入参数。However, Azure Automation currently supports only the preceding input parameters.

PowerShell 工作流 Runbook 中的参数定义采用以下常规格式,其中,多个参数必须以逗号分隔。A parameter definition in PowerShell Workflow runbooks has the following general form, where multiple parameters are separated by commas.

Param
(
  [Parameter (Mandatory= $true/$false)]
  [Type] $Name1 = <Default value>,

  [Parameter (Mandatory= $true/$false)]
  [Type] $Name2 = <Default value>
)

备注

定义参数时,如果未指定 Mandatory 属性,则会默认将参数视为可选。When you're defining parameters, if you don’t specify the Mandatory attribute, then by default, the parameter is considered optional. 此外,如果在 PowerShell 工作流 Runbook 中设置某个参数的默认值,则 PowerShell 会将其视为可选参数,而不考虑 Mandatory 属性的值。Also, if you set a default value for a parameter in PowerShell Workflow runbooks, it is treated by PowerShell as an optional parameter, regardless of the Mandatory attribute value.

例如,让我们为输出有关虚拟机(可以是单个 VM 或资源组中的所有 VM)的详细信息的 PowerShell 工作流 Runbook 配置输入参数。As an example, let’s configure the input parameters for a PowerShell Workflow runbook that outputs details about virtual machines, either a single VM or all VMs within a resource group. 如以下屏幕截图中所示,此 Runbook 有两个参数:虚拟机的名称和资源组的名称。This runbook has two parameters as shown in the following screenshot: the name of virtual machine and the name of the resource group.

自动化 PowerShell 工作流

在此参数定义中,$VMName 和 $resourceGroupName 参数是字符串类型的简单参数。In this parameter definition, the parameters $VMName and $resourceGroupName are simple parameters of type string. 但是,PowerShell 和 PowerShell 工作流 Runbook 支持所有简单类型和复杂类型,例如输入参数的 object 或 PSCredential。However, PowerShell and PowerShell Workflow runbooks support all simple types and complex types, such as object or PSCredential for input parameters.

如果 Runbook 有 object 类型输入参数,则使用包含 (name, value) 对的 PowerShell 哈希表来传入值。If your runbook has an object type input parameter, then use a PowerShell hashtable with (name, value) pairs to pass in a value. 例如,如果 Runbook 中有以下参数:For example, if you have the following parameter in a runbook:

[Parameter (Mandatory = $true)]
[object] $FullName

则可将以下值传递到该参数:Then you can pass the following value to the parameter:

@{"FirstName"="Joe";"MiddleName"="Bob";"LastName"="Smith"}

备注

如果没有将值传递到具有的默认值 \$null 的可选 [String] 类型参数,则参数的值将为空字符串,而不是 \$nullWhen you pass no value to an optional [String] type parameter that has a default value of \$null, then the parameter's value will be an empty string, not \$null.

在图形 Runbook 中配置输入参数Configure input parameters in graphical runbooks

为了使用输入参数配置图形 Runbook,让我们创建输出有关虚拟机(可以是单个 VM 或资源组中的所有 VM)的详细信息的图形 Runbook。To configure a graphical runbook with input parameters, let’s create a graphical runbook that outputs details about virtual machines, either a single VM or all VMs within a resource group. 配置 Runbook 包括两个主要活动,如下所述。Configuring a runbook consists of two major activities, as described below.

使用 Azure 运行方式帐户进行 Runbook 身份验证,以便与 Azure 进行身份验证。Authenticate Runbooks with Azure Run As account to authenticate with Azure.

通过 Get-AzureRmVm 获取虚拟机属性。Get-AzureRmVm to get the properties of a virtual machine.

可使用 Write-Output 活动输出虚拟机的名称。You can use the Write-Output activity to output the names of virtual machines. Get-AzureRmVm 活动接受两个参数:虚拟机名称资源组名称The activity Get-AzureRmVm accepts two parameters, the virtual machine name and the resource group name. 由于这些参数在每次启动 Runbook 时可能需要不同的值,因此可以将输入参数添加到 Runbook。Since these parameters could require different values each time you start the runbook, you can add input parameters to your runbook. 以下是添加输入参数的步骤:Here are the steps to add input parameters:

  1. 从“Runbook”边栏选项卡选择图形 Runbook,并单击“编辑”进行编辑。Select the graphical runbook from the Runbooks blade and then click Edit it.

  2. 在 Runbook 编辑器中,单击“输入和输出”,打开“输入和输出”边栏选项卡。From the runbook editor, click Input and output to open the Input and output blade.

    自动化图形 Runbook

  3. “输入和输出”边栏选项卡显示针对 Runbook 定义的输入参数列表。The Input and output blade displays a list of input parameters that are defined for the runbook. 在此边栏选项卡上,可以添加新的输入参数,或编辑现有输入参数的配置。On this blade, you can either add a new input parameter or edit the configuration of an existing input parameter. 若要为 Runbook 添加新参数,请单击“添加输入”,打开“Runbook 输入参数”边栏选项卡。To add a new parameter for the runbook, click Add input to open the Runbook input parameter blade. 在此处可以配置以下参数:There, you can configure the following parameters:

    属性Property 说明Description
    Name 必需。Required. 参数的名称。The name of the parameter. 在 Runbook 中必须唯一,并且只能包含字母、数字或下划线字符。This must be unique within the runbook, and can contain only letters, numbers, or underscore characters. 必须以字母开头。It must start with a letter.
    Description 可选。Optional. 有关输入参数用途的说明。Description about the purpose of input parameter.
    Type 可选。Optional. 参数值的预期数据类型。The data type that's expected for the parameter value. 支持的参数类型包括 StringInt32Int64DecimalBooleanDateTimeObjectSupported parameter types are String, Int32, Int64, Decimal, Boolean, DateTime, and Object. 如果未选择数据类型,将默认为 StringIf a data type is not selected, it defaults to String.
    Mandatory 可选。Optional. 指定是否必须为该参数提供值。Specifies whether a value must be provided for the parameter. 如果选择“是”,则启动 Runbook 时必须提供一个值。If you choose yes, then a value must be provided when the runbook is started. 如果选择“否”,则启动 Runbook 时不需要提供值,并且可以设置一个默认值。If you choose no, then a value is not required when the runbook is started, and a default value may be set.
    Default Value 可选。Optional. 指定在启动 Runbook 时未传入值的情况下要用于参数的值。Specifies a value that is used for the parameter if a value is not passed in when the runbook is started. 对于不是必需的参数,可以设置默认值。A default value can be set for a parameter that's not mandatory. 若要设置默认值,请选择“自定义”。To set a default value, choose Custom. 除非在 Runbook 启动时提供了其他值,否则将使用此值。This value is used unless another value is provided when the runbook is started. 如果不想提供任何默认值,请选择“无”。Choose None if you don’t want to provide any default value.

    添加新输入

  4. 使用以下属性,创建 Get-AzureRmVm 活动使用的两个参数:Create two parameters with the following properties that are used by the Get-AzureRmVm activity:

    • 参数 1:Parameter1:
      • 名称 - VMNameName - VMName
      • 类型 - StringType - String
      • 必需 - 否Mandatory - No
    • 参数 2:Parameter2:
      • 名称 - resourceGroupNameName - resourceGroupName
      • 类型 - StringType - String
      • 必需 - 否Mandatory - No
      • 默认值 - 自定义Default value - Custom
      • 自定义默认值 - <包含虚拟机的资源组的名称>Custom default value - <Name of the resource group that contains the virtual machines>
  5. 添加参数后,单击“确定”。Once you add the parameters, click OK. 现在,可在“输入和输出页”中查看这些参数。You can now view them in the Input and output page. 再次单击“确定”,并单击“保存”“发布”Runbook。Click OK again, and then click Save and Publish your runbook.

在 Python Runbook 中配置输入参数Configure input parameters in Python runbooks

与 PowerShell、PowerShell 工作流和图形 Runbook 不同,Python Runbook 不使用命名参数。Unlike PowerShell, PowerShell Workflow, and Graphical runbooks, Python runbooks do not take named parameters. 所有输入形参均解析为实参值数组。All input parameters are parsed as an array of argument values. 通过将 sys 模块导入 Python 脚本然后使用 sys.argv 数组来访问数组。You access the array by importing the sys module into your Python script, and then using the sys.argv array. 请务必注意,数组的第一个元素 sys.argv[0] 是脚本的名称,因此第一个输入参数实际上是 sys.argv[1]It is important to note that the first element of the array, sys.argv[0], is the name of the script, so the first actual input parameter is sys.argv[1].

若要获取如何在 Python Runbook 中使用输入参数的示例,请参阅我在 Azure 自动化中的第一个 Python RunbookFor an example of how to use input parameters in a Python runbook, see My first Python runbook in Azure Automation.

为 Runbook 中的输入参数赋值Assign values to input parameters in runbooks

在以下情况下,可以将值传递到 Runbook 中的输入参数:You can pass values to input parameters in runbooks in the following scenarios:

启动 Runbook 并分配参数Start a runbook and assign parameters

Runbook 有多种启动方式:通过 Azure 门户、Webhook、PowerShell cmdlet、REST API 或 SDK。A runbook can be started many ways: through the Azure portal, with a webhook, with PowerShell cmdlets, with the REST API, or with the SDK. 下面介绍了启动 Runbook 和分配参数的不同方法。Below we discuss different methods for starting a runbook and assigning parameters.

使用 Azure 门户启动已发布的 Runbook 并分配参数Start a published runbook by using the Azure portal and assign parameters

启动 Runbook 时,“启动 Runbook”边栏选项卡会打开,可以为所创建的参数输入值。When you start the runbook, the Start Runbook blade opens and you can enter values for the parameters that you created.

使用门户启动

在输入框下面的标签中,可以查看为参数设置的属性。In the label beneath the input box, you can see the attributes that have been set for the parameter. 属性包括必需或可选属性、类型和默认值。Attributes include mandatory or optional, type, and default value. 在参数名称旁边的帮助气球中,可以查看做出参数输入值相关决策时所需的所有关键信息。In the help balloon next to the parameter name, you can see all the key information you need to make decisions about parameter input values. 此信息包括参数是必需还是可选的。This information includes whether a parameter is mandatory or optional. 此外还包括类型和默认值(如果有)及其他有用的说明。It also includes the type and default value (if any), and other helpful notes.

备注

字符串类型参数支持 字符串值。String type parameters support Empty String values. 在输入参数框中输入 [EmptyString] 将向参数传递空字符串。Entering [EmptyString] in the input parameter box passes an empty string to the parameter. 另外,字符串类型参数不支持传递 Null 值。Also, String type parameters don’t support Null values being passed. 如果未向字符串参数传递任何值,PowerShell 会将值解释为 NULL。If you don’t pass any value to the String parameter, then PowerShell interprets it as null.

使用 PowerShell cmdlet 启动已发布的 Runbook 并分配参数Start a published runbook by using PowerShell cmdlets and assign parameters

  • Azure 资源管理器 cmdlet: 可以使用 Start-AzureRmAutomationRunbook 启动在资源组中创建的自动化 Runbook。Azure Resource Manager cmdlets: You can start an Automation runbook that was created in a resource group by using Start-AzureRmAutomationRunbook.

    示例:Example:

    $params = @{"VMName"="WSVMClassic";"resourceGroupeName"="WSVMClassicSG"}
    
    Start-AzureRmAutomationRunbook -AutomationAccountName "TestAutomation" -Name "Get-AzureVMGraphical" –ResourceGroupName $resourceGroupName -Parameters $params
    
  • Azure 经典部署模型 cmdlet: 可以使用 Start-AzureAutomationRunbook 启动在默认资源组中创建的自动化 Runbook。Azure classic deployment model cmdlets: You can start an automation runbook that was created in a default resource group by using Start-AzureAutomationRunbook.

    示例:Example:

    $params = @{"VMName"="WSVMClassic"; "ServiceName"="WSVMClassicSG"}
    
    Start-AzureAutomationRunbook -AutomationAccountName "TestAutomation" -Name "Get-AzureVMGraphical" -Parameters $params
    

备注

使用 PowerShell cmdlet 启动 Runbook 时,将创建值为 PowerShell 的默认参数 MicrosoftApplicationManagementStartedBy。When you start a runbook by using PowerShell cmdlets, a default parameter, MicrosoftApplicationManagementStartedBy is created with the value PowerShell. 可在“作业详细信息”页中查看此参数。You can view this parameter in the Job details page.

使用 SDK 启动 Runbook 并分配参数Start a runbook by using an SDK and assign parameters

  • Azure 资源管理器方法: 可以使用某种编程语言的 SDK 来启动 Runbook。Azure Resource Manager method: You can start a runbook by using the SDK of a programming language. 以下 C# 代码片段用于在自动化帐户中启动 Runbook。Below is a C# code snippet for starting a runbook in your Automation account. 可以在 GitHub 存储库中查看完整代码。You can view all the code at our GitHub repository.

    public Job StartRunbook(string runbookName, IDictionary<string, string> parameters = null)
        {
          var response = AutomationClient.Jobs.Create(resourceGroupName, automationAccount, new JobCreateParameters
           {
              Properties = new JobCreateProperties
               {
                  Runbook = new RunbookAssociationProperty
                   {
                     Name = runbookName
                   },
                     Parameters = parameters
               }
           });
        return response.Job;
        }
    
  • Azure 经典部署模型方法: 可以使用某种编程语言的 SDK 来启动 Runbook。Azure classic deployment model method: You can start a runbook by using the SDK of a programming language. 以下 C# 代码片段用于在自动化帐户中启动 Runbook。Below is a C# code snippet for starting a runbook in your Automation account. 可以在 GitHub 存储库中查看完整代码。You can view all the code at our GitHub repository.

    public Job StartRunbook(string runbookName, IDictionary<string, string> parameters = null)
      {
        var response = AutomationClient.Jobs.Create(automationAccount, new JobCreateParameters
      {
        Properties = new JobCreateProperties
           {
             Runbook = new RunbookAssociationProperty
           {
             Name = runbookName
                },
                  Parameters = parameters
                }
         });
        return response.Job;
      }
    

    若要启动此方法,请创建一个字典来存储 Runbook 参数(VMName 和 resourceGroupName)及其值。To start this method, create a dictionary to store the runbook parameters, VMName and resourceGroupName, and their values. 然后启动 Runbook。Then start the runbook. 以下 C# 代码片段用于调用上面定义的方法。Below is the C# code snippet for calling the method that's defined above.

    IDictionary<string, string> RunbookParameters = new Dictionary<string, string>();
    
    // Add parameters to the dictionary.
    RunbookParameters.Add("VMName", "WSVMClassic");
    RunbookParameters.Add("resourceGroupName", "WSSC1");
    
    //Call the StartRunbook method with parameters
    StartRunbook("Get-AzureVMGraphical", RunbookParameters);
    

使用 REST API 启动 Runbook 并分配参数Start a runbook by using the REST API and assign parameters

可在以下请求 URI 中使用 PUT 方法通过 Azure 自动化 REST API 来创建并启动 Runbook 作业:A runbook job can be created and started with the Azure Automation REST API by using the PUT method with the following request URI: https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/jobs/{jobName}?api-version=2017-05-15-preview

在请求 URI 中替换以下参数:In the request URI, replace the following parameters:

  • subscriptionID: Azure 订阅 ID。subscriptionId: Your Azure subscription ID.
  • ResourceGroupName: 自动化帐户的资源组的名称。resourceGroupName: The name of the resource group for the Automation Account.
  • automationAccountName: 托管在指定云服务中的自动化帐户的名称。automationAccountName: The name of your automation account that's hosted within the specified cloud service.
  • jobName: 作业的 GUID。jobName: The GUID for the job. 使用 [GUID]::NewGuid().ToString() 命令可以创建 PowerShell 中的 GUID。GUIDs in PowerShell can be created by using the [GUID]::NewGuid().ToString() command.

要将参数传递到 Runbook 作业,请使用请求正文。In order to pass parameters to the runbook job, use the request body. 它采用两个以 JSON 格式提供的属性:It takes the following two properties provided in JSON format:

  • Runbook 名称: 必需。Runbook name: Required. 作业要启动的 Runbook 的名称。The name of the runbook for the job to start.
  • Runbook 参数: 可选。Runbook parameters: Optional. 参数列表的字典;列表必须采用 (name, value) 格式,其中的名称应为字符串类型,值可以是任何有效的 JSON 值。A dictionary of the parameter list in (name, value) format where name should be of String type and value can be any valid JSON value.

若要启动之前以 VMName 和 resourceGroupName 作为参数创建的 Get-AzureVMTextual Runbook,请使用以下 JSON 格式的请求正文。If you want to start the Get-AzureVMTextual runbook that was created earlier with VMName and resourceGroupName as parameters, use the following JSON format for the request body.

 {
   "properties":{
     "runbook":{
     "name":"Get-AzureVMTextual"},
   "parameters":{
      "VMName":"WindowsVM",
      "resourceGroupName":"ContosoSales"}
     }
 }

如果成功创建了作业,将返回 HTTP 状态代码 201。An HTTP status code 201 is returned if the job is successfully created. 有关响应标头和响应正文的详细信息,请参阅有关如何使用 REST API 创建 Runbook 作业的文章。For more information on response headers and the response body, see the article about how to create a runbook job by using the REST API.

测试 Runbook 并分配参数Test a runbook and assign parameters

使用测试选项测试 Runbook 的草稿版本时,将打开“测试”页,可在其中为所创建的参数配置值。When you test the draft version of your runbook by using the test option, the Test page opens and you can configure values for the parameters that you created.

测试并分配参数

可以将计划链接到 Runbook,以便在特定的时间启动 Runbook。You can link a schedule to your runbook so that the runbook starts at a specific time. 创建计划时将指定输入参数,Runbook 在按计划启动时,将使用这些值。You assign input parameters when you create the schedule, and the runbook uses these values when it is started by the schedule. 只有在提供所有必需参数值之后,才可以保存计划。You can’t save the schedule until all mandatory parameter values are provided.

计划并分配参数

创建 Runbook 的 Webhook 并分配参数Create a webhook for a runbook and assign parameters

可为 Runbook 创建 Webhook 并配置 Runbook 输入参数。You can create a webhook for your runbook and configure runbook input parameters. 只有在提供所有必需参数值之后,才可以保存 Webhook。You can’t save the webhook until all mandatory parameter values are provided.

创建 webhook 并分配参数

使用 Webhook 执行 Runbook 时,会发送预定义的输入参数 Webhookdata 定义的输入参数。When you execute a runbook by using a webhook, the predefined input parameter Webhookdata is sent, along with the input parameters that you defined. 可单击 WebhookData 参数将它展开,以查看更多详细信息。You can click to expand the WebhookData parameter for more details.

WebhookData 参数

将 JSON 对象传递给 runbookPass a JSON object to a runbook

它可用于存储想要传递到 JSON 文件中的 Runbook 的数据。It can be useful to store data that you want to pass to a runbook in a JSON file. 例如,你可能会创建包含所有想要传递给 Runbook 的参数的 JSON 文件。For example, you might create a JSON file that contains all of the parameters you want to pass to a runbook. 为此,必须先将 JSON 转换为字符串,将字符串转换为 PowerShell 对象,然后再将其传递到 Runbook。To do this, you have to convert the JSON to a string and then convert the string to a PowerShell object before it is passed to the runbook.

在此示例中,将创建调用 Start-AzureRmAutomationRunbook 的 PowerShell 脚本来启动 PowerShell Runbook,将 JSON 的内容传递给 Runbook。In this example, you have a PowerShell script that calls Start-AzureRmAutomationRunbook to start a PowerShell runbook, passing the contents of the JSON to the runbook. PowerShell Runbook 会启动一个 Azure VM,从传入的 JSON 获取 VM 的参数。The PowerShell runbook starts an Azure VM, getting the parameters for the VM from the JSON that was passed in.

创建 JSON 文件Create the JSON file

在文本文件中键入以下测试,并在本地计算机上将其保存为 test.jsonType the following test in a text file, and save it as test.json somewhere on your local computer.

{
   "VmName" : "TestVM",
   "ResourceGroup" : "AzureAutomationTest"
}

创建 RunbookCreate the runbook

在 Azure 自动化中创建名为“Test-Json”的新 PowerShell Runbook。Create a new PowerShell runbook named "Test-Json" in Azure Automation. 若要了解如何创建新的 PowerShell Runbook,请参阅我的第一个 PowerShell RunbookTo learn how to create a new PowerShell runbook, see My first PowerShell runbook.

要接受 JSON 数据,Runbook 必须将一个对象作为输入参数。To accept the JSON data, the runbook must take an object as an input parameter.

然后,Runbook 可以使用 JSON 中定义的属性。The runbook can then use the properties defined in the JSON.

Param(
     [parameter(Mandatory=$true)]
     [object]$json
)

# Connect to Azure account
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -EnvironmentName AzureChinaCloud `
    -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

# Convert object to actual JSON
$json = $json | ConvertFrom-Json

# Use the values from the JSON object as the parameters for your command
Start-AzureRmVM -Name $json.VMName -ResourceGroupName $json.ResourceGroup

在自动化帐户中保存并发布此 Runbook。Save and publish this runbook in your Automation account.

通过 PowerShell 调用 RunbookCall the runbook from PowerShell

现在可以通过 Azure PowerShell 从本地计算机调用 Runbook。Now you can call the runbook from your local machine by using Azure PowerShell. 运行以下 PowerShell 命令:Run the following PowerShell commands:

  1. 登录 Azure:Sign in to Azure:

    Connect-AzureRmAccount -EnvironmentName AzureChinaCloud
    

    系统会提示输入 Azure 凭据。You're prompted to enter your Azure credentials.

    重要

    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.

  2. 获取 JSON 文件的内容并将其转换为字符串:Get the contents of the JSON file and convert it to a string:

    $json =  (Get-content -path 'JsonPath\test.json' -Raw) | Out-string
    

    JsonPath 是保存 JSON 文件的路径。is the path where you saved the JSON file.

  3. $json 的字符串内容转换为 PowerShell 对象:Convert the string contents of $json to a PowerShell object:

    $JsonParams = @{"json"=$json}
    
  4. Start-AzureRmAutomationRunbook 的参数创建哈希表:Create a hashtable for the parameters for Start-AzureRmAutomationRunbook:

    $RBParams = @{
         AutomationAccountName = 'AATest'
         ResourceGroupName = 'RGTest'
         Name = 'Test-Json'
         Parameters = $JsonParams
    }
    

    请注意,你正在将 Parameters 的值设置为包含 JSON 文件中的值的 PowerShell 对象。Notice that you're setting the value of Parameters to the PowerShell object that contains the values from the JSON file.

  5. 启动 RunbookStart the runbook

    $job = Start-AzureRmAutomationRunbook @RBParams
    

后续步骤Next steps