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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
Anonymous
Not applicable

encrypt credentials to create datasource - PowerShell

I'm trying to automate gateway data source management using PowerShell. First I need to encrypt credentials. Here is my PowerShell script to do this;

$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)

The solution described on this page stopped working - on the end of 2019

We're receiving the error that the
"Destination array was not long enough. Check destIndex and length, and the array's lower bounds"
on the line "Array.Copy(segmentEncryptedResult, 0, encryptedData, encryptedDataPosition, segmentEncryptedResult.Length);"

Do you have any fix writing in PowerShell? I have found sth in cs: microsoft/PowerBI-CSharp@83ba689

I will be apricated for any hint. Thanks!

1 ACCEPTED SOLUTION
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

See if this sample will help you:

https://github.com/microsoft/PowerBI-Developer-Samples/blob/master/PowerShell%20Scripts/bindToGatewa... 

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
Sébastien
New Member

Hello, here is my function, i found some code in the past available from internet.

In this function, it assumes that you already have imported the MicrosoftPowerBiMgmt modules

You need to download the Microsoft.PowerBI.Api.dll in order to call some code:

 

<#
.Synopsis
   Encrypt the credentials against a DataGateway
#>
function Get-EncryptedCredentialDetails
{
    param
    (
        # The Username
        [parameter(Mandatory=$true)][string]$Username,
        # The Password
        [parameter(Mandatory=$true)][string]$Password,
        # The DataGateway ID
        [parameter(Mandatory=$true)][string]$DataGatewayId
    )

    [System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\Microsoft\Microsoft.PowerBI.Api.dll")

    $publicKey = (Invoke-PowerBIRestMethod -Url gateways/$($DataGatewayId) -Method Get | ConvertFrom-Json).publicKey
    $gatewayPublicKey = [Microsoft.PowerBI.Api.Models.GatewayPublicKey]::new($publicKey.exponent, $publicKey.modulus)
    $basicCredentials = [Microsoft.PowerBI.Api.Models.Credentials.BasicCredentials]::new($Username, $Password)
    $credentialsEncryptor = [Microsoft.PowerBI.Api.Extensions.AsymmetricKeyEncryptor]::new($gatewayPublicKey)

    [Microsoft.PowerBI.Api.Models.CredentialDetails]::new(
        $basicCredentials,
        [Microsoft.PowerBI.Api.Models.PrivacyLevel]::Organizational,
        [Microsoft.PowerBI.Api.Models.EncryptedConnection]::Encrypted,
        $credentialsEncryptor)
}

 

V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

See if this sample will help you:

https://github.com/microsoft/PowerBI-Developer-Samples/blob/master/PowerShell%20Scripts/bindToGatewa... 

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.