Search code examples
powershellrestazure-data-explorerkql

How to run Get Kusto query using Rest API


I need to frame the REST API call to Kusto Cluster to get results for a basic Kusto Query.

As per this Kusto Documentation, We need to send the Query as a parameter. Is an example available? The documentation has examples only for POST calls which I was able to figure out. But now, I need to do GET calls as well to fetch data from Kusto tables.

Below is my sample REST API Call to Kusto. Where and how to pass the Kusto query below?

$accessToken = (Get-AzAccessToken -ResourceUrl "https://MyKustoClusterName.westus2.kusto.windows.net").Token

$authHeader = @{
        'Content-Type'='application/json'
        'Authorization'='Bearer ' + $accessToken
        'Host'='MyKustoClusterName.westus2.kusto.windows.net'
        }

$Query = MyTableName | project column1, column2, column3

$Uri = "https://MyKustoClusterName.westus2.kusto.windows.net/v2/rest/query"
$Response = Invoke-RestMethod -Uri $Uri -Method GET -Headers $authHeader -Query???


Solution

  • note: it's probably much simpler to invoke the .NET libraries from powershell, instead of authoring your own code for constructing and sending a request, then parsing the response.


    the request body is documented here: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/api/rest/request

    there's also an example at the bottom, that uses curl (which you should easily be able to use as-is, or 'translate' to using the Invoke-WebRequest, if you prefer the syntax of the latter)

    the sample body provided in that document is as follows (and matches the query print Test="Hello, World!", running against a database named Samples):

    {
      "db":"Samples",
      "csl":"print Test=\"Hello, World!\"",
      "properties":"{\"Options\":{\"queryconsistency\":\"strongconsistency\"},\"Parameters\":{},\"ClientRequestId\":\"MyApp.Query;e9f884e4-90f0-404a-8e8b-01d883023bf1\"}"
    }