cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
AlexTanasie Visitor
Visitor

Re: Power BI REST API via Powershell: Create Datasource

The credentials from credentialDetails need to be encripted unsing RSA algorithm before calling the api. I've used an alghorthim posted in https://community.powerbi.com/t5/Developer/How-do-I-update-the-Credentials-after-I-upload-a-report-t... and converted it to powershell. The algorithm from https://docs.microsoft.com/en-us/power-bi/developer/encrypt-credentials is also good. Also the server name needs to have double the amount of "\" charahters to work.

 

function CreateDatasourceForGateway([guid]$GatewayId, 
                                    [string]$DatasourceName, 
                                    [string]$TenantServer, 
                                    [string]$TenantDatabase,
                                    [string]$TenantDataSourceUser,
                                    [string]$TenantDataSourcePassword) {   

    $gateway = Invoke-PowerBIRestMethod `
                -Url "https://api.powerbi.com/v1.0/myorg/gateways/$GatewayId" `
                -Method GET ` | ConvertFrom-Json                

    $datasourceDetails = ConvertTo-Json -Depth 4 -InputObject $(@{
        dataSourceType = "Sql"
        connectionDetails = '{"server":"'+$TenantServer.Replace("\","\\")+'","database":"'+$TenantDatabase+'"}'
        datasourceName = $DatasourceName
        credentialDetails = @{
            credentialType = "Basic"
            credentials = Encript `
                            -Username $TenantDataSourceUser `
                            -Password $TenantDataSourcePassword `
                            -GatewayExponent $gateway.publicKey.exponent `
                            -GatewayModulus $gateway.publicKey.modulus
            encryptedConnection = "Encrypted"
            encryptionAlgorithm = "RSA-OAEP"
            privacyLevel = "None"
        }
    })

    $result = Invoke-PowerBIRestMethod `
            -Url "https://api.powerbi.com/v1.0/myorg/gateways/$GatewayId/datasources" `
            -Method POST `
            -Body $datasourceDetails
}

function Encript([string]$Username,[string]$Password,[string]$GatewayExponent,[string]$GatewayModulus) {
    $segmentLength = 85
    $encryptedLength = 128
    $plaintTxt = '{"credentialData":[{"value":"'+$Username+'","name":"username"},{"value":"'+$Password+'","name":"password"}]}'
    $rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider ($encryptedLength * 8)
    $parameters = $rsa.ExportParameters($false)
    $parameters.Exponent = [System.Convert]::FromBase64String($GatewayExponent)
    $parameters.Modulus = [System.Convert]::FromBase64String($GatewayModulus)
    $rsa.ImportParameters($parameters)
    $plainTextArray = [System.Text.Encoding]::UTF8.GetBytes($plaintTxt)    
    $hasIncompleteSegment = $plainTextArray.Length % $segmentLength -ne 0

    $segmentNumber = If (-not $hasIncompleteSegment) {[int]($plainTextArray.Length / $segmentLength)} Else {[int]($plainTextArray.Length / $segmentLength) + 1}
    $encryptedData = [System.Byte[]]::CreateInstance([System.Byte],$segmentNumber * $encryptedLength)
    [int]$encryptedDataPosition = 0;

    For ($i=0; $i -lt $segmentNumber; $i++) {
        $lengthToCopy = If ($i -eq ($segmentNumber - 1) -and $hasIncompleteSegment) {$plainTextArray.Length % $segmentLength} Else {$segmentLength}
        $segment = [System.Byte[]]::CreateInstance([System.Byte],$lengthToCopy)
        [System.Array]::Copy($plainTextArray,$i*$segmentLength,$segment,0,$lengthToCopy)
        $segmentEncryptedResult = $rsa.Encrypt($segment, $true)
        [System.Array]::Copy($segmentEncryptedResult,0,$encryptedData,$encryptedDataPosition,$segmentEncryptedResult.Length)
        $encryptedDataPosition += $segmentEncryptedResult.Length;
    }

    return [System.Convert]::ToBase64String($encryptedData)
}

Connect-PowerBIServiceAccount

CreateDatasourceForGateway `
    -GatewayId $GatewayId `
    -DatasourceName "$TenantReportPrefix Datasource"`
    -TenantServer $TenantServer `
    -TenantDatabase $TenantDatabase `
    -TenantDataSourceUser $TenantDataSourceUser `
    -TenantDataSourcePassword $TenantDataSourcePassword

 

 

pbiembed4 Occasional Visitor
Occasional Visitor

Re: Power BI REST API via Powershell: Create Datasource

Thanks a LOT !

 

Got stuck couple of hours only to found out what you suggested:

--> Also the server name needs to have double the amount of "\" charahters to work.

 

 

 

Helpful resources

Announcements
New Topics Started Badges Coming

New Topics Started Badges Coming

We're releasing new versions of the badge that everyone's talking about. ;) Check your inbox for notifications.

MBAS 2020

Save the new date (and location)!

Our business applications community is growing—so we needed a different venue, resulting in a new date and location. See you there!

Difinity Conference

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Kudoed Authors (Last 30 Days)