下表定义了 Azure 自动化流程自动化功能支持的几种 Runbook 类型。
| 类型 |
说明 |
PowerShell (推荐) |
基于 Windows PowerShell 脚本的文本运行手册。 当前支持的版本包括:PowerShell 5.1(GA)、PowerShell 7.1(预览版)和 PowerShell 7.2。 |
|
PowerShell 工作流 |
基于 Windows PowerShell 工作流脚本编写的文本运行簿。 |
Python (推荐) |
基于 Python 脚本编写的文本式运行手册。 当前支持的版本包括:Python 2.7(GA)和 Python 3.8 (GA),Python 3.10。 |
|
图形 |
基于 Windows PowerShell 的图形 Runbook,只能在 Azure 门户上的图形编辑器中创建和编辑。 |
|
图形 PowerShell 工作流 |
基于 Windows PowerShell 工作流并且完全在 Azure 门户中的图形编辑器中创建和编辑的图形运行手册。 |
注意
Azure 自动化将按照父产品、PowerShell 和 Python 发布的时间线分别遵循 PowerShell 和 Python 语言版本的支持生命周期。 建议使用支持语言版本的 Runbook。
在确定用于特定运行手册的类型时,请考虑以下因素:
- 无法将运行手册从图形类型转换为文本类型,也无法反向转换。
- 将不同类型的运行手册用作子运行手册时存在限制。 有关更多信息,请参阅 Azure 自动化中的子运行手册。
PowerShell 运行手册
PowerShell runbook 基于 Windows PowerShell。 可以在 Azure 门户中使用文本编辑器直接编辑 Runbook 的代码。 还可以使用任何离线文本编辑器,并将运行手册导入 Azure 自动化。
PowerShell 版本由指定的 运行时版本 确定。
同一 Azure 沙盒和混合 Runbook 辅助角色可以同时执行多个针对不同运行时版本的 PowerShell Runbook。
注意
- 在 Runbook 执行时,如果选择 运行时版本 为 7.4,则使用面向 7.4 运行时版本的 PowerShell 模块;如果选择 运行时版本 为 5.1,则使用面向 5.1 运行时版本的 PowerShell 模块。
确保为模块选择正确的运行时版本。
例如:如果要在 运行时版本7.4 中为 SharePoint 自动化方案执行 Runbook,请在 运行时版本7.4 中导入模块;如果要在 运行时版本5.1 中为 SharePoint 自动化方案执行 Runbook,请在 运行时版本5.1 中导入该模块。
优点
- 通过 PowerShell 代码来实现所有复杂的逻辑,没有 PowerShell 工作流的各种其他复杂操作。
- 与 PowerShell 工作流 Runbook 相比,它的启动速度更快,因为它们在运行前不需要经过编译。
- 可在 Azure 中运行,也可在适用于 Windows 和 Linux 的混合 Runbook Worker 上运行。
限制和已知问题
下面是 PowerShell Runbook 的当前限制和已知问题:
限制
注意
父产品 PowerShell 不再支持 PowerShell 7.2 版本。
- 对于 PowerShell 7.2 运行时版本,不会为导入的模块提取模块活动。
- PowerShell 7.x 不支持工作流。 有关详细信息,请参阅 PowerShell 工作流。
- PowerShell 7.x 目前不支持已签名的运行手册。
- 源代码管理集成不支持 PowerShell 7.2。 此外,源代码管理中的 PowerShell 7.2 runbook 还在自动化帐户中创建为运行时 5.1。
- 默认情况下安装 Az 模块 8.3.0。 使用 Azure 门户或 API 再次配置 Az 版本后,将显示所选 Az 模块版本的组件模块的完整列表。
- 导入的 PowerShell 7.2 模块将在作业执行期间进行验证。 请确保还导入了所选模块的所有依赖项以成功执行作业。
- Azure Runbook 不支持带有
Start-Job 的 -credential。
- Azure 不支持所有 PowerShell 输入参数。
了解详细信息。
已知问题
由于服务后端基础结构的更改,依赖于内部文件路径 (如 C:\modules) 的 Runbook 可能会失败。 更改 Runbook 代码以确保内部文件路径上没有依赖项,并使用 Get-ChildItem 来获取所需的模块信息。
Get-AzStorageAccount cmdlet 可能会失败并出现错误:在模块 Get-AzStorageAccount 中找到了 Az.Storage 命令,但无法加载该模块 。
不支持使用 .\child-runbook.ps1 执行子脚本。
解决方法:使用 Start-AutomationRunbook(内部 cmdlet)或 Start-AzAutomationRunbook(来自 Az.Automation 模块)从父 runbook 启动另一个 runbook。
使用 ExchangeOnlineManagement 模块版本 3.0.0 或更高版本时,可能会遇到错误。 若要解决此问题,请确保显式上传 PowerShellGet 和 PackageManagement 模块。
使用 Az.Automation 模块中的 New-AzAutomationVariable cmdlet 上传 object 类型的变量时,该操作不按预期运行。
解决方法:使用 ConvertTo-Json cmdlet 将该对象转换为 JSON 字符串,然后使用 JSON 字符串作为变量值来上传变量。 此解决方法可确保将 Azure 自动化环境中变量作为 JSON 字符串正确处理。
示例 - 创建一个 PowerShell 对象,用于存储有关 Azure VM 的信息
azurepowershell
# Retrieve Azure virtual machines with status information for the 'chinanorth3' region
$AzVM = Get-AzVM -Status | Where-Object {$_.Location -eq "chinanorth3"}
$VMstopatch = @($AzVM).Id
# Create an Azure Automation variable (This cmdlet will not fail, but the variable may not work as intended when used in the runbook.)
New-AzAutomationVariable -ResourceGroupName "mrg" -AutomationAccountName "mAutomationAccount2" -Name "complex1" -Encrypted $false -Value $VMstopatch
# Convert the object to a JSON string
$jsonString = $VMstopatch | ConvertTo-Json
# Create an Azure Automation variable with a JSON string value (works effectively within the automation runbook)
New-AzAutomationVariable -ResourceGroupName "mrg" -AutomationAccountName "mAutomationAccount2" -Name "complex1" -Encrypted $false -Value $jsonString
限制
- Runbook 无法使用并行处理并行执行多个操作。
- 出现错误时,Runbook 无法使用检查点恢复 Runbook。
- 你可以使用 Start-AzAutomationRunbook cmdlet(用于创建新作业),仅将 PowerShell、PowerShell 工作流 Runbook 和图形 Runbook 以子 Runbook 的形式包括在内。
- Runbook 不能使用 PowerShell #Requires 语句,因为 Azure 沙盒和混合 Runbook 辅助角色均不支持该语句,并且它可能导致作业失败。
- Azure Runbook 不支持带有
Start-Job 的 -credential。
- Azure 不支持所有 PowerShell 输入参数。
了解详细信息。
已知问题
由于服务后端基础结构的更改,依赖于内部文件路径 (如 C:\modules) 的 Runbook 可能会失败。 更改 Runbook 代码以确保内部文件路径上没有依赖项,并使用 Get-ChildItem 来获取所需的模块信息。
示例脚本
# Get information about module "Microsoft.Graph.Authentication"
$ModuleName = "Microsoft.Graph.Authentication"
$NewPath = "C:\usr\src\PSModules\$ModuleName"
$OldPath = "C:\Modules\User\$ModuleName"
if (Test-Path -Path $NewPath -PathType Container) {
Get-ChildItem -Path $NewPath
} elseif (Test-Path -Path $OldPath -PathType Container) {
Get-ChildItem -Path $OldPath
} else {
Write-Output "Module $ModuleName not present."
}
# Getting the path to the Temp folder, if needed.
$tmp = $env:TEMP
Get-AzStorageAccount cmdlet 可能会失败并出现错误:在模块 Get-AzStorageAccount 中找到了 Az.Storage 命令,但无法加载该模块 。
PowerShell Runbook 无法检索未加密且值为 Null 的变量资产。
PowerShell Runbook 无法检索名称中包含 *~* 的变量资产。
在 PowerShell Runbook 中,处于循环状态的 Get-Process 操作在经历大约 80 次迭代后可能会崩溃。
如果 PowerShell Runbook 尝试一次性将大量数据写入输出流中,则可能会发生故障。 通常情况下,通过使 Runbook 仅输出处理大型对象所需的信息可以避免出现这种问题。 例如,可以让 cmdlet 仅输出所需的参数,如在 Get-Process 中那样,而不是不受限地使用 Get-Process | Select ProcessName, CPU。
使用 ExchangeOnlineManagement 模块版本 3.0.0 或更高版本时,可能会遇到错误。 若要解决此问题,请确保还显式上传 PowerShellGet 和 PackageManagement 模块。
如果导入版本 2.12.3 或更高版本的 Az.Accounts 模块,并且 PowerShell 5.1 runbook 依赖于此版本的模块,请确保显式导入 Newtonsoft.Json v10 模块。 此问题的解决方法是使用 PowerShell 7.2 runbook。
使用 Az.Automation 模块中的 New-AzAutomationVariable cmdlet 上传 object 类型的变量时,该操作不按预期运行。
解决方法:使用 ConvertTo-Json cmdlet 将该对象转换为 JSON 字符串,然后使用 JSON 字符串作为变量值来上传变量。 此解决方法可确保将 Azure 自动化环境中变量作为 JSON 字符串正确处理。
示例 - 创建一个 PowerShell 对象,用于存储有关 Azure VM 的信息
# Retrieve Azure virtual machines with status information for the 'chinanorth3' region
$AzVM = Get-AzVM -Status | Where-Object {$_.Location -eq "chinanorth3"}
$VMstopatch = @($AzVM).Id
# Create an Azure Automation variable (This cmdlet will not fail, but the variable may not work as intended when used in the runbook.)
New-AzAutomationVariable -ResourceGroupName "mrg" -AutomationAccountName "mAutomationAccount2" -Name "complex1" -Encrypted $false -Value $VMstopatch
# Convert the object to a JSON string
$jsonString = $VMstopatch | ConvertTo-Json
# Create an Azure Automation variable with a JSON string value (works effectively within the automation runbook)
New-AzAutomationVariable -ResourceGroupName "mrg" -AutomationAccountName "mAutomationAccount2" -Name "complex1" -Encrypted $false -Value $jsonString
限制
- 父产品 PowerShell 不再支持 PowerShell 7.1。 建议在 PowerShell 7.4 中创建新的 runbook,以便长期受支持并更新过时的 runbook。
- Linux 混合 Runbook 辅助角色不支持 Azure 自动化内部 PowerShell cmdlet。 必须在 PowerShell runbook 的开头部分导入
automationassets 模块才能访问自动化帐户共享资源(资产)函数。
- 对于 PowerShell 7 运行时版本,不会为导入的模块提取模块活动。
- PowerShell 7 运行时版本不支持 PSCredential runbook 参数类型。
- PowerShell 7.x 不支持工作流。 有关详细信息,请参阅 PowerShell 工作流。
- PowerShell 7.x 目前不支持已签名的运行手册。
- 源代码控制集成不支持 PowerShell 7.1(预览版)。此外,源代码控制中的 PowerShell 7.1(预览版)运行手册会在自动化帐户中作为运行时版本 5.1 创建。
- 不支持通过
Get-AzAutomationModule cmdlet 管理 PowerShell 7.1 模块。
- 如果输入值包含字符 ',则 runbook 会失败,且没有日志跟踪。
- Azure Runbook 不支持带有
Start-Job 的 -credential。
- Azure 不支持所有 PowerShell 输入参数。
了解详细信息。
已知问题
由于服务后端基础结构的更改,依赖于内部文件路径 (如 C:\modules) 的 Runbook 可能会失败。 更改 Runbook 代码以确保内部文件路径上没有依赖项,并使用 Get-ChildItem 来获取所需的模块信息。
示例脚本
# Get information about module "Microsoft.Graph.Authentication"
$ModuleName = "Microsoft.Graph.Authentication"
$NewPath = "C:\usr\src\PSModules\$ModuleName"
$OldPath = "C:\Modules\User\$ModuleName"
if (Test-Path -Path $NewPath -PathType Container) {
Get-ChildItem -Path $NewPath
} elseif (Test-Path -Path $OldPath -PathType Container) {
Get-ChildItem -Path $OldPath
} else {
Write-Output "Module $ModuleName not present."
}
# Getting the path to the Temp folder, if needed.
$tmp = $env:TEMP
Get-AzStorageAccount cmdlet 可能会失败并出现错误:在模块 Get-AzStorageAccount 中找到了 Az.Storage 命令,但无法加载该模块 。
此预览版不支持使用 .\child-runbook.ps1 执行子脚本。
变通方法:使用 Start-AutomationRunbook(内部 cmdlet)或 Start-AzAutomationRunbook(来自 Az.Automation 模块)从父 runbook 中启动另一个 runbook。
在 PowerShell 7 运行时中,用于定义日志记录首选项的 runbook 属性不受支持。
解决方法:在运行手册开头显式设置首选项,如下所示:
$VerbosePreference = "Continue"
$ProgressPreference = "Continue"
请避免将版本为 2.4.0 的 Az.Accounts 模块导入到 PowerShell 7 运行时,因为在 Azure 自动化中使用此版本可能会出现意外行为。
对于在 PowerShell 7 运行时中运行的作业,可能会出现错误输出流的格式设置问题。
导入依赖于其他模块的 PowerShell 7.1 模块时,你可能会发现即使安装了依赖模块的 PowerShell 7.1 版本,导入按钮也是灰色的。 例如,Az PowerShell 模块 Compute 版本 4.20.0 依赖于 Az.Accounts 版本 >= 2.6.0。 当 PowerShell 5.1 中的等效依赖模块不满足版本要求时,会出现此问题。 例如,Az.Accounts 的 5.1 版本为 < 2.6.0。
在使用 Webhook 启动 PowerShell 7 runbook 时,它会将 Webhook 输入参数自动转换为无效的 JSON。
建议使用 ExchangeOnlineManagement 模块 3.0.0 或更低版本,因为 3.0.0 或更高版本可能会导致作业失败。
如果导入版本 2.12.3 或更高版本的 Az.Accounts 模块,并且 PowerShell 7.1 runbook 依赖于此版本的模块,请确保显式导入 Newtonsoft.Json v10 模块。 此问题的解决方法是使用 PowerShell 7.2 runbook。
使用 Az.Automation 模块中的 New-AzAutomationVariable cmdlet 上传 object 类型的变量时,该操作不按预期运行。
解决方法:使用 ConvertTo-Json cmdlet 将该对象转换为 JSON 字符串,然后使用 JSON 字符串作为变量值来上传变量。 此解决方法可确保将 Azure 自动化环境中变量作为 JSON 字符串正确处理。
示例 - 创建一个 PowerShell 对象,用于存储有关 Azure VM 的信息
# Retrieve Azure virtual machines with status information for the 'chinanorth3' region
$AzVM = Get-AzVM -Status | Where-Object {$_.Location -eq "chinanorth3"}
$VMstopatch = @($AzVM).Id
# Create an Azure Automation variable (This cmdlet will not fail, but the variable may not work as intended when used in the runbook.)
New-AzAutomationVariable -ResourceGroupName "mrg" -AutomationAccountName "mAutomationAccount2" -Name "complex1" -Encrypted $false -Value $VMstopatch
# Convert the object to a JSON string
$jsonString = $VMstopatch | ConvertTo-Json
# Create an Azure Automation variable with a JSON string value (works effectively within the automation runbook)
New-AzAutomationVariable -ResourceGroupName "mrg" -AutomationAccountName "mAutomationAccount2" -Name "complex1" -Encrypted $false -Value $jsonString
PowerShell 工作流 Runbook
PowerShell 工作流 Runbook 是基于 Windows PowerShell 工作流 的文本 Runbook。 可以在 Azure 门户中使用文本编辑器直接编辑 Runbook 的代码。 还可以使用任何离线文本编辑器,并将运行手册导入 Azure 自动化。
注意
PowerShell 7.1(预览版)和 PowerShell 7.2 不支持工作流 runbook。
优点
- 通过 PowerShell 工作流代码实现所有复杂的逻辑。
- 出现错误时,使用检查点恢复操作。
- 使用并行处理并行执行多个操作。
- 可将其他图形化 Runbook 和 PowerShell 工作流 Runbook 作为子 Runbook 包含进来,以创建高层工作流。
限制
- PowerShell 7+ 版本不支持 PowerShell 工作流。 因此,已过时的运行手册无法升级。
- 与较新的 PowerShell 7+ 版本相比,并行执行的处理效率低下。
- PowerShell 工作流在内部使用多个进程来工作。 因此,一个进程中可用的模块可能在另一个进程中不可用,并导致找不到命令等异常。
- Runbook 还必须处理与 PowerShell 工作流相关的其他复杂问题,例如反序列化的对象。
- 与 PowerShell Runbook 相比,Runbook 的启动时间更长,因为它们在运行前需要进行编译。
- 只能通过使用
Start-AzAutomationRunbook cmdlet 将 PowerShell Runbook 包含为子 Runbook。
- 运行簿无法在 Linux 混合运行簿辅助角色上运行。
Python 运行手册
Python 运行手册在 Python 3.10 下编译。 可以在 Azure 门户中使用文本编辑器直接编辑 Runbook 的代码。 还可以使用脱机文本编辑器,并将运行簿导入 Azure 自动化。 父产品不再支持 Python 2.7 和 Python 3.8,建议在 Python 3.10 运行时版本中创建 Runbook。
所有区域的云作业和混合作业都支持 Python 3.10 运行时版本。
优点
- 使用可靠的 Python 库。
- 可在 Azure 中运行,也可在混合 Runbook 辅助角色上运行。
- 如果代码与不同的版本兼容,则任何 3.x 版本的脚本和包都可能起作用。
- 对于 Windows 计算机上的 Python 3.10 混合作业,可以选择安装要使用的任何 3.x 版本。
- 对于 Linux 计算机上的 Python 3.10 混合作业,我们依赖于计算机上安装的 Python 3 版本来运行 DSC OMSConfig 和 Linux 混合辅助角色。 如果 Python 3 版本间的方法签名或协定没有发生重大更改,则不同的版本应该也会工作。
限制
Python Runbook 的限制包括:
- 对于 Python 3.10 模块,目前仅支持面向 cp310 Linux OS 的 wheel 文件。
了解更多信息
- 不支持源代码管理集成。
- 仅在作业运行时期间验证 Python 3.10 的自定义包。 如果包在运行时不兼容或者包所需的依赖项未导入到自动化帐户中,则作业预计会失败。
- 目前,仅 Azure 门户和 Rest API 支持 Python 3.10 Runbook。
- 父产品 Python 不再支持 Python 3.8。 建议在受支持的版本中创建新的 Runbook,并更新过时的 Runbook。
- 你必须熟悉 Python 脚本。
- 不支持源代码管理集成。
- 对于 Python 3.8 模块,请使用针对 cp38-amd64 的 wheel 文件。
- 若要使用第三方库,必须将包导入自动化帐户。
- 在 PowerShell/PowerShell 工作流中使用 Start-AutomationRunbook cmdlet 启动 Python 3.8 runbook 的操作不起作用。 你可以使用 Az.Automation 模块中的 Start-AzAutomationRunbook 或 AzureRm.Automation 模块中的 Start-AzureRmAutomationRunbook 来解决此限制。
- Azure 自动化不支持 sys.stderr。
- Python automationassets 包在 pypi.org 上不可用,因此无法导入到 Windows 计算机上。
- 父产品 Python 不再支持 Python 2.7。 建议在受支持的版本中创建新的运行手册,并更新过时的运行手册。
- 你必须熟悉 Python 脚本。
- 对于 Python 2.7.12 模块,请使用 cp27-amd64 的 wheel 文件。
- 若要使用第三方库,必须将包导入自动化帐户。
- Azure 自动化不支持 sys.stderr。
- Python automationassets 包在 pypi.org 上不可用,因此无法导入到 Windows 计算机上。
注意
不支持使用 Webhook 启动 Python Runbook。
多个 Python 版本
它适用于 Windows 混合辅助角色工作器。 对于 Windows Runbook 辅助角色,运行 Python 2 Runbook 时,它会首先查找环境变量 PYTHON_2_PATH,并验证它是否指向有效的可执行文件。 例如,如果安装文件夹为 C:\Python2,它将检查 C:\Python2\python.exe 是否是有效的路径。 如果未找到,它会查找 PATH 环境变量以执行类似的检查。
对于 Python 3,它首先查找 PYTHON_3_PATH 环境变量,然后返回到 PATH 环境变量。
如果只使用一个版本的 Python,可以将安装路径添加到 PATH 变量。 如果要在 Runbook Worker 上同时使用这两个版本,请将 PYTHON_2_PATH 和 PYTHON_3_PATH 设置为这些版本对应模块的路径。
已知问题
对于云作业,Python 3.8 作业有时会失败并显示异常消息 invalid interpreter executable path。 如果作业延迟、启动时间超过 10 分钟或使用 Start-AutomationRunbook 来启动 Python 3.8 Runbook,则可能会看到此异常。 如果作业延迟,重新启动 runbook 应该就足够了。
图形化运行手册
可以在 Azure 门户中使用图形编辑器创建和编辑图形 Runbook 与图形 PowerShell 工作流 Runbook。 但是,您无法使用其他工具创建或编辑此类 Runbook。 图形化运行手册的主要功能:
- 导出到自动化帐户中的文件,然后导入到另一个自动化帐户中。
- 生成 PowerShell 代码。
- 在导入过程中转换为图形化 PowerShell 工作流运行手册,或从图形化 PowerShell 工作流运行手册转换而来。
优点
- 使用可视化的插入-链接-配置创作模式。
- 重点关注数据如何在流程中流转。
- 直观展示管理流程。
- 将其他 Runbook 作为子 Runbook 包含进来,以创建高层级工作流。
- 鼓励模块化编程。
限制
- 无法在 Azure 门户之外进行创建或编辑。
- 可能需要包含 PowerShell 代码的代码活动,才能执行复杂逻辑。
- 无法转换为文本格式中的一种,也无法将文本 Runbook 转换为图形格式。
- 不能查看或直接编辑图形工作流创建的 PowerShell 代码。 你可以查看在任意代码活动中创建的代码。
- 无法在 Linux 混合运行手册辅助角色上运行运行手册。 请参阅使用混合 Runbook 辅助角色使数据中心或云端的资源实现自动化。
- 图形运行手册无法进行数字签名。
后续步骤