microsoft / microsoft/powerbi-powershell

Power BI Service - Bind workspace to a another gateway using BindToGateway in PowerShell

Open
#367 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
413
Forks
134
Avg merge
33m
Merged PRs (30d)
1

Description

Based on the following example provided on GitHub, I am trying to bind a new gateway to a workspace:
https://github.com/Azure-Samples/powerbi-powershell/blob/master/bindtogateway.ps1

However, I keep receiving errors and based on these two articles stating issues experienced, is there an alternative method of binding to a new gateway?

https://community.powerbi.com/t5/Developer/BindToGateway-Powershell-REST-API-POST-failing-Need-Help/m-p/847527
https://community.powerbi.com/t5/Developer/BindToGateway-Powershell-REST-API-POST-failing-Need-Help/m-p/852851#M21503

The error I receive is: You cannot call a method on a null-valued expression on the following line in the catch section:
$result = $_.Exception.Response.GetResponseStream()

Here is a snipped of the code:

$datasets = Get-PowerBIDataset -WorkspaceId $target_workspace_ID

echo "GatewayId: $newGatewayId"

Looping through all datasets in the workspace

foreach ($dataset in $datasets)
{
echo "Entered for each loop"

$datasetId = $dataset.id; # Get dataset id
$gatewayId = $newGatewayId

# POST body 
$postParams = @{
    "gatewayObjectId" = "$gatewayId"
}

$jsonPostBody = $postParams | ConvertTo-JSON
echo "jsonPost: $jsonPostBody"

# Make the request to bind to a gateway
$uri = "groups/$target_workspace_ID/datasets/$datasetId/BindToGateway"

echo "uri: $uri"

# Try to bind to a new gateway     
try 
{
    Invoke-RestMethod -Uri $uri -Method POST -Body $jsonPostBody -Verbose

    Write-Host ""
    Write-Host "The Gateway has been updated!"
} 
catch 
{
    $result = $_.Exception.Response.GetResponseStream()
    $reader = New-Object System.IO.StreamReader($result)
    $reader.BaseStream.Position = 0
    $reader.DiscardBufferedData()
    $responseBody = $reader.ReadToEnd();
        
    # DMTS_CanNotFindMatchingDatasourceInGatewayError means that there is no 
    # datasource available in a gateway to bind to.
    # Ignore DMTS_CanNotFindMatchingDatasourceInGatewayError
    If(($responseBody).Contains("DMTS_CanNotFindMatchingDatasourceInGatewayError"))
    {
        Write-Host "Error: No data source available in gateway."
    }
    elseif($_.Exception.Response.StatusCode.value__ -eq "401")
    {
        Write-Host "Error: No access to app workspace."
    }
    elseif($_.Exception.Response.StatusCode.value__ -eq "404")
    {
        Write-Host "Error: Dataset may be owned by someone else. Call take over API to assume ownership and try again."
    }
    else
    {
        Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
        Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
        Write-Host "StatusBody:" $responseBody
    }
}

}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked bindtogateway.ps1 example and the PowerShell loop around Invoke-RestMethod. Reproduce the BindToGateway request and inspect the exception before the null-valued response stream is accessed. Done means documenting a supported binding method or a confirmed correction for the failing request.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
api, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.