1

I'm trying to call a PowerBI GETinfo Scanner API using PowerShell. One of the requirements is to pass multiple workspaces to get the info. Here is the MS doc link :\

https://learn.microsoft.com/en-us/rest/api/power-bi/admin/workspace-info-post-workspace-info#example

However, I'm not able to pass below syntax for API body in PowerShell. The below syntax to call multiple workspaces in API body is not working :

$auth_body =@{
  "workspaces": [
    "97d03602-4873-4760-b37e-1563ef5358e3",
    "67b7e93a-3fb3-493c-9e41-2c5051008f24"
  ]
}

I'm only able to pass single workspace and below syntax works :

$auth_body =@{'workspaces' ="3b7e9b1c-bdac-4e46-a39d-1b3d24a0e122"}

Please help me to form the syntax for multiple workspaces. Seems I'm not able to form key value pair inside PowerShell for multiple workspaces

Updated Code after applying MathiasR.Jessen suggestion:

$authority = 'https://login.microsoftonline.com/oauth2/'+ $tenantID 
$authResult = Get-AdalToken -Authority $authority -Resource $resourceAppIdURI -ClientID $UId -Clientsecret $password -TenantID $tenantID 
  $Token=$authResult.AccessToken
#Write-Output "Token: $Token"
$auth_header = @{
    'Accept' = "application/json";
    'Authorization' = 'Bearer ' +$Token
}

$auth_body = @{
  "workspaces" = 
    @("50c4bd8e-fc75-433e-a0cd-755f9329515e","97d03602-4873-4760-b37e-1563ef5358e3")
    
  
}

$uri = "https://api.powerbi.com/v1.0/myorg/admin/workspaces/getInfo"
$all_workspace = (Invoke-RestMethod -Uri $uri –Headers $auth_header -Body $auth_body  –Method Post)

And Error Message :

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
+ ... rkspace1 = (Invoke-RestMethod -Uri $uri –Headers $auth_header -Body $ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

However, It works perfectly if I provide single workspace.

Sanjibsrkr
  • 15
  • 4
  • Looks like you need JSON, not a Hashtable – Theo Feb 25 '22 at 16:47
  • @Theo I'm assuming this gets passed to a web cmdlet that'll take care of json serialization... – Mathias R. Jessen Feb 25 '22 at 16:48
  • @MathiasR.Jessen Thanks for your reply. At least the PowerShell syntax issue is resolved. However, Still getting error with the API response. Updated the code and error message above – Sanjibsrkr Feb 28 '22 at 09:14
  • 1
    @MathiasR.Jessen Understood where it went wrong. Added below arguments to the API call and worked perfectly. Thank you very much ``` -Body ($auth_body | ConvertTo-Json) -ContentType "application/json – Sanjibsrkr Feb 28 '22 at 16:52

1 Answers1

1

You're mixing PowerShell and JSON syntax.

To define an array in PowerShell, use the @() array subexpression operator:

$auth_body = @{
  "workspaces" = @(
    "97d03602-4873-4760-b37e-1563ef5358e3",
    "67b7e93a-3fb3-493c-9e41-2c5051008f24"
  )
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206