Azure 自动化中的变量资产Variable assets in Azure Automation

变量资产是可供自动化帐户中的所有 Runbook 和 DSC 配置使用的值。Variable assets are values that are available to all runbooks and DSC configurations in your automation account. 可以通过 Azure 门户、PowerShell、Runbook 或 DSC 配置管理这些资产。They can be managed from the Azure portal, PowerShell, within a runbook, or DSC configuration. 自动化变量可用于以下方案:Automation variables are useful for the following scenarios:

  • 在多个 Runbook 或 DSC 配置之间共享某个值。Share a value between multiple runbooks or DSC configurations.

  • 在同一 Runbook 或 DSC 配置中的多个作业之间共享某个值。Share a value between multiple jobs from the same runbook or DSC configuration.

  • 从门户或 PowerShell 命令行管理由 Runbook 或 DSC 配置使用的值,例如一组常用配置项,如特定的 VM 名称列表、特定资源组、AD 域名,等等。Manage a value from the portal or from the PowerShell command line that is used by runbooks or DSC configurations, such as a set of common configuration items like specific list of VM names, a specific resource group, an AD domain name, and more.

由于自动化变量将会持久保存,即使 Runbook 或 DSC 配置失败,它们也仍然可用。Since Automation variables are persisted, they are available even if the runbook or DSC configuration fails. 此行为允许一个 Runbook 设置的值随后由另一个 Runbook 使用,或由同一 Runbook 或 DSC 配置在下次运行时使用。This behavior allows a value to be set by one runbook that is then used by another, or is used by the same runbook or DSC configuration the next time that it's run.

创建变量时,可以指定将其加密存储。When a variable is created, you can specify that it is stored encrypted. 加密的变量安全地存储在 Azure 自动化中并且不能从 Azure PowerShell 模块随附的 Get-AzureRmAutomationVariable cmdlet 检索变量值。Encrypted variables are stored securely in Azure Automation, and its value can't be retrieved from the Get-AzureRmAutomationVariable cmdlet that ships as part of the Azure PowerShell module. 可以检索加密值的唯一方法是从 Runbook 或 DSC 配置中的 Get-AutomationVariable 活动进行检索。The only way that an encrypted value can be retrieved is from the Get-AutomationVariable activity in a runbook or DSC configuration. 若要将已加密的变量更改为未加密,必须删除该变量并将其重新创建为未加密的变量。If you want to change an encrypted variable to un-encrypted, you must delete and re-create the variable as un-encrypted.


Azure 自动化中的安全资产包括凭据、证书、连接和加密的变量。Secure assets in Azure Automation include credentials, certificates, connections, and encrypted variables. 这些资产已使用针对每个自动化帐户生成的唯一密钥加密并存储在 Azure 自动化中。These assets are encrypted and stored in Azure Automation using a unique key that is generated for each automation account. 此密钥存储在系统托管的密钥保管库中。This key is stored in a system managed Key Vault. 在存储安全资产之前,从密钥保管库加载密钥,然后使用该密钥加密资产。Before storing a secure asset, the key is loaded from Key Vault and then used to encrypt the asset. 此过程由 Azure 自动化管理。This process is managed by Azure Automation.

变量类型Variable types

当使用 Azure 门户创建变量时,必须通过下拉列表指定一个数据类型,以便门户可以显示用于输入变量值的相应控件。When you create a variable with the Azure portal, you must specify a data type from the drop-down list so the portal can display the appropriate control for entering the variable value. 该变量并不局限于此数据类型。The variable isn't restricted to this data type. 如果想要指定不同类型的值,则必须使用 Windows PowerShell 设置该变量。You must set the variable using Windows PowerShell if you want to specify a value of a different type. 如果指定为“未定义” ,则该变量的值将设置为 $null,并且必须使用 Set-AzureRMAutomationVariable cmdlet 或 Set-AutomationVariable 活动来设置该值。If you specify Not defined, then the value of the variable sets to $null, and you must set the value with the Set-AzureRMAutomationVariable cmdlet or Set-AutomationVariable activity. 无法在该门户中创建或更改复杂变量类型的值,但可以使用 Windows PowerShell 提供任何类型的值。You can't create or change the value for a complex variable type in the portal, but you can provide a value of any type using Windows PowerShell. 复杂类型将作为 PSCustomObject返回。Complex types are returned as a PSCustomObject.

可以通过创建一个数组或哈希表并将其保存到变量,来将多个值存储到单一变量。You can store multiple values to a single variable by creating an array or hashtable and saving it to the variable.

下面列出了自动化中的可用变量类型:The following are a list of variable types available in Automation:

  • StringString
  • IntegerInteger
  • DateTimeDateTime
  • BooleanBoolean
  • NullNull

AzureRM PowerShell cmdletAzureRM PowerShell cmdlets

对于 AzureRM,下表中的 cmdlet 用于通过 Windows PowerShell 创建和管理自动化凭据资产。For AzureRM, the cmdlets in the following table are used to create and manage automation credential assets with Windows PowerShell. 可在自动化 Runbook 和 DSC 配置中使用的 AzureRM.Automation 模块已随附了这些 cmdlet。They ship as part of the AzureRM.Automation module, which is available for use in Automation runbooks and DSC configurations.

CmdletCmdlets 说明Description
Get-AzureRmAutomationVariableGet-AzureRmAutomationVariable 检索现有变量的值。Retrieves the value of an existing variable.
New-AzureRmAutomationVariableNew-AzureRmAutomationVariable 创建新变量并设置变量值。Creates a new variable and sets its value.
Remove-AzureRmAutomationVariableRemove-AzureRmAutomationVariable 删除现有变量。Removes an existing variable.
Set-AzureRmAutomationVariableSet-AzureRmAutomationVariable 设置现有变量的值。Sets the value for an existing variable.


下表中的活动用于在 Runbook 和 DSC 配置中访问变量。The activities in the following table are used to access variables in a runbook and DSC configurations. 本文档开头阐明了 Get-AzureRmAutomationVariable cmdlet 和 Get-AutomationVariable cmdlet 之间的区别。The difference between Get-AzureRmAutomationVariable and Get-AutomationVariable cmdlets is clarified above at the start of this document.

活动Activities 说明Description
Get-AutomationVariableGet-AutomationVariable 检索现有变量的值。Retrieves the value of an existing variable.
Set-AutomationVariableSet-AutomationVariable 设置现有变量的值。Sets the value for an existing variable.


应避免在 Runbook 或 DSC 配置中的 Get-AutomationVariable 的 –Name 参数中使用变量,因为这可能会使设计时发现 Runbook 或 DSC 配置与自动化变量之间的依赖关系变得复杂化。You should avoid using variables in the –Name parameter of Get-AutomationVariable in a runbook or DSC configuration since this can complicate discovering dependencies between runbooks or DSC configuration, and Automation variables at design time.

下表中的函数用于在 Python2 Runbook 中访问和检索变量。The functions in the following table are used to access and retrieve variables in a Python2 runbook.

Python2 函数Python2 Functions 说明Description
automationassets.get_automation_variableautomationassets.get_automation_variable 检索现有变量的值。Retrieves the value of an existing variable.
automationassets.set_automation_variableautomationassets.set_automation_variable 设置现有变量的值。Sets the value for an existing variable.


必须在 Python Runbook 顶部导入“automationassets”模块才能访问资产函数。You must import the "automationassets" module at the top of your Python runbook in order to access the asset functions.

创建新的自动化变量Creating a new Automation variable

使用 Azure 门户创建新变量To create a new variable with the Azure portal

  1. 在自动化帐户中,单击“资产” 磁贴,然后在“资产” 边栏选项卡中选择“变量” 。From your Automation account, click the Assets tile and then on the Assets blade, select Variables.
  2. 在“变量” 磁贴中,选择“添加变量” 。On the Variables tile, select Add a variable.
  3. 完成“新建变量” 边栏选项卡上的选项,然后单击“创建” 保存新变量。Complete the options on the New Variable blade and click Create save the new variable.

使用 Windows PowerShell 创建新变量To create a new variable with Windows PowerShell

New-AzureRmAutomationVariable cmdlet 创建一个新的变量并设置其初始值。The New-AzureRmAutomationVariable cmdlet creates a new variable and sets its initial value. 可以使用 Get-AzureRmAutomationVariable 检索该值。You can retrieve the value using Get-AzureRmAutomationVariable. 如果该值为简单类型,则返回相同的类型。If the value is a simple type, then that same type is returned. 如果其为复杂类型,则返回 PSCustomObjectIf it’s a complex type, then a PSCustomObject is returned.

下面的示例命令演示如何创建字符串类型的变量,并返回其值。The following sample commands show how to create a variable of type string and then return its value.

New-AzureRmAutomationVariable -ResourceGroupName "ResourceGroup01" 
–AutomationAccountName "MyAutomationAccount" –Name 'MyStringVariable' `
–Encrypted $false –Value 'My String'
$string = (Get-AzureRmAutomationVariable -ResourceGroupName "ResourceGroup01" `
–AutomationAccountName "MyAutomationAccount" –Name 'MyStringVariable').Value

下面的示例命令演示如何创建复杂类型的变量,并返回其属性。The following sample commands show how to create a variable with a complex type and then return its properties. 在这种情况下,会使用来自 Get-AzureRmVm 的虚拟机对象。In this case, a virtual machine object from Get-AzureRmVm is used.

$vm = Get-AzureRmVm -ResourceGroupName "ResourceGroup01" –Name "VM01"
New-AzureRmAutomationVariable –AutomationAccountName "MyAutomationAccount" –Name "MyComplexVariable" –Encrypted $false –Value $vm

$vmValue = (Get-AzureRmAutomationVariable -ResourceGroupName "ResourceGroup01" `
–AutomationAccountName "MyAutomationAccount" –Name "MyComplexVariable").Value
$vmName = $vmValue.Name
$vmIpAddress = $vmValue.IpAddress

使用 Runbook 或 DSC 配置中的变量Using a variable in a runbook or DSC configuration

使用 Set-AutomationVariable 活动设置 PowerShell Runbook 或 DSC 配置中自动化变量的值,并使用 Get-AutomationVariable 来检索该值。Use the Set-AutomationVariable activity to set the value of an Automation variable in a PowerShell runbook or DSC configuration, and the Get-AutomationVariable to retrieve it. 不应在 Runbook 或 DSC 配置中使用 Set-AzureRMAutomationVariable 或 Get-AzureRMAutomationVariable cmdlet,因为它们的效率低于工作流活动。You shouldn't use the Set-AzureRMAutomationVariable or Get-AzureRMAutomationVariable cmdlets in a runbook or DSC configuration since they are less efficient than the workflow activities. 也不能使用 Get-AzureRMAutomationVariable 来检索安全变量的值。You also cannot retrieve the value of secure variables with Get-AzureRMAutomationVariable. 从 Runbook 或 DSC 配置中创建新变量的唯一方法是使用 New-AzureRMAutomationVariable cmdlet。The only way to create a new variable from within a runbook or DSC configuration is to use the New-AzureRMAutomationVariable cmdlet.

文本 Runbook 示例Textual runbook samples

设置和检索变量中的一个简单值Setting and retrieving a simple value from a variable

下面的示例命令演示如何设置和检索文本 Runbook 中的变量。The following sample commands show how to set and retrieve a variable in a textual runbook. 在此示例中,假定已创建名为 NumberOfIterationsNumberOfRunnings 的整数类型变量以及名为 SampleMessage 的字符串类型变量。In this sample, it is assumed that variables of type integer named NumberOfIterations and NumberOfRunnings and a variable of type string named SampleMessage have been created.

$NumberOfIterations = Get-AzureRmAutomationVariable -ResourceGroupName "ResourceGroup01" –AutomationAccountName "MyAutomationAccount" -Name 'NumberOfIterations'
$NumberOfRunnings = Get-AzureRmAutomationVariable -ResourceGroupName "ResourceGroup01" –AutomationAccountName "MyAutomationAccount" -Name 'NumberOfRunnings'
$SampleMessage = Get-AutomationVariable -Name 'SampleMessage'

Write-Output "Runbook has been run $NumberOfRunnings times."

for ($i = 1; $i -le $NumberOfIterations; $i++) {
    Write-Output "$i`: $SampleMessage"
Set-AzureRmAutomationVariable -ResourceGroupName "ResourceGroup01" –AutomationAccountName "MyAutomationAccount" –Name NumberOfRunnings –Value ($NumberOfRunnings += 1)

在 Python2 中设置和检索变量Setting and retrieving a variable in Python2

以下代码示例演示了如何在 Python2 Runbook 中使用变量、设置变量以及处理关于不存在的变量的异常。The following sample code shows how to use a variable, set a variable, and handle an exception for a non-existent variable in a Python2 runbook.

import automationassets
from automationassets import AutomationAssetNotFound

# get a variable
value = automationassets.get_automation_variable("test-variable")
print value

# set a variable (value can be int/bool/string)
automationassets.set_automation_variable("test-variable", True)
automationassets.set_automation_variable("test-variable", 4)
automationassets.set_automation_variable("test-variable", "test-string")

# handle a non-existent variable exception
    value = automationassets.get_automation_variable("non-existing variable")
except AutomationAssetNotFound:
    print "variable not found"

图形 Runbook 示例Graphical runbook samples

在图形 Runbook 中,通过在图形编辑器的“库”窗格中右键单击变量并选择所需的活动来添加 Get-AutomationVariableSet-AutomationVariableIn a graphical runbook, you add the Get-AutomationVariable or Set-AutomationVariable by right-clicking on the variable in the Library pane of the graphical editor and selecting the activity you want.


设置变量中的值Setting values in a variable

下图显示了在图形 Runbook 中用于更新具有简单值的一个变量的示例活动。The following image shows sample activities to update a variable with a simple value in a graphical runbook. 在此示例中,Get-AzureRmVM 检索一个 Azure 虚拟机并将计算机名称保存到一个字符串类型的现有自动化变量。In this sample, Get-AzureRmVM retrieves a single Azure virtual machine and the computer name saves to an existing Automation variable with a type of String. 链接是管道还是序列并不重要,因为你仅预期输出中的单个对象。It doesn't matter whether the link is a pipeline or sequence since you only expect a single object in the output.


后续步骤Next Steps