assets/OfficialDemos/Others/PowerShell/1.Kusto-BasicRead.ps1 (55 lines of code) (raw):

# Pre-reqs: # ------------ # Invoke-WebRequest -Uri https://nuget.org/api/v2/package/Microsoft.Azure.Kusto.Tools -OutFile Microsoft.Azure.Kusto.Tools.zip # Expand-Archive .\Microsoft.Azure.Kusto.Tools.zip -Destination "C:\Microsoft.Azure.Kusto.Tools\" -Force # Remove-Item .\Microsoft.Azure.Kusto.Tools.zip -force # Install PowerShell 7: winget install --id Microsoft.Powershell --source winget # Part 1 of 3 # ------------ # Packages location - This is an example of the location from where you extract the Microsoft.Azure.Kusto.Tools package # Please make sure you load the types from a local directory and not from a remote share # Please make sure you load the version compatible with your PowerShell version (see explanations above) $packagesRoot = "C:\Microsoft.Azure.Kusto.Tools\tools\net5.0" # Part 2 of 3 # ------------ # Loading the Kusto.Client library and its dependencies [System.Reflection.Assembly]::LoadFrom("$packagesRoot\Kusto.Data.dll") # Part 3 of 3 # ------------ # Defining the connection to your cluster / database $clusterUrl = "https://help.kusto.windows.net;Fed=True" $databaseName = "Samples" # 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