Json 将参数值传递给PowerShell脚本

Json 将参数值传递给PowerShell脚本,json,powershell,Json,Powershell,以下是来自我的PowerShell脚本的片段,其中未替换参数$book和$author的值。请建议一些技术,我可以应用于修复它或分享一些代码,可以帮助我 $body = @{ version = '1.0' inactive = 'false' yml = { "Service1:\n book: $book\n author: $author\n "} | ConvertFrom-Json } | ConvertTo-Json $request = Invoke-WebRequest -Us

以下是来自我的PowerShell脚本的片段,其中未替换参数
$book
$author
的值。请建议一些技术,我可以应用于修复它或分享一些代码,可以帮助我

$body = @{
version = '1.0'
inactive = 'false'
yml = { "Service1:\n book: $book\n author: $author\n "} | ConvertFrom-Json
} | ConvertTo-Json

$request = Invoke-WebRequest -UseBasicParsing -Method Post -Uri $uri -Body 
$body -Headers $headers -ContentType $contentType

$response = ConvertFrom-Json -InputObject $request.Content

你在这行有一些奇怪的事情

... yml = { "Service1:\n book: $book\n author: $author\n "} | ConvertFrom-Json } | ConvertTo-Json
因为它说“使用这个主体做一个测试,并尝试将脚本块转换为JSON”

因此,如果您希望在
yml
字段中有一个JSON字符串,您有两个选项

  • 自己编写正确的JSON字符串:

    @{...put the rest of your elements here...; yml = "{Service1:'', book:'$book', author: '$author'}"
    
  • 首先填充,然后将其转换为JSON字符串:

    @{...put the rest of your elements here...; yml = @{Service1=''; book='$book'; author='$author'} } | ConvertTo-Json
    

  • @沃鲁…我想你可能没有仔细看卷发背带。。。。ConvertToJSON用于最外层的构造,ConvertFrom Json仅用于该哈希表中的该元素。。。请再看看。。。感谢您提出这两个选项。。。让我试试,我会让你知道…请忽略我的问题,因为我不再寻求解决方案。。。它已经通过一些其他技术得到了解决。非常感谢hep。请忽略我的问题,因为我不再寻求解决方案。。。它已经通过一些其他技术得到了解决。尽管如此,还是非常感谢hep。@dn\U学习者很高兴听到你成功了。不过,如果我的答案回答了原来的问题,请将其标记为答案。