KustoPowershell.psm1

function Import-Kusto-Package
{
    # Check if Nuget is available as an installed package provider, add it if not:
    Write-Host "Checking for Nuget package Microsoft.Azure.Kusto.Tools..."
    $isNugetAvailable = Get-PackageSource -ProviderName Nuget -Location "https://www.nuget.org/api/v2"
    if (!$isNugetAvailable)
    {
        Write-Host "Nuget V2 not found as a Package Source. Adding it now..."
        Register-PackageSource -Name NuGet -Location https://www.nuget.org/api/v2 -ProviderName NuGet
    }

    # Check if Nuget is installed as an installed package, add it if not:
    $isKqlDllInstalled = Get-Package -Name "Microsoft.Azure.Kusto.Tools"
    if (!$isKqlDllInstalled)
    {
        Write-Host "Microsoft.Azure.Kusto.Tools package not installed. Installing now..."
        # This requires Admin. Check if Admin, kill if not:
        $isAdmin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
        if ($isAdmin -eq $false)
        {
            Write-Host "Installation of a new Nuget package requires Admin. Please re-run this script as an administrator"
            $Script:importkusto = 1
        }
        else
        {
            Write-Host "Package Microsoft.Azure.Kusto.Tools not yet installed! Installing..."
            $isKqlDllInstalled = Install-Package "Microsoft.Azure.Kusto.Tools" -Source "https://www.nuget.org/api/v2" -Force
        }
    }
    else
    {
        $version = $isKqlDllInstalled.Version
        Write-Host "Package Microsoft.Azure.Kusto.Tools (Version $version) already installed! Proceeding..."
    }

    $packagePath = $isKqlDllInstalled.Source
    $packageRoot = $packagePath.Substring(0, ($packagePath).lastIndexOf('\'))
    Get-ChildItem $packagesRoot\* | Unblock-File
    [System.Reflection.Assembly]::LoadFrom("$packageRoot\tools\net472\Kusto.Data.dll")
}
Export-ModuleMember -Function Import-Kusto-Package

Function Invoke-Kusto-Query {
    [cmdletbinding()]
    param(
       [parameter(Mandatory=$True,ValueFromPipeline=$true)]
       $query,
       $cluster,
       $databaseName
    )
    Process {
        $clusterUrl = "https://"+$cluster+";Fed=True"
        $kcsb = New-Object Kusto.Data.KustoConnectionStringBuilder ($clusterUrl, $databaseName)
        $queryProvider = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCslQueryProvider($kcsb)

        $crp = New-Object Kusto.Data.Common.ClientRequestProperties
        $crp.ClientRequestId = "MyPowershellScript.ExecuteQuery." + [Guid]::NewGuid().ToString()
        $crp.SetOption([Kusto.Data.Common.ClientRequestProperties]::OptionServerTimeout, [TimeSpan]::FromSeconds(60))

        # 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]
        $result = New-Object System.Data.DataView($dataTable)
    }
    end {
        Return $result
    }
}
Export-ModuleMember -Function Invoke-Kusto-Query