powershell - 发送请求时在 PowerShell 中使用括号

标签 powershell powershell-2.0 powershell-3.0 powershell-4.0

我正在尝试在 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

但是我可以看到我需要转义括号。有关如何提出这样的请求的任何指示吗?

enter image description here

最佳答案

如果要在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/

相关文章:

PowerShell:ForEach-Object 的神秘 -RemainingScripts 参数

azure - 如何通过用户交互来安排任务

json - 不知道如何在Powershell中获取JSON对象的名称字段?

powershell - Get-FSRM-如何获取以GB为单位的使用率和大小?

powershell-2.0 - 如何从 Powershell 向 Slack 发布消息

powershell - $使用:var in Start-Job within Invoke-Command

powershell - 测试PowerShell中是否存在文件

mysql - 如何通过从数据库读取计算机名来检索登录用户?

string - 在PowerShell中拆分没有分隔符的字符串

powershell - 如何使用Powershell转换文件内容