2017-06-14 74 views
0

我的主要問題是:我想將Azure SQL中運行速度最慢的查詢發送到負載測試調試的集中式日誌記錄系統。以表格格式嚮應用程序洞察發送Azure自動化輸出

如何將結果集發送給應用程序見解?我想將Azure自動化中運行速度最慢的查詢發送給Application Insights?

我有運氣嘗試這樣做時,它以表格形式

workflow Use-SqlCommandSample 
{ 
param(
    [parameter(Mandatory=$True)] 
    [string] $SqlServer, 

    [parameter(Mandatory=$False)] 
    [int] $SqlServerPort = 1433, 

    [parameter(Mandatory=$True)] 
    [string] $Database, 

    [parameter(Mandatory=$True)] 
    [string] $Table, 

    [parameter(Mandatory=$True)] 
    [PSCredential] $SqlCredential 
) 

# Get the username and password from the SQL Credential 
$SqlUsername = $SqlCredential.UserName 
$SqlPass = $SqlCredential.GetNetworkCredential().Password 

inlinescript { 
    # Define the connection to the SQL Database 
    $Conn = New-Object System.Data.SqlClient.SqlConnection("xxxx") 

    # Open the SQL connection 
    $Conn.Open() 
    $Cmd=new-object system.Data.SqlClient.SqlCommand("SELECT top 10 creation_time"+ 
     ",last_execution_time"+ 
     ",total_physical_reads"+ 
     ",total_logical_reads "+ 
     ",total_logical_writes"+ 
     ", execution_count"+ 
     ", total_worker_time"+ 
     " , total_elapsed_time"+ 
     ", total_elapsed_time/execution_count avg_elapsed_time"+ 
     ",SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,"+ 
     " ((CASE statement_end_offset"+ 
     " WHEN -1 THEN DATALENGTH(st.text)"+ 
     " ELSE qs.statement_end_offset END"+ 
     " - qs.statement_start_offset)/2) + 1) AS statement_text"+ 
     " FROM sys.dm_exec_query_stats AS qs"+ 
     " CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st"+ 
     " ORDER BY total_elapsed_time/execution_count DESC;", $Conn) 
    $Cmd.CommandTimeout=120 

    # Execute the SQL command 
    $Ds=New-Object system.Data.DataSet 
    $Da=New-Object system.Data.SqlClient.SqlDataAdapter($Cmd) 
    [void]$Da.fill($Ds) 



     $assemblyPath = 
    "C:\Modules\Global\Azure\Compute\Microsoft.ApplicationInsights.dll" 
    [System.Reflection.Assembly]::LoadFrom($assemblyPath) 
    $TelClient = New-Object "Microsoft.ApplicationInsights.TelemetryClient" 
    $TelClient.InstrumentationKey = "1234" 

    # Output the result 


    $TelClient.TrackEvent($Ds.Tables) 
    $TelClient.Flush 

    # Close the SQL connection 
    $Conn.Close() 
} 

}

回答

1

在應用程序見解活動可以採取名稱,價值屬性包。因此,您必須將表格序列化爲json,然後將其提交給AppInsights

1

你確認你已經登錄您的SQL服務器成功。我檢查你的腳本行,

$Conn = New-Object System.Data.SqlClient.SqlConnection("xxxx") 

我想一件事,你需要注意,根據本official document

默認情況下,在工作流程中定義的變量不是 InlineScript腳本塊中的命令可見。要使 工作流變量對InlineScript可見,請使用$ Using範圍 修飾符。 InlineScript中的每個 變量只需要$ Using範圍修飾符一次。

如果要在inlinecript中使用$SqlUsername$SqlPass,則應使用以下幾行。

inlinescript { 

# Get the username and password from workflow 
$ServerName = $Using:SqlUsername 
$Password = $Using:SqlPass 
    ...... 
} 

更多信息請參考此鏈接:Azure Automation: Your SQL Agent in the Cloud

+0

我遇到了從Azure觸發應用程序洞察的麻煩。它適用於我的PowerShell ISE。我如何參考Azure自動化中的DLL? –