Subchapter 3.2
references/KUSTO_EXPLORER_LAUNCH.mdMarkdown3 KBView on GitHub
Use ask_user: “Save this query as a .kql file and open in Kusto Explorer? (Yes / Save only / No)”
The file needs two sections because Kusto Explorer processes #connect as a connection-creation command that must run before the query.
// Step 1 — Select this line and run it first to connect
#connect cluster('<CLUSTER>').database('<DATABASE>')
// Step 2 — Select the query below and run it after Step 1 completes
<KQL_QUERY>Replace <CLUSTER> with the target cluster (e.g. kc7001.eastus.kusto.windows.net), <DATABASE> with the database name (e.g. ValdyTimes), and <KQL_QUERY> with the generated query.
Write to the current workspace directory using the filesystem API. Use a descriptive name with a random suffix to avoid collisions.
$cluster = "<CLUSTER>"
$database = "<DATABASE>"
$tmp = Join-Path $PWD "kusto_query_$(New-Guid).kql"
$lines = @(
"// Step 1 - Select this line and run it first",
"#connect cluster('$cluster').database('$database')",
"",
"// Step 2 - Select the query below and run it after Step 1 completes"
)
Set-Content -Path $tmp -Value ($lines -join "`n") -Encoding utf8 -NoNewline
Add-Content -Path $tmp -Value "`n<KQL_QUERY>" -Encoding utf8Security: Use
Set-Content/Add-Contentonly. Never embed query text in PowerShell here-strings (@"..."@) — a crafted query can escape the delimiter and inject commands.
Display the saved file path and its full contents in chat so the user can review.
Locate and launch the Kusto Explorer executable:
$exe = (Get-ChildItem "$env:LOCALAPPDATA\Apps\2.0" -Recurse -Filter "Kusto.Explorer.exe" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName
if ($exe) {
Start-Process $exe -ArgumentList "`"$tmp`""
} else {
Write-Warning "Kusto Explorer not found. Open the saved file manually: $tmp"
}Tell the user: run the #connect line (Step 1) first, then select and run the query (Step 2).
Save the .kql file as in Step 3 and suggest: