共用方式為


從 PowerShell 使用 Kusto .NET 用戶端連結庫

使用 版本 下拉選單切換服務。 了解更多關於導航的資訊
適用於: ✅ Microsoft Fabric ✅ Azure Data Explorer

PowerShell 腳本可以使用 Kusto 用戶端連結庫,因為 PowerShell 原本就與 .NET 連結庫整合。 在本文中,您將瞭解如何載入和使用用戶端連結庫來執行查詢和管理命令。

必要條件

  • 用來擷取 ZIP 檔案的封存工具,例如 7-Zip 或 WinRAR。

取得連結庫

若要在 PowerShell 中使用 Kusto .NET 用戶端連結庫:

  1. 下載 Microsoft.Azure.Kusto.Tools

  2. 以滑鼠右鍵按鍵按鍵的套件。 從功能表中,選取您的封存工具並擷取套件內容。 如果功能表中看不到封存工具,請選取 [ 顯示更多選項]。 擷取會產生多個資料夾,其中一個是具名 工具

  3. [工具 ] 資料夾內,有不同的子資料夾會迎合不同的PowerShell版本。 針對 PowerShell 5.1 版,請使用 net472 資料夾。 針對 PowerShell 第 7 版或更新版本,請使用 net472 資料夾以外的任何版本資料夾。 複製相關資料夾的路徑。

  4. 從 PowerShell 載入連結庫,並將 取代 <path> 為複製的資料夾路徑:

    [System.Reflection.Assembly]::LoadFrom("<path>\Kusto.Data.dll")
    

    您應該會看到如下所示的輸出:

    GAC 版本 Location
    False v4.0.30319 C:\Downloads\tools\net472\Kusto.Data.dll

載入之後,您可以使用連結庫來 連線到叢集和資料庫

注意

Linux 不支援 net472 資料夾中的工具。

線上到叢集和資料庫

使用下列其中一種方法向叢集和資料庫進行驗證:

  • 用戶驗證: 提示使用者在網頁瀏覽器中驗證其身分識別。
  • 應用程式驗證:建立Microsoft Entra 應用程式,並使用認證進行驗證。
  • Azure CLI 驗證: 登入您電腦上的 Azure CLI,而 Kusto 會從 Azure CLI 擷取令牌。

選取相關的索引標籤。

執行第一個查詢或命令之後,這個方法會開啟互動式瀏覽器視窗以供用戶授權。

$clusterUrl = "<Your cluster URI>"
$databaseName = "<Your database name>"

$kcsb = New-Object Kusto.Data.KustoConnectionStringBuilder($clusterUrl, $databaseName)

執行查詢

建立查詢提供者並執行 Kusto 查詢語言 查詢。

下列範例會定義簡單的 take 查詢來取樣數據。 若要執行查詢,請將 取代 <TableName> 為您資料庫中的數據表名稱。 ClientRequestProperties 類別 用來設定用戶端要求標識碼和伺服器逾時,再執行查詢。 然後,會執行查詢,並格式化和排序結果集。

$queryProvider = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCslQueryProvider($kcsb)
$query = "<TableName> | take 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))

# Run 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

輸出

StartTime EndTime EpisodeID EventID 州/省 EventType InjuriesDirect InjuriesIndirect DeathsDirect DeathsIndirect
2007-12-30 16:00:00 2007-12-30 16:05:00 11749 64588 喬治亞州 雷暴風 0 0 0 0
2007-12-20 07:50:00 2007-12-20 07:53:00 12,554 68796 密西西比州 雷暴風 0 0 0 0
2007-09-29 08:11:00 2007-09-29 08:11:00 11091 61032 大西洋南部 水 spout 0 0 0 0
2007-09-20 21:57:00 2007-09-20 22:05:00 11078 60913 佛羅里達州 龍捲風 0 0 0 0
2007-09-18 20:00:00 2007-09-19 18:00:00 11074 60904 佛羅里達州 大雨 0 0 0 0

執行管理命令

建立 CSL 管理提供者並執行 管理命令

下列範例會執行管理命令來檢查叢集的健康情況。

$adminProvider = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCslAdminProvider($kcsb)
$command = [Kusto.Data.Common.CslCommandGenerator]::GenerateDiagnosticsShowCommand()
Write-Host "Executing command: '$command' with connection string: '$($kcsb.ToString())'"
# Run the command
$reader = $adminProvider.ExecuteControlCommand($command)
# Read the results
$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"

輸出

IsHealthy = True

如需如何使用 Kusto 用戶端連結庫執行管理命令的詳細資訊,請參閱 建立應用程式以執行管理命令

範例

下列範例示範在可公開存取 help 叢集上載入連結庫、驗證和執行查詢的程式。

#  This is an example of the location from where you extract the Microsoft.Azure.Kusto.Tools package
#  Make sure you load the types from a local directory and not from a remote share
#  Make sure you load the version compatible with your PowerShell version (see explanations above)
#  Use `dir "$packagesRoot\*" | Unblock-File` to make sure all these files can be loaded and executed
$packagesRoot = "C:\Microsoft.Azure.Kusto.Tools\tools\net472"
#  Load the Kusto client library and its dependencies
[System.Reflection.Assembly]::LoadFrom("$packagesRoot\Kusto.Data.dll")
#  Define the connection to the help cluster and database
$clusterUrl = "https://help.kusto.windows.net;Fed=True"
$databaseName = "Samples"
# MS Entra user authentication with interactive prompt
$kcsb = New-Object Kusto.Data.KustoConnectionStringBuilder($clusterUrl, $databaseName)
# Run a simple query
$queryProvider = [Kusto.Data.Net.Client.KustoClientFactory]::CreateCslQueryProvider($kcsb)
$query = "StormEvents | take 5"
$reader = $queryProvider.ExecuteQuery($query, $crp)

控制追蹤

由於所有 PowerShell 應用程式只有一個全域 PowerShell.exe.config 檔案,因此連結庫通常不能依賴 。NET 的 app.config 模型可存取其設定。 您仍然可以使用程式設計模型進行追蹤。 如需詳細資訊,請參閱 控制追蹤

您可以改用下列方法:

  • 開啟主控台追蹤:

    $traceListener = New-Object Kusto.Cloud.Platform.Utils.ConsoleTraceListener
    [Kusto.Cloud.Platform.Utils.TraceSourceManager]::RegisterTraceListener($traceListener)
    
  • 使用 Kusto.Cloud.Platform.Utils.RollingCsvTraceListener2 寫入追蹤之資料夾位置的單一自變數建立物件。