Forum Discussion

dom99's avatar
dom99
Advocate V
6 years ago
Solved

BindToGateway Powershell REST API POST failing - Need Help

Hi,

I am struggling to get the BindToGateway REST API call to work in powershell https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/bindtogatewayingroup

 

Below is my code for doing the API call

 

 

 

Invoke-PowerBiRestMethod -Url "groups/$($workspaceid)/datasets/$($datasetid)/Default.BindToGateway" -Method POST -Body $jsonPostBody

 

 


The workspace and dataset varibales are correct, and the Body is in the format:

 

 

 

{
"datasourceObjectIds": [
"dc2f2dac-e5e2-4v37-af76-2a0bc10f1bs5",
"3bfe5d33-ag7d-4d24-b0b5-e2b96eb01cf5"
] ,
"gatewayObjectId": "1f85e798-5852-4fdd-ab01-33cc14b6e934"
}

 

 



When I run this as a user who is an administrator on the gateway I get the error

 

 

 

Response status code does not indicate success: 404 (Not Found).

StackTrace : at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at Microsoft.PowerBI.Commands.Profile.InvokePowerBIRestMethod.<INVOKERESTMETHOD>d__31.MoveNext()
Exception : System.Net.Http.HttpRequestException
InvocationInfo : {Invoke-PowerBIRestMethod}
Line : Invoke-PowerBiRestMethod -Url $url -Method POST -Body $jsonPostBody
Position : At line:1 char:1
+ Invoke-PowerBiRestMethod -Url $url -Method POST -Body $jsonPostBody
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 


When I run it as a service principal with all deletgated API permissions and an admin on the workspace and dataset owner I get the following error

 

 

 

Message: Response status code does not indicate success: 400 (Bad Request).

StackTrace : at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at Microsoft.PowerBI.Commands.Profile.InvokePowerBIRestMethod.<INVOKERESTMETHOD>d__31.MoveNext()
Exception : System.Net.Http.HttpRequestException
InvocationInfo : {Invoke-PowerBIRestMethod}
Line : Invoke-PowerBiRestMethod -Url $url -Method POST -Body $jsonPostBody
Position : At line:1 char:1
+ Invoke-PowerBiRestMethod -Url $url -Method POST -Body $jsonPostBody
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 



So one gives me a 400 and the other a 404 error with no further usfull info which I can see.

Can anyone offer any suggestions?&nbsp;

Thanks
Dominic

  • I have found the reasons why this wasnt working so thought I would list them here for future users. To get this to work there are a few things to be aware of:

     

    1. If the datasource is not assigned to a gateway you need to not include the datasource ID values (you wont actually be able to get these ID values if it is unassigned). I assume the ability to specify the data source id values is only relevent when the dataset is already assigned to a gateway, but I dont know if you can use two separate gateways for one dataset with two datasources.

     

    2. Service principals cant be set to administrators of gateways, so you cant use them

     

    3. Users must be set as administrators of gateways

     

    4. The user must be assigned as a user of the data source defined on the gateway (different from being a gateway admin)

     

    With all this set up properly it works like a charm 🙂

7 Replies

  • I have found the reasons why this wasnt working so thought I would list them here for future users. To get this to work there are a few things to be aware of:

     

    1. If the datasource is not assigned to a gateway you need to not include the datasource ID values (you wont actually be able to get these ID values if it is unassigned). I assume the ability to specify the data source id values is only relevent when the dataset is already assigned to a gateway, but I dont know if you can use two separate gateways for one dataset with two datasources.

     

    2. Service principals cant be set to administrators of gateways, so you cant use them

     

    3. Users must be set as administrators of gateways

     

    4. The user must be assigned as a user of the data source defined on the gateway (different from being a gateway admin)

     

    With all this set up properly it works like a charm 🙂

    • Anonymous's avatar
      Anonymous
      Not applicable

      Dom99... I am also facing with the same issue.

      Are you saying that with a Service principal we cannot achive this?

      if it can be achieved can you provide more details.

      • ilav's avatar
        ilav
        Frequent Visitor

        Is someone able to pick the DSN attached to the gateway via an API or Powershell?

      • dom99's avatar
        dom99
        Advocate V

        I didnt, it wasnt possible for reasons listed in the solution. 

         

        As far as I'm aware it is still not possible, if you want to do this you will need to bind it as a user with a pro license who is an administrator of the gateway, and point number 4 listed in the solution