Monday, September 26, 2022

Querying a Kusto table using PowerShell Core 7.0 fails and throws error


kusto table query using PowerShell Core 7.0 fails when hosted in Azure function, the Code works fine when executed in local machine with PowerShell version 5.* however the same code doesn't work in my local machine when I change the PowerShell version from 5.* to 7.0 and Azure function (which is also running PS version 7.0). PowerShell .net client library is downloaded from this location

I am trying AAD authentication using secret, below is the sample code and the error I am getting when executing it in Azure Function with PowerShell core version 7.0

Sample Code

$packagesRoot = "$PSScriptRoot\tools\netcoreapp2.1"[System.Reflection.Assembly]::LoadFrom("$packagesRoot\Kusto.Data.dll")
$clusterUrl = ";Fed=True"
$databaseName = "DatabaseName"
$kcsb1 = New-Object Kusto.Data.KustoConnectionStringBuilder ($clusterUrl, $databaseName)

#Using Azure AD application Authentication
$applicationId = "ClientId_here"
$applicationKey = "ClientSecret_here"
$authority = "TenantId_here"
$kcsb = $kcsb1.WithAadApplicationKeyAuthentication($applicationId, $applicationKey, $authority)
$queryProvider = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCslQueryProvider($kcsb)
$query = 'KustoQuery_here'
$crp = New-Object Kusto.Data.Common.ClientRequestProperties
$crp.ClientRequestId = "MyPowershellScript.ExecuteQuery." + [Guid]::NewGuid().ToString()
$crp.SetOption([Kusto.Data.Common.ClientRequestProperties]::OptionServerTimeout, [TimeSpan]::FromSeconds(30))

#Executing the query
$reader = $queryProvider.ExecuteQuery($query, $crp)

Error received when executing the code in PowerShell Core 7.0:

PS C:\Program Files\PowerShell\7>  C:\Users\Administrator\Desktop\Kusto7.0\TestKusto.ps1
Please make sure you have the .Net Kusto libraries downloaded at C:\Users\Administrator\Desktop\Kusto7.0
Location is C:\Users\Administrator\Desktop\Kusto7.0\tools\netcoreapp2.1

GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\Users\Administrator\Desktop\Kusto7.0\tools\netcoreapp2.1\Kusto.Data.dll
MethodInvocationException: C:\Users\Administrator\Desktop\Kusto7.0\TestKusto.ps1:53
Line |
  53 |  $queryProvider = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCs …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "CreateCslQueryProvider" with "1" argument(s): "The type initializer for
     | 'Kusto.Data.Common.KustoTrustedEndpoints' threw an exception."


Found that the issue is happening only on PowerShell Core 7.0, I changed the PowerShell core version of Azure function to 7.2 and the code started working. I did install PowerShell version 7.0 and &.7.2 in my local machine and executed the same code to rule out any issues with Azure function and it showed the exact same behavior, the code executed with PowerShell Core version 7.2 in my local machine and started throwing errors on PowerShell Core 7.0, which concludes that .net kusto library has some issues with PowerShell Core version 7.0.