-
Notifications
You must be signed in to change notification settings - Fork 1.5k
CreateDatasource #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
CreateDatasource #217
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| ######################################################################## | ||
| # Script created by Sabre Ammar, Cesar Almeida and Arjun Mohan | ||
| # 09/03/2023 | ||
| # | ||
| # **Notes** | ||
| # Script created based on the idea and basis here: | ||
| # https://endjin.com/blog/2020/12/how-to-update-credentials-for-an-on-prem-power-bi-data-source-using-powershell | ||
| ######################################################################## | ||
|
|
||
| #region Initialize | ||
| # EncryptGatewayCredentials script path | ||
| $EncryptCredentialsScriptPath = "<path to the 'EncryptGatewayCredentials' script>" | ||
|
|
||
| # Update datasource API details | ||
| $datasourceName = "" | ||
| $datasourceType = "" | ||
| $GatewayId = "" | ||
| $CreateDatasourceUrl = "https://api.powerbi.com/v1.0/myorg/gateways/$GatewayId/datasources" | ||
|
|
||
| # Service Principal credentials | ||
| $Secret = "" | ||
| $AppID = "" | ||
| $AppPassword = ConvertTo-SecureString $Secret -AsPlainText -Force | ||
| $Credential = New-Object PSCredential $AppID, $AppPassword | ||
| $TenantID = "" | ||
|
|
||
| # Acquire token for Service Principal | ||
| Connect-PowerBIServiceAccount -Tenant $TenantID -ServicePrincipal -Credential $Credential | ||
|
|
||
| # In this case we are using an example to create a datasource for an On-Prem SQL database using "Windows credentials" | ||
| # The username here is the windows username, so this is why it needs to contain the domain. | ||
| # Be mindful of the permission levels the user has on the database | ||
| # The password is written here in plain text for convenience, in production, we recommend that you use KeyVault and store the password securely there. | ||
| $username = "" | ||
| # password = SQL user password in case of basic credentials, or windows user password in case of windows credentials | ||
| $password = '' | ||
| # Get the Gateway data for the encryption process | ||
| $gw = Invoke-PowerBIRestMethod ` | ||
| -Url "https://api.powerbi.com/v1.0/myorg/gateways/$GatewayId" ` | ||
| -Method GET ` | ConvertFrom-Json | ||
|
|
||
| # On-Prem Gateway exponent | ||
| $gatewayExponent = $gw.publicKey.exponent | ||
|
|
||
| # On-Prem Gateway modulus which you can get using https://learn.microsoft.com/en-us/rest/api/power-bi/gateways/get-gateway API | ||
|
Tamarael marked this conversation as resolved.
|
||
| $gatewayModulus = $gw.publicKey.modulus | ||
|
|
||
| #endregion | ||
|
|
||
| # Encrypt credentials using EncryptGatewayCredentials script and create request body | ||
| Import-Module $EncryptCredentialsScriptPath | ||
|
|
||
| # On the line below, change the "EncryptWindowsCredentials" according to the credentials you are using | ||
| # Please check the "EncryptGatewayCredentials" script for the supported credentials | ||
| $encryptedCredentials = EncryptWindowsCredentials -Username $username -PasswordAsString $password -GatewayExponent $gatewayExponent -GatewayModulus $gatewayModulus | ||
| $credentialsObject = $encryptedCredentials | ConvertFrom-Json | ||
| $credentialDetails = $credentialsObject.credentialDetails | ||
|
|
||
| $Body = @{ | ||
| "datasourceType"=$datasourceType | ||
| "connectionDetails"='{"server":"<serverName>","database":"<DBName>"}' | ||
| "datasourceName"=$datasourceName | ||
| "credentialDetails" = @{ | ||
| "credentialType" = $credentialDetails.credentialType | ||
| "credentials" = $credentialDetails.credentials | ||
| "encryptedConnection" = $credentialDetails.encryptedConnection | ||
| "encryptionAlgorithm" = $credentialDetails.encryptionAlgorithm | ||
| "privacyLevel" = $credentialDetails.privacyLevel | ||
| } | ||
| } | ||
| $bodyJson = $body | ConvertTo-Json | ||
|
|
||
| Invoke-PowerBIRestMethod -Url $CreateDatasourceUrl -Method Post -Body $bodyJson | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.