Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
sm3
Frequent Visitor

Creating new SQL Datasource for Gateway with Powershell.

Hello,

Is it possible to automatise creating SQL Datasource in specified Gateway with Powershell?
Documentation says it's possible to create new Datasource https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/createdatasource with Rest API by 'Invoke-PowerBiRestMethod' in PS.
If I'm not wrong as credentials we can use example from here: https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/updatedatasource#examples (update credentials).

So we've got body:

$body = '{
"datasourceType": "Sql",
"connectionDetails": "{\"server\":\"real_sql_server_address\",
                       \"database\":\"real_database_name\"}",
"datasourceName": "new_name_for_datasource"
"credentialDetails": {"credentialType": "Basic",
                                "credentials": "{\"credentialData\":[{\"name\":\"username\",
                                                                      \"value\":\"real_sql_username\"},
                                                                     {\"name\":\"password\", 
                                                                      \"value\":\"real_password\"}]
                                }",
                                 "encryptedConnection": "Encrypted",
                                 "encryptionAlgorithm": "None",
                                 "privacyLevel": "None"
},
}'



Invoke-PowerBiRestMethod -Url https://api.powerbi.com/v1.0/myorg/gateways/Id-of-real-gateway/datasources -Method Post -Body $body -Verbose


*with bold are marked real parameters we can use

When we execute above code: we've got response:

Status Code: BadRequest (400)


Above code without slashes and with option to convert to json format doesn't work either:

$body = '{"datasourceType": "Sql",
"connectionDetails": "{"server":"real_sql_server_address",
                       "database":"real_database_name"}",
  "datasourceName": "new_name_for_datasource"
  "credentialDetails": {"credentialType": "Basic",
                        "useEndUserOAuth2Credentials":false,
                        "credentials": "{"credentialData\":[{"name":"username",
                                                              "value":"real_sql_username"},
                                                             {"name":"password", 
                                                              "value":"real_password"}]}",
                        "encryptedConnection": "Encrypted",
                        "encryptionAlgorithm": "None",
                        "privacyLevel": "None"
  },
}'
$body = $body | ConvertTo-Json

After executing command:

Resolve-PowerBIError -last 

we receive:

Message        : 400 (Bad Request).
StackTrace     :    w System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
                    w Microsoft.PowerBI.Commands.Profile.InvokePowerBIRestMethod.<InvokeRestMethod>d__31.MoveNext()
Exception      : System.Net.Http.HttpRequestException
InvocationInfo : {Invoke-PowerBIRestMethod}
Line           : Invoke-PowerBiRestMethod -Url https://api.powerbi.com/v1.0/myorg/gateways/Id-of-real-gateway/datasources -Method Post -Body $body
                  -Verbose
Position       : At line:1 char:1
                 + Invoke-PowerBiRestMethod -Url https://api.powerbi.com/v1.0/myorg/gate ...
                 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : xxx


Even simple update datasource doesn't work:

$body = '{
  "credentialDetails": {
    "credentialType": "Basic",
    "credentials": "{"credentialData":[{"name":"username", "value":"real_sql_username"},{"name":"password", "value":"real_sql_password"}]}",
    "encryptedConnection": "Encrypted",
    "encryptionAlgorithm": "None",
    "privacyLevel": "None"
  }
}'

$body = $body | ConvertTo-Json



Invoke-PowerBiRestMethod -Url  https://api.powerbi.com/v1.0/myorg/gateways/Id-gateway/datasources/Id-datasource -Method Patch -Body $body -Verbose

Response:

VERBOSE: Status Code: BadRequest (400)

Am I doing something wrong or it's just doesn't work?
Is it even possible with Powershell?

Greetings


3 REPLIES 3
Anonymous
Not applicable

Anyone figured out how to add a datasource without encryption and without credentials like the checkbox "skip test connection"?

 

I tried using "anonymus" credential type but that did not work yet in my tests.

Anonymous
Not applicable

I am having the exact same issue.  No clue how to resolve.  Have you had any luck?

Anonymous
Not applicable

Was this issue ever solved? I am getting a 400 bad request. 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors