You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure自动化混合工作器PowerShell7.1调用Get-AutomationPSCredential报错

Azure Automation混合工作器PowerShell 7.1兼容Get-AutomationPSCredential的解决方案

根因说明

Orchestrator.AssetManagement.Cmdlets是Azure Automation内置模块,基于.NET Framework 4.x开发,仅原生兼容Windows PowerShell 5.1运行时。PowerShell 7.1基于.NET Core 3.1构建,无法直接加载.NET Framework程序集及其依赖(如报错中提到的JobRuntimeData.Client程序集),因此会触发加载失败的报错。

排查步骤

  • 确认模块目标运行时:在Windows PowerShell 5.1中执行以下命令,查看模块对应程序集的目标框架,确认其为.NET Framework 4.x版本
    (Get-Module Orchestrator.AssetManagement.Cmdlets -ListAvailable).Path
    
  • 确认混合工作器版本:当前V1、V2版本混合工作器的内置内部Cmdlet均仅原生支持Windows PowerShell 5.1,无适配PowerShell 7+的官方版本。

解决方法

方案1:使用PowerShell 7兼容层加载模块

PowerShell 7内置Windows兼容层,可通过参数指定将.NET Framework模块转发到后台Windows PowerShell进程执行,无需大幅修改原有代码:

# 导入模块时添加兼容参数
Import-Module Orchestrator.AssetManagement.Cmdlets -UseWindowsPowerShell
# 原有获取凭证逻辑无需修改
[PSCredential] $AutomationCredential = Get-AutomationPSCredential -Name 'abcdef'

方案2:调用Azure Automation REST API获取凭证

如果不想依赖兼容层,可使用混合工作器的托管身份直接调用Azure公开API拉取凭证,原生兼容PowerShell 7+运行时:

  1. 为混合工作器所属的虚拟机/虚拟机规模集启用系统分配托管身份,为该身份分配自动化账户的Automation Credential Reader权限
  2. 使用以下代码获取凭证:
# 获取托管身份访问令牌
$tokenUri = $env:IDENTITY_ENDPOINT + "?resource=https://management.azure.com&api-version=2019-08-01"
$headers = @{"X-IDENTITY-HEADER" = $env:IDENTITY_HEADER}
$token = (Invoke-RestMethod -Uri $tokenUri -Headers $headers -Method Get).access_token

# 调用API拉取凭证
$subId = "替换为你的订阅ID"
$rgName = "替换为资源组名称"
$aaName = "替换为自动化账户名称"
$credName = "abcdef"
$apiUri = "https://management.azure.com/subscriptions/$subId/resourceGroups/$rgName/providers/Microsoft.Automation/automationAccounts/$aaName/credentials/$credName`?api-version=2022-08-08"
$credInfo = Invoke-RestMethod -Uri $apiUri -Headers @{Authorization = "Bearer $token"} -Method Get

方案3:拆分脚本执行逻辑

将需要调用内部Cmdlet的逻辑拆分到单独的.ps1脚本,在PowerShell 7.1中通过powershell.exe(即Windows PowerShell 5.1)调用该脚本获取返回结果,适合复杂场景下的逻辑拆分。

内容的提问来源于stack exchange,提问作者David Thoma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 06:06:08