我正在尝试在 ISE 中发送包含以下正文的请求:
$body = @{
'roles'=['write'],
"grantedToIdentities": [{
"application": {
"id": "xx-7736-4e25-95ad-3fa95f62b66e",
"displayName": "Contoso Time Manager App"
}
}]
}
$Result = Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/sites/xx-53D2-xx-A368-A7F3E475F0A0/permissions' -Headers $Headers
write-host $Result
但是我可以看到我需要转义括号。有关如何提出这样的请求的任何指示吗?
最佳答案
如果要在PowerShell中表达数组,可以使用@()
语法。此外,您必须省略每个属性后面的逗号并使用 @{}
语法定义对象/哈希表。您的 body 可能是这样的:
$body = @{
roles = @('write')
grantedToIdentities = @( @{
application = @{
id = "xx-7736-4e25-95ad-3fa95f62b66e"
displayName = "Contoso Time Manager App"
}
})
}
对应的 JSON 如下所示:
{
"roles": [
"write"
],
"grantedToIdentities": [
{
"application": {
"id": "xx-7736-4e25-95ad-3fa95f62b66e",
"displayName": "Contoso Time Manager App"
}
}
]
}
这将与 Create permission Graph request 的预期有效负载匹配.
⚠ 注意:您没有将 $body
对象添加到示例中的请求中。
关于powershell - 发送请求时在 PowerShell 中使用括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68405541/