如何在Windows PowerShell中以JSON格式填充集合中的列表
我正在处理一个任务,在这个任务中,我应该在Powershell环境中调用REST Post请求 基于谷歌的研究,我决定使用如何在Windows PowerShell中以JSON格式填充集合中的列表,json,powershell,Json,Powershell,我正在处理一个任务,在这个任务中,我应该在Powershell环境中调用REST Post请求 基于谷歌的研究,我决定使用Invoke-RestMethodcmdlet。 对于POST请求,正文应为JSON格式,如下所示 { "appid": "sample string 1", "appName": "sample string 2", "appType": "sample string 3", "hostName": "sample string 4", "userNam
Invoke-RestMethod
cmdlet。
对于POST
请求,正文应为JSON格式,如下所示
{
"appid": "sample string 1",
"appName": "sample string 2",
"appType": "sample string 3",
"hostName": "sample string 4",
"userName": "sample string 5",
"password": "sample string 6",
"status": "sample string 7",
"appUri": "sample string 8",
"resourceParams": [
{
"data1": "sample string 1",
"data2": "sample string 2",
"data3": "sample string 3"
},
{
"data1": "sample string 1",
"data2": "sample string 2",
"data3": "sample string 3"
}
],
"Params": [
{
"hostName": "sample string 1",
"ip": "sample string 2"
},
{
"hostName": "sample string 1",
"ip": "sample string 2"
},
{
"hostName": "sample string 1",
"ip": "sample string 2"
}
]
}
现在为了填充这个json内容,我使用了如下代码
$externalAPP = @{
appId = 1
appName = "test"
appType = 1
userName = "Administrator@test"
password = "xxxxxxxx"
status = "1"
resourceParams = @{
data1 = "test"
data2 = "test"
data3 = "test"
}
Params = {@{
hostName = "ex1"
ipAddress = "192.1.1.1"
}
@{
hostName = "ex2"
ipAddress = "192.1.1.2"
}
}
}
$json = $externalAPP | ConvertTo-Json
Write-Host $json
如果resourceparms
和Params
中只有一项,则上述代码可以正常工作。如果有多个,则格式不同,如下所示
样本输出
"appName": "test",
"esxiParams": {
"Attributes": [
],
"File": "C:\\Users\\kspviswa\\Desktop\\Demo3\\test.ps1",
"IsFilter": false,
"IsConfiguration": false,
"Module": null,
"StartPosition": {
"Content": "{@{\r\n\t\thostName = \"ex1\"\r\n\t\tipAddress = \"192.
"\r\n\t}\r\n\t@{\r\n\t\thostName = \"ex2\"\r\n\t\tipAddress = \"192.1.1.2\"\r\n\t}\r\n\t}",
"Type": 19,
"Start": 292,
"Length": 117,
"StartLine": 14,
"StartColumn": 15,
"EndLine": 22,
"EndColumn": 3
},
"DebuggerHidden": false,
"Ast": {
"ParamBlock": null,
"BeginBlock": null,
"ProcessBlock": null,
"EndBlock": "@{\r\n\t\thostName = \"ex1\"\r\n\t\tipAddress = \"192.1.1.1\"\r\
\n\t@{\r\n\t\thostName = \"ex2\"\r\n\t\tipAddress = \"192.1.1.2\"\r\n\t}",
"DynamicParamBlock": null,
"ScriptRequirements": null,
"Extent": "{@{\r\n\t\thostName = \"ex1\"\r\n\t\tipAddress = \"192.1.1.1\"\r\n
n\t@{\r\n\t\thostName = \"ex2\"\r\n\t\tipAddress = \"192.1.1.2\"\r\n\t}\r\n\t}",
"Parent": "{@{\r\n\t\thostName = \"ex1\"\r\n\t\tipAddress = \"192.1.1.1\"\r\n
n\t@{\r\n\t\thostName = \"ex2\"\r\n\t\tipAddress = \"192.1.1.2\"\r\n\t}\r\n\t}"
}
},
"password": "xxxx"
}
我犯了什么错误?我尝试了几个网站寻找答案。所有的例子都是关于扁平容器的。我想了解如何在容器中填充列表。在JSON中,
[]
表示列表或数组-因此,不要在{}
中包含参数
值(使其成为脚本块),而是使用数组子表达式操作符@()
:
$externalAPP = @{
appId = 1
appName = "test"
appType = 1
userName = "Administrator@test"
password = "xxxxxxxx"
status = "1"
resourceParams = @(
@{
data1 = "test"
data2 = "test"
data3 = "test"
}
)
Params = @(
@{
hostName = "ex1"
ipAddress = "192.1.1.1"
},
@{
hostName = "ex2"
ipAddress = "192.1.1.2"
}
)
}
这将产生:
{
"userName": "Administrator@test",
"appType": 1,
"appId": 1,
"Params": [
{
"ipAddress": "192.1.1.1",
"hostName": "ex1"
},
{
"ipAddress": "192.1.1.2",
"hostName": "ex2"
}
],
"resourceParams": [
{
"data1": "test",
"data2": "test",
"data3": "test"
}
],
"status": "1",
"appName": "test",
"password": "xxxxxxxx"
}
非常感谢。我知道这应该是一个基本的错误。但是我不能通过这个,因为我是powershell的新手。