Invoke-SqlAssessment
執行 SQL 評定最佳做法檢查所選SQL Server物件,並傳回其結果。
Syntax
Invoke-SqlAssessment
[-Check <Object[]>]
[[-InputObject] <PSObject>]
[-Configuration <PSObject>]
[-MinSeverity <SeverityLevel>]
[-FlattenOutput]
[<CommonParameters>]
Description
Invoke-SqlAssessment Cmdlet 會針對每個輸入物件執行評估,並傳回應該套用至指定物件的最佳做法建議清單。 您必須先遵循指定的建議。 如需詳細資訊,請參閱 SQL 評定 API 概觀。
此 Cmdlet 接受下列輸入類型:
- Microsoft.SqlServer.Management.Smo.Server
- Microsoft.SqlServer.Management.Smo.Database
- Microsoft.SqlServer.Management.Smo.AvailabilityGroup
- Microsoft.SqlServer.Management.Smo.FileGroup
- Microsoft.SqlServer.Management.Smo.RegisteredServers.RegisteredServer
- 包含上述類型之任何物件路徑的字串
- 物件的集合
您可以使用 sqlServer Cmdlet 取得輸入物件,例如 Get-SqlInstance 和 Get-SqlDatabase 或基本的 PowerShell Cmdlet,例如 Get-Item 和 Get-ChildItem。 此外,Cmdlet 也支援 SQL Server PowerShell 提供者,因此它可以從其路徑取得物件。 可以明確傳遞路徑,否則會使用目前的路徑。
所選物件的檢查可用性會因SQL Server版本、平臺和物件類型而異。 此外,也會檢查以 或 master
之類的 tempdb
特定資料庫為目標。 您也可以使用參數 -MinSeverity 和 -Check,依標記、名稱和嚴重性來篩選檢查。
您可以使用Get-SqlAssessmentItem Cmdlet,取得適用于指定SQL Server物件的檢查清單。
Cmdlet 只會執行適用于輸入物件的檢查。 例如,即使在 -Check 清單中指定,資料庫檢查也不會針對SQL Server實例或可用性群組執行。
您可以使用 -Configuration 參數來套用自訂群組態。 Github 上提供自訂範例。
附註。 在第一個公開預覽中 ,Invoke-SqlAssessment 分別傳回具有 Check 識別碼和 DisplayName 屬性 CheckId 和 CheckName 的 AssessmentNote 物件。 在第二個公開預覽中,兩個屬性已取代為單一 Check 屬性,以提供更多資料。 假設$note是 Invoke-SqlAssessment傳回的物件,您可以存取檢查的識別碼為 $note.Check.Id,而不是$note。CheckId 或檢查的描述為 $note。Check.Description。 您可以使用 -FlattenOutput 參數,使用 CheckId 和 CheckName 取得先前格式的結果。 此參數也有助於保留某些 Cmdlet 的相容性,例如 Write-SqlTableData。 如需詳細資訊,請參閱範例 12-14。
Invoke-SqlAssessment Cmdlet 的輸出是每個指定SQL Server物件的違反最佳做法清單。 使用 Description 屬性來瞭解最佳做法和 Message 屬性,以瞭解如何解決。 此外,每個檢查結果都包含線上檔的連結,這可協助您更妥善地找出問題。
Azure VM 上的SQL Server支援
透過 SQL 評定 Cmdlet,您不僅可以將 Azure VM 上的SQL Server實例評估為內部部署SQL Server,也可以評估 Azure VM 上SQL Server專屬的規則, (使用虛擬機器組態) 的相關資訊。 例如,AzSqlVmSize 規則會檢查裝載 Azure VM 上SQL Server實例的 VM 是否為建議的大小。
若要使用這類規則,請使用Azure PowerShell Module連線到 Azure,並確定已安裝Az.ResourceGraph模組。
在對 Azure VM 實例上的SQL Server叫用 SQL 評定之前,請使用 Azure PowerShell 登入。 範例 16 顯示互動式登入程式和訂用帳戶選取專案。
附註。 您可以使用在 PowerShell 會話之間保存的 Azure 帳戶連線,也就是在一個會話中叫用Connect-AzAccount,並在稍後省略此命令。 不過,目前版本的 SQL 評定 Cmdlet 需要明確匯入 Az.ResourceGraph 模組:Import-Module Az.ResourceGraph
範例
範例 1:針對本機預設實例叫用評估
PS:> Get-SqlInstance -ServerInstance localhost | Invoke-SqlAssessment
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Info Enable trace flag 834 to use large-page allocations to improve TF834 Microsoft Ruleset 0.1.202
analytical and data warehousing workloads.
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
Medi Amount of single use plans in cache is high (100%). Consider PlansUseRatio Microsoft Ruleset 0.1.202
enabling the Optimize for ad hoc workloads setting on heavy OLTP
ad-hoc workloads to conserve resources.
...
此範例示範如何取得目前電腦上執行之預設實例之SQL Server的所有最佳做法建議。
範例 2:使用 PSProvider Cmdlet 叫用評定
PS:> Get-Item SQLSERVER:\SQL\localhost\default | Invoke-SqlAssessment
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Info Enable trace flag 834 to use large-page allocations to improve TF834 Microsoft Ruleset 0.1.202
analytical and data warehousing workloads.
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
Medi Amount of single use plans in cache is high (100%). Consider PlansUseRatio Microsoft Ruleset 0.1.202
enabling the Optimize for ad hoc workloads setting on heavy OLTP
ad-hoc workloads to conserve resources.
...
此範例示範如何取得預設實例的所有最佳做法建議SQL Server。
範例 3:使用 PS 提供者路徑叫用評定
PS:> Invoke-SqlAssessment SQLSERVER:\SQL\localhost\default
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Info Enable trace flag 834 to use large-page allocations to improve TF834 Microsoft Ruleset 0.1.202
analytical and data warehousing workloads.
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
Medi Amount of single use plans in cache is high (100%). Consider PlansUseRatio Microsoft Ruleset 0.1.202
enabling the Optimize for ad hoc workloads setting on heavy OLTP
ad-hoc workloads to conserve resources.
...
此範例示範如何取得預設實例的所有最佳做法建議SQL Server。
範例 4:使用自訂群組態叫用評量
PS:> Get-SqlInstance -ServerInstance '(local)' | Invoke-SqlAssessment -Configuration C:\profileA.json, C:\profileB.json
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
Medi A custom rule violation detected. CustomRuleA Profile A 1.0
...
此範例示範如何套用自訂群組態,以取得一組已修改的最佳做法建議。 自訂群組態會在 JSON 檔案中描述。 自訂規則集 profileA.json 和 profileB.json 已停用一些檢查,並引進一些新的檢查。 profileA.json 的其中一個新檢查偵測到SQL Server實例的目前組態發生問題。 請流覽 Github 上的 SQL 評定範例資料夾 ,以瞭解如何進行自訂。
範例 5:叫用所有實例的評估
PS:> dir SQLSERVER:\SQL\localhost | Invoke-SqlAssessment
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Info Enable trace flag 834 to use large-page allocations to improve TF834 Microsoft Ruleset 0.1.202
analytical and data warehousing workloads.
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
Medi Amount of single use plans in cache is high (100%). Consider PlansUseRatio Microsoft Ruleset 0.1.202
enabling the Optimize for ad hoc workloads setting on heavy OLTP
ad-hoc workloads to conserve resources.
TargetPath : Server[@Name='LOCAL\INSTANCE1']
Sev. Message Check ID Origin
---- ------- -------- ------
Medi Product version 14.0.1000 is not the latest available. Keep your LatestCU Microsoft Ruleset 0.1.202
your SQL Server up to date and install Service Packs and
Cumulative Updates as they are released.
...
此範例顯示Invoke-SqlAssessment Cmdlet 透過管線接受一組SQL Server實例。
範例 6:執行篩選集 ot traget 的評定
PS:> Get-SqlInstance -ServerInstance . | Where { $_.Name -Match '.*\d+' } | Invoke-SqlAssessment
TargetPath : Server[@Name='LOCAL\INSTANCE1']
Sev. Message Check ID Origin
---- ------- -------- ------
Medi Product version 14.0.1000 is not the latest available. Keep your LatestCU Microsoft Ruleset 0.1.202
your SQL Server up to date and install Service Packs and
Cumulative Updates as they are released.
...
此範例顯示Invoke-SqlAssessment Cmdlet 透過管線接受一組SQL Server實例。 此集合會使用標準 PowerShell Where-Object Cmdlet 進行篩選。
範例 7:依名稱叫用資料庫的評量
PS:> Get-SqlDatabase master -ServerInstance localhost | Invoke-SqlAssessment -Verbose
VERBOSE: Base ruleset version: 0.2.
VERBOSE: No recommendations for [master].
此範例顯示 Invoke-SqlAssessment Cmdlet 接受資料庫的名稱。 在此情況下,找不到問題。
範例 8:依路徑叫用資料庫的評估
PS:> Invoke-SqlAssessment SQLSERVER:\SQL\localhost\default\Databases\master -Verbose
VERBOSE: Base ruleset version: 0.2.
VERBOSE: No recommendations for [master].
此範例顯示Invoke-SqlAssessment Cmdlet 接受SQL Server資料庫的路徑。
範例 9:偵測資料庫的高問題
PS:> cd SQLSERVER:\SQL\localhost\default\Databases\master
PS:> Invoke-SqlAssessment -MinSeverity High
VERBOSE: Base ruleset version: 0.2.
VERBOSE: No recommendations for [master].
此範例顯示 Invoke-SqlAssessment Cmdlet 評估目前的位置。 只會報告高問題。
範例 10:執行依標籤選取的檢查
PS:> Get-SqlInstance -ServerInstance . | Invoke-SqlAssessment -Check Backup -Verbose
VERBOSE: Base ruleset version: 0.2.
VERBOSE: No recommendations for [LOCAL].
此範例顯示Invoke-SqlAssessment Cmdlet,執行本機伺服器上每個SQL Server實例的所有備份相關檢查。
範例 11:以互動方式執行選取的檢查
PS:> $serverInstance = Get-SqlInstance -ServerInstance '(local)'
PS:> $checks = Get-SqlAssessmentItem $serverInstance | Select Name, Description | Out-GridView -PassThru
PS:> Invoke-SqlAssessment $serverInstance -Check $checks
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Info Enable trace flag 834 to use large-page allocations to improve TF834 Microsoft Ruleset 0.1.202
analytical and data warehousing workloads.
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
此範例的第二行顯示取得$serverInstance的檢查,並以互動方式選取其中一些。 選取的專案會儲存在陣列變數中,然後做為 Invoke-SqlAssessment Cmdlet 的輸入。 只有在評估程式期間才會執行挑選的檢查。
範例 12:-FlattenOutput 參數的效果
PS> $inst = Get-SqlInstance -ServerInstance .
PS> Invoke-SqlAssessment $inst -FlattenOutput | Select -First 1 | Get-Member
TypeName: Microsoft.SqlServer.Management.Assessment.Cmdlets.AssessmentNoteFlat
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CheckId Property string CheckId {get;}
CheckName Property string CheckName {get;}
HelpLink Property string HelpLink {get;}
Message Property string Message {get;}
RulesetName Property string RulesetName {get;}
RulesetVersion Property string RulesetVersion {get;}
Severity Property string Severity {get;}
TargetPath Property string TargetPath {get;}
TargetType Property string TargetType {get;}
PS> Invoke-SqlAssessment $inst | Select -First 1 | Get-Member
TypeName: Microsoft.SqlServer.Management.Assessment.Cmdlets.AssessmentNote
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Check Property Microsoft.SqlServer.Management.Assessment.Checks.ICheck Check {get;}
HelpLink Property string HelpLink {get;}
Message Property string Message {get;}
Severity Property Microsoft.SqlServer.Management.Assessment.SeverityLevel Severity {get;}
TargetPath Property string TargetPath {get;}
TargetType Property string TargetType {get;}
PS> (Invoke-SqlAssessment $inst | Select -First 1).Check | Get-Member
TypeName: Microsoft.SqlServer.Management.Assessment.Checks.Check
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Condition Property Microsoft.SqlServer.Management.Assessment....
Description Property string Description {get;set;}
DisplayName Property string DisplayName {get;set;}
Enabled Property bool Enabled {get;set;}
HelpLink Property string HelpLink {get;set;}
Id Property string Id {get;set;}
Level Property Microsoft.SqlServer.Management.Assessment....
Message Property string Message {get;set;}
OriginName Property string OriginName {get;set;}
OriginVersion Property version OriginVersion {get;set;}
Parameters Property System.Collections.Generic.IDictionary[str...
Probes Property System.Collections.Generic.List[Microsoft....
Tags Property System.Collections.Generic.HashSet[string]...
Target Property Microsoft.SqlServer.Management.Assessment....
此範例顯示使用 或不含 -FlattenOutput 參數傳回的物件之間的差異。 參數會以兩個字串屬性 CheckId 和 CheckName 取代龐大的複雜 Check 物件。 這適用于序列化用途, (請參閱下一個範例) 。
第一個命令會顯示具有字串類型所有屬性的簡單物件。
第二個命令會顯示另一個具有兩個非字串屬性的物件:檢查和嚴重性。
第三個命令會顯示可透過 Check 屬性存取的豐富資料集。
範例 13:再次執行失敗的檢查
PS> $inst = Get-SqlInstance -ServerInstance .
PS> $results = Invoke-SqlAssessment $inst
PS> $results
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Info Enable trace flag 834 to use large-page allocations to improve TF834 Microsoft Ruleset 0.1.202
analytical and data warehousing workloads.
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
Medi Amount of single use plans in cache is high (100%). Consider PlansUseRatio Microsoft Ruleset 0.1.202
enabling the Optimize for ad hoc workloads setting on heavy OLTP
ad-hoc workloads to conserve resources.
PS> $results[1].Check.Description
This check detects deprecated or discontinued features used on target SQL Server instance. Deprecated features may be
removed in a future release of SQL Server. Discontinued features have been removed from specific versions of SQL Server.
PS> Invoke-SqlAssessment $inst -Check $results[1].Check
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
PS> Invoke-SqlAssessment $inst -Check ($results).Check
TargetPath : Server[@Name='LOCAL']
Sev. Message Check ID Origin
---- ------- -------- ------
Info Enable trace flag 834 to use large-page allocations to improve TF834 Microsoft Ruleset 0.1.202
analytical and data warehousing workloads.
Low Detected deprecated or discontinued feature uses: String literals DeprecatedFeatures Microsoft Ruleset 0.1.202
as column aliases, syscolumns, sysusers, SET FMTONLY ON, XP_API,
Table hint without WITH, More than two-part column name. We
recommend to replace them with features actual for SQL Server
version 14.0.1000.
Medi Amount of single use plans in cache is high (100%). Consider PlansUseRatio Microsoft Ruleset 0.1.202
enabling the Optimize for ad hoc workloads setting on heavy OLTP
ad-hoc workloads to conserve resources.
此範例示範如何使用評量結果傳回的 Check 重複使用。 您可以重新執行先前產生的 AssessmentNotes 檢查。
範例 14:將評定結果儲存至資料表
PS:> $serverInstance = Get-SqlInstance -ServerInstance '(local)'
PS:> Invoke-SqlAssessment $serverInstance -FlattenOutput |
Write-SqlTableData -ServerInstance localhost -DatabaseName SqlAssessment -SchemaName Assessment -TableName Results -Force
此範例的第二行示範如何使用 -FlattenOutput 參數搭配 Write-SqlTableData Cmdlet,將評定結果儲存至 SQL 資料庫。
範例 15:明確指定認證
PS> $cred = Get-Credential
PowerShell credential request
Enter your credentials.
User: Administrator
Password for user Administrator: ********
PS> $inst = Get-SqlInstance -ServerInstance 10.0.3.118 -Credential $cred
PS> Invoke-SqlAssessment $inst
TargetPath : Server[@Name='ContosSQL']
Sev. Message Check ID Origin
---- ------- -------- ------
Medi Amount of single use plans in cache is high (100%). Consider PlansUseRatio Microsoft Ruleset 0.1.202
enabling the Optimize for ad hoc workloads setting on heavy OLTP
ad-hoc workloads to conserve resources
此範例示範如何使用明確指定的認證來叫用 SQL 評定。
範例 16:在 Azure VM 實例上叫用SQL Server評估
PS> Connect-AzAccount
PS> Set-Subscription My-Pay-As-You-Go
PS> $cred = Get-Credential
PowerShell credential request
Enter your credentials.
User: Administrator
Password for user Administrator: ********
PS> $inst = Get-SqlInstance -ServerInstance 10.0.3.118 -Credential $cred
PS> Invoke-SqlAssessment $inst
TargetPath : Server[@Name='ContosoAzureSQL']
Sev. Message Check ID Origin
---- ------- -------- ------
Medi Amount of single use plans in cache is high (100%). Consider PlansUseRatio Microsoft Ruleset 0.1.202
enabling the Optimize for ad hoc workloads setting on heavy OLTP
ad-hoc workloads to conserve resources
Info Use memory optimized virtual machine sizes for the best AzSqlVmSize Microsoft Ruleset 0.1.202
performance of SQL Server workloads
此範例示範如何在 Azure VM 實例上叫用SQL Server評估。
使用中的 Azure 訂用帳戶連線可在 Azure VM 相關檢查上啟用SQL Server, (本範例中的 AzSqlVmSize) 。 第一行會連線到 Azure 帳戶,以從 Azure Resource Graph取得資料。 第二行是選擇性的。
若要執行這些檢查,SQL 評定需要 Az.ResourceGraph 模組。
參數
-Check
一或多個檢查、檢查識別碼或標記。
針對每個檢查物件, Invoke-SqlAssessment 會執行,以檢查它是否支援輸入物件。
針對每個檢查識別碼, Invoke-SqlAssessment 會在支援輸入物件時執行對應的檢查。
針對標籤, Invoke-SqlAssessment 會使用任何這些標記來執行檢查。
Type: | Object[] |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Configuration
指定包含自訂群組態的檔案路徑。 自訂檔案會依指定順序套用至預設組態。 範圍僅限於此 Cmdlet 調用。
Type: | PSObject |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-FlattenOutput
指出此 Cmdlet 會產生 Microsoft.SqlServer.Management.Assessment.Cmdlets.AssessmentNoteFlat 類型的簡單物件,而不是 Microsoft.SqlServer.Management.Assessment.Cmdlets.AssessmentNote。
從 Invoke-SqlAssessment 傳回的一般 AssessmentNote 包含其他實用複雜物件的參考,例如 Check (請參閱範例 12) 。 使用 Check 屬性,您可以取得檢查的描述,或重複使用檢查 (請參閱範例 13) 。 但某些 Cmdlet 不支援複雜的屬性。 例如,嘗試將 AssessmentNote 寫入資料庫時,Write-SqlTableData會引發錯誤。 若要避免這種情況,您可以使用 -FlattenOutput 參數,將 Check 屬性取代為兩個簡單的字串:CheckId 和 CheckName (請參閱範例 14) 。
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-InputObject
指定SQL Server物件或這類物件的路徑。 Cmdlet 會執行此物件的評估。 省略此參數時,會使用目前的位置做為輸入物件。 如果目前位置不是支援的 SQL Server 物件,Cmdlet 會發出錯誤訊號。
Type: | PSObject |
Aliases: | Target |
Position: | 10 |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-MinSeverity
指定要找到檢查的最小嚴重性層級。 例如,當 -MinSeverity High 時,將不會傳回低、中或資訊層級的檢查。
Type: | SeverityLevel |
Aliases: | Severity |
Accepted values: | Information, Low, Medium, High |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
輸入
System.String[]
Microsoft.SqlServer.Management.Smo.SqlSmoObject[]
輸出
Microsoft.SqlServer.Assessment.Cmdlets.AssessmentNote