Search code examples
powershellpowerbipowerbi-apipowerbi-rest-api

Scanner API Call using PowerShell for multiple PowerBI workspaces


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.


Solution

  • 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"
      )
    }