assets/OfficialDemos/Others/PowerShell/2.Kusto-BasicRead-MyCluster.ps1 (42 lines of code) (raw):

# dependencies $packagesRoot = "C:\Microsoft.Azure.Kusto.Tools\tools\net5.0" [System.Reflection.Assembly]::LoadFrom("$packagesRoot\Kusto.Data.dll") # Part 3 of 3 # ------------ # Defining the connection to your cluster / database $clusterUrl = "https://kvc43f0ee6600e24ef2b0e.southcentralus.kusto.windows.net;Fed=True" $databaseName = "MyDatabase" # Option A: using Azure AD User Authentication $kcsb = New-Object Kusto.Data.KustoConnectionStringBuilder ($clusterUrl, $databaseName) # Option B: using Azure AD application Authentication # $applicationId = "application ID goes here" # $applicationKey = "application key goes here" # $authority = "authority goes here" # $kcsb = $kcsb.WithAadApplicationKeyAuthentication($applicationId, $applicationKey, $authority) # # NOTE: if you're running with Powershell 7 (or above) and the .NET Core library, # AAD user authentication with prompt will not work, and you should choose # a different authentication method. # Example: Running an admin command $adminProvider = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCslAdminProvider($kcsb) $command = [Kusto.Data.Common.CslCommandGenerator]::GenerateDiagnosticsShowCommand() Write-Host "Executing command: '$command' with connection string: '$($kcsb.ToString())'" $reader = $adminProvider.ExecuteControlCommand($command) $reader.Read() # this reads a single row/record. If you have multiple ones returned, you can read in a loop $isHealthy = $Reader.GetBoolean(0) Write-Host "IsHealthy = $isHealthy" # Example: Running a query $queryProvider = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCslQueryProvider($kcsb) $query = "StormEvents | limit 5" Write-Host "Executing query: '$query' with connection string: '$($kcsb.ToString())'" # Optional: set a client request ID and set a client request property (e.g. Server Timeout) $crp = New-Object Kusto.Data.Common.ClientRequestProperties $crp.ClientRequestId = "MyPowershellScript.ExecuteQuery." + [Guid]::NewGuid().ToString() $crp.SetOption([Kusto.Data.Common.ClientRequestProperties]::OptionServerTimeout, [TimeSpan]::FromSeconds(30)) # Execute the query $reader = $queryProvider.ExecuteQuery($query, $crp) # Do something with the result datatable, for example: print it formatted as a table, sorted by the # "StartTime" column, in descending order $dataTable = [Kusto.Cloud.Platform.Data.ExtendedDataReader]::ToDataSet($reader).Tables[0] $dataView = New-Object System.Data.DataView($dataTable) $dataView | Sort StartTime -Descending | Format-Table -AutoSize