将参数值传递给PowerShell脚本
问题描述:
以下是我的PowerShell脚本中的片段,其中参数$book
和$author
的值未被替换。请提出一些我可以应用的技术来修复它或分享一些可以帮助我的代码。将参数值传递给PowerShell脚本
$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
因为它说:“做这个身体script block,并尝试将脚本块转换成JSON”。
所以,如果你想在yml
字段中有一个JSON字符串,你有两个选择。
-
写正确的JSON字符串自己:
@{...put the rest of your elements here...; yml = "{Service1:'', book:'$book', author: '$author'}"
-
填充hashtable第一,然后将其转换成JSON字符串:
@{...put the rest of your elements here...; yml = @{Service1=''; book='$book'; author='$author'} } | ConvertTo-Json
@vorou ...我认为你可能没有仔细看过花括号...... ConvertTo-Json是最外层的构造,而ConvertFrom-Json仅用于该哈希表中的元素...请再看看...谢谢你提出这2个选项...让我试试,我会让你knkow ... –
你是对的,我错过了大括号;看到更新 – vorou
请不理我的问题,因为我不再寻求解决方案......它已经通过一些其他技术照顾。真的很欣赏肝虽然。 –