且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将json对象作为参数传递给另一个Powershell脚本

更新时间:2023-11-09 23:52:52

您的JSON格式不正确.我认为核心问题是JSON末尾带有逗号.您也不会在声明中关闭开头的引号.

Your JSON is malformed. I think the core issue is that you have a trailing comma at the end of your JSON. You also don't close the opening quotation in your declaration.

如果您仍然使用here-string,则可能会更轻松.这是您不必使用所有反引号.

You might have a much easier time if you use a here-string for this anyway. This was you don't have to use all those backticks.

$jsonParams = @"
{
     "TaskName": "$taskName",
      "ExitCode": "$exitCode",
      "ErrorMessage": "$errorMessage"
   }
"@

$jsonObject = $jsonParams | ConvertFrom-Json


$jsonObject已经是一个自定义对象,不再是JSON.您无需对其进行任何特殊处理.删除参数块中的类型,然后在脚本中调用属性.


$jsonObject is already a custom object and no longer JSON. You don't need to do anything special with it. Remove the type in your param block and just call the properties in your script.

param (
    [string]$param1,
    [string]$param2,
    $jsonObject
)
$jsonObject.TaskName