Using brackets in PowerShell when sending a request

Viewed 49

I am trying to send a request with following body in 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

However I can see I need to escape the brackets. Any pointers on how to make a request like this?

enter image description here

1 Answers

If you want to express an array in PowerShell, you can use the @() syntax. In addition, you have to omit the comma after each property and define objects/hashtables using the @{} syntax . This is how your body could look like:

$body = @{
    roles               = @('write')
    grantedToIdentities = @( @{
            application = @{
                id          = "xx-7736-4e25-95ad-3fa95f62b66e"
                displayName = "Contoso Time Manager App"
            }
        })
}

And this is how the corresponding JSON looks like:

{
  "roles": [
    "write"
  ],
  "grantedToIdentities": [
    {
      "application": {
        "id": "xx-7736-4e25-95ad-3fa95f62b66e",
        "displayName": "Contoso Time Manager App"
      }
    }
  ]
}

This will match the expected payload for the Create permission Graph request.

⚠ Note: You are not adding the $body object to the request in your example.

Related