Azure Automation中Logic Apps传参至PowerShell脚本失败求助
解决Azure Automation PowerShell作业参数转换失败问题
你的报错核心原因是Logic Apps传递的是JSON对象而非字符串,但脚本参数定义为[string],类型不匹配导致参数转换失败。以下是两种可行的修复方案:
方案一:修改PowerShell脚本适配JSON对象
直接将参数类型从[string]改为[PSObject],跳过字符串转JSON的步骤,直接处理结构化对象:
param ( [Parameter(Mandatory=$true)] [PSObject]$azureArcJson, [Parameter(Mandatory=$true)] [PSObject]$crowdstrikeJson ) try { # 调试输出对象数量 Write-Output "Azure Arc对象数量: $($azureArcJson.Count)" Write-Output "Crowdstrike对象数量: $($crowdstrikeJson.Count)" # 直接从对象提取主机名 $azureArcHostnames = $azureArcJson | ForEach-Object { $_.Hostname } $crowdstrikeHostnames = $crowdstrikeJson | ForEach-Object { $_.Hostname } # 对比差异 $azureArcMissing = $crowdstrikeHostnames | Where-Object { $_ -notin $azureArcHostnames } $crowdstrikeMissing = $azureArcHostnames | Where-Object { $_ -notin $crowdstrikeHostnames } # 格式化输出结果 $output = "Azure Arc与Crowdstrike主机名差异:`n" $output += "仅存在于Crowdstrike的主机名:`n" $output += ($azureArcMissing -join "`n") $output += "`n`n仅存在于Azure Arc的主机名:`n" $output += ($crowdstrikeMissing -join "`n") Write-Output $output } catch { Write-Error "错误详情: $_" }
方案二:在Logic Apps中转换参数为字符串
如果不想修改脚本,在Logic Apps调用Azure Automation作业的步骤中,将输入参数的值用string()函数包裹,强制转换为JSON字符串:
- 例如,将
azureArcJson的参数值设置为string(triggerBody()['azureArcData'])(替换为你实际的JSON对象路径) - 确保传递给脚本的是JSON格式的字符串,而非结构化对象
内容的提问来源于stack exchange,提问作者SentinelAzure
相关产品推荐
相关产品推荐

