共用方式為


Get-SqlAssessmentItem

取得適用於所選 SQL Server 物件的 SQL 評定最佳做法檢查。

語法

Get-SqlAssessmentItem
   [-Check <String[]>]
   [[-InputObject] <PSObject>]
   [-Configuration <PSObject>]
   [-MinSeverity <SeverityLevel>]
   [-FlattenOutput]
   [-ProgressAction <ActionPreference>]
   [<CommonParameters>]

Description

Get-SqlAssessmentItem 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 版本、平台和物件類型而異。 此外,也會檢查目標為特定資料庫,例如 tempdbmaster。 您也可以使用參數 -MinSeverity 和 -Check,依標記、名稱和嚴重性來篩選檢查。

透過 Get-SqlAssessmentItem Cmdlet,您可以取得適用於指定 SQL Server 對象的檢查清單。 此外,您可以使用此 Cmdlet 的輸出作為 Invoke-SqlAssessment Cmdlet 的 -Check 自變數。

您可以使用 -Configuration 參數來套用自定義組態。 自定義範例 Github提供。

Azure VM 上的 SQL Server 支援

透過 SQL 評定 Cmdlet,您可以評估 Azure VM 上的 SQL Server 實例,不僅作為內部部署 SQL Server,還可以評估 Azure VM 上 SQL Server 特有的規則(使用虛擬機設定的相關信息)。 例如,AzSqlVmSize 規則會檢查裝載 Azure VM 上 SQL Server 實例的 VM 是否為建議大小。

若要使用這類規則,請使用 Azure PowerShell 模組 連線到 Azure,並確定已安裝 Az.ResourceGraph 模組。

使用 Azure PowerShell 登入,再對 Azure VM 實例上的 SQL Server 叫用 SQL 評定。 範例 13 顯示互動式登錄程式和訂用帳戶選取專案。

注意。 您可以使用PowerShell會話之間保存的 Azure 帳戶連線,也就是在一個工作階段中叫用 Connect-AzAccount,稍後省略此命令。 不過,目前版本的 SQL 評定 Cmdlet 需要在此案例中明確匯入 Az.ResourceGraph 模組:Import-Module Az.ResourceGraph

範例

範例 1:取得本機預設實例的檢查

PS:> Get-SqlInstance -ServerInstance 'localhost' | Get-SqlAssessmentItem

   Target: [LOCAL]

ID                             ON      Name                                     Origin                                  
--                             --      ----                                     ------                                  
TF1204                         True    TF 1204 returns deadlock information     Microsoft Ruleset 0.1.202               
BlackboxTrace                  True    Blackbox trace is configured and running Microsoft Ruleset 0.1.202               
HintsStatistics                True    Hints are being used                     Microsoft Ruleset 0.1.202               
PlansUseRatio                  True    Amount of single use plans in cache i... Microsoft Ruleset 0.1.202            
TempDBFilesAutoGrowth          True    Some TempDB data files have different... Microsoft Ruleset 0.1.202             
CpuUtil90                      True    CPU usage over 90%                       Microsoft Ruleset 0.1.202  
...

此範例會取得目前計算機上執行之 SQL Server 預設實例的所有可用檢查。

範例 2:使用 Get-Item Cmdlet 取得檢查

PS:> Get-Item SQLSERVER:\SQL\localhost\default | Get-SqlAssessmentItem

   Target: [LOCAL]

ID                             ON      Name                                     Origin                                  
--                             --      ----                                     ------                                  
TF1204                         True    TF 1204 returns deadlock information     Microsoft Ruleset 0.1.202               
BlackboxTrace                  True    Blackbox trace is configured and running Microsoft Ruleset 0.1.202               
HintsStatistics                True    Hints are being used                     Microsoft Ruleset 0.1.202               
PlansUseRatio                  True    Amount of single use plans in cache i... Microsoft Ruleset 0.1.202               
TempDBFilesAutoGrowth          True    Some TempDB data files have different... Microsoft Ruleset 0.1.202               
CpuUtil90                      True    CPU usage over 90%                       Microsoft Ruleset 0.1.202  
...

此範例會取得目前計算機上執行之 SQL Server 預設實例的所有可用檢查。

範例 3:取得目標物件路徑的檢查

PS:> Get-SqlAssessmentItem SQLSERVER:\SQL\localhost\default

   Target: [LOCAL]

ID                             ON      Name                                     Origin                                  
--                             --      ----                                     ------                                  
TF1204                         True    TF 1204 returns deadlock information     Microsoft Ruleset 0.1.202                   
BlackboxTrace                  True    Blackbox trace is configured and running Microsoft Ruleset 0.1.202                   
HintsStatistics                True    Hints are being used                     Microsoft Ruleset 0.1.202                   
PlansUseRatio                  True    Amount of single use plans in cache i... Microsoft Ruleset 0.1.202                   
TempDBFilesAutoGrowth          True    Some TempDB data files have different... Microsoft Ruleset 0.1.202                   
CpuUtil90                      True    CPU usage over 90%                       Microsoft Ruleset 0.1.202  
...

此範例會取得目前計算機上執行之 SQL Server 預設實例的所有可用檢查。

範例 4:取得已套用自定義組態的檢查

PS:> Get-SqlDatabase master -ServerInstance . |
     Get-SqlAssessmentItem -Configuration C:\rulesetA.json, D:\rulesetB.json

   Target: [LOCAL]

ID                             ON      Name                                     Origin                                  
--                             --      ----                                     ------                                  
TF1204                         False   TF 1204 returns deadlock information     Microsoft Ruleset 0.1.202                   
BlackboxTrace                  True    Blackbox trace is configured and running Microsoft Ruleset 0.1.202                   
HintsStatistics                True    Hints are being used                     Microsoft Ruleset 0.1.202                   
PlansUseRatio                  True    Amount of single use plans in cache i... Microsoft Ruleset 0.1.202                   
TempDBFilesAutoGrowth          False   Some TempDB data files have different... Microsoft Ruleset 0.1.202                   
CpuUtil90                      True    CPU usage over 90%                       Microsoft Ruleset 0.1.202                   
SomeCustomCheck                True    Some custom check                        Ruleset A 1.0                           
AnotherCustomCheck             True    Another custom check                     Ruleset B 1.0
...

此範例會取得從指定 JSON 檔案取得之套用自定義組態的所有可用檢查。 請流覽 Github 上的 SQL 評定範例資料夾,以瞭解如何進行自定義。

範例 5:取得 localhost 上所有實例的檢查

PS:> Get-SqlInstance -ServerInstance localhost | Get-SqlAssessmentItem

   Target: [LOCAL]

ID                             ON      Name                                     Origin                                  
--                             --      ----                                     ------                                  
TF1204                         True    TF 1204 returns deadlock information     Microsoft Ruleset 0.1.202               
BlackboxTrace                  True    Blackbox trace is configured and running Microsoft Ruleset 0.1.202               
CpuUtil90                      True    CPU usage over 90%                       Microsoft Ruleset 0.1.202

   Target: [LOCAL\INSTANCE1]

ID                             ON      Name                                     Origin                                  
--                             --      ----                                     ------                                  
HintsStatistics                True    Hints are being used                     Microsoft Ruleset 0.1.202             
PlansUseRatio                  True    Amount of single use plans in cache i... Microsoft Ruleset 0.1.202               
TempDBFilesAutoGrowth          True    Some TempDB data files have different... Microsoft Ruleset 0.1.202               
CpuUtil90                      True    CPU usage over 90%                       Microsoft Ruleset 0.1.202
...

此範例示範 Get-SqlAssessmentItem Cmdlet 透過管線接受一組 SQL Server 實例。

範例 6:取得所有名稱以數位結尾的實例檢查

PS:> Get-SqlInstance -ServerInstance localhost | Where { $_.Name -Match '.*\d+' } | Get-SqlAssessmentItem

   Target: [LOCAL\INSTANCE1]

ID                             ON      Name                                     Origin                                  
--                             --      ----                                     ------                                  
HintsStatistics                True    Hints are being used                     Microsoft Ruleset 0.1.202               
PlansUseRatio                  True    Amount of single use plans in cache i... Microsoft Ruleset 0.1.202             
TempDBFilesAutoGrowth          True    Some TempDB data files have different... Microsoft Ruleset 0.1.202               
CpuUtil90                      True    CPU usage over 90%                       Microsoft Ruleset 0.1.202
...

此範例示範 Get-SqlAssessmentItem Cmdlet 透過管線接受一組 SQL Server 實例。 只會處理名稱結尾為數字的實例。

範例 7:依路徑取得資料庫檢查

PS:> Get-SqlAssessmentItem SQLSERVER:\SQL\localhost\default\Databases\master


   TargetObject: [master]

ID                             ON      Name                                     Origin                    
--                             --      ----                                     ------                    
AutoCreateStats                True    Auto-Create Statistics should be on      Microsoft Ruleset 0.1.202 
HintsUsageInModules            False   Hints usage in modules                   Microsoft Ruleset 0.1.202 
FullBackup                     True    Full backup is missed or outdated        Microsoft Ruleset 0.1.202 
DuplicateIndexes               True    Duplicate Indexes                        Microsoft Ruleset 0.1.202 
RedundantIndexes               True    Redundant Indexes                        Microsoft Ruleset 0.1.202 
...

此範例示範 Get-SqlAssessmentItem Cmdlet 接受 SQL Server 資料庫的路徑。

範例 8:取得資料庫的高嚴重性檢查

PS:> cd SQLSERVER:\SQL\localhost\default\Databases\master
PS:> Get-SqlAssessmentItem -MinSeverity High

此範例示範 Get-SqlAssessmentItem 傳回 master 資料庫嚴重性較高的可用檢查。 它會接受目前的PowerShell提供者位置作為目標。

範例 9:取得資料庫的高嚴重性檢查

PS:> $db = Get-SqlDatabase master -ServerInstance localhost
PS:> Get-SqlAssessmentItem $db -MinSeverity High

此範例示範 Get-SqlAssessmentItem 傳回 master 資料庫嚴重性較高的可用檢查。

範例 10:依標記取得檢查

PS:> Get-SqlDatabase -ServerInstance . | Get-SqlAssessmentItem -Check Backup


   TargetObject: [master]

ID                             ON      Name                                     Origin                    
--                             --      ----                                     ------                    
FullBackup                     True    Full backup is missed or outdated        Microsoft Ruleset 0.1.202 


   TargetObject: [msdb]

ID                             ON      Name                                     Origin                    
--                             --      ----                                     ------                    
FullBackup                     True    Full backup is missed or outdated        Microsoft Ruleset 0.1.202

此範例示範 Get-SqlAssessmentItem Cmdlet 傳回預設本機 SQL Server 實例上所有資料庫的所有備份相關檢查。

範例 11:以互動方式執行選取的檢查

PS:> $serverInstance = Get-SqlInstance -ServerInstance '(local)'
PS:> $checks = Get-SqlAssessmentItem $serverInstance | Select Id, 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:明確指定認證

PS> $cred = Get-Credential

PowerShell credential request
Enter your credentials.
User: Administrator
Password for user Administrator: ********

PS> $db = Get-SqlDatabase master -ServerInstance 10.0.3.118 -Credential $cred
PS> Get-SqlAssessmentItem $db

   TargetObject: [master]

ID                             ON      Name                                     Origin                    
--                             --      ----                                     ------                    
AutoCreateStats                True    Auto-Create Statistics should be on      Microsoft Ruleset 0.1.202 
FullBackup                     True    Full backup is missed or outdated        Microsoft Ruleset 0.1.202 
DuplicateIndexes               True    Duplicate Indexes                        Microsoft Ruleset 0.1.202 
RedundantIndexes               True    Redundant Indexes                        Microsoft Ruleset 0.1.202 
...

此範例示範如何取得具有明確指定認證的 SQL 評定檢查清單。

範例 13:取得 Azure VM 實例上 SQL Server 的 SQL 評定規則清單

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> Get-SqlAssessmentItem $inst

   TargetObject: [ContosoAzureSql]

ID                             ON      Name                                     Origin                    
--                             --      ----                                     ------                    
HintsStatistics                True    Hints are being used                     Microsoft Ruleset 0.1.202               
PlansUseRatio                  True    Amount of single use plans in cache i... Microsoft Ruleset 0.1.202             
TempDBFilesAutoGrowth          True    Some TempDB data files have different... Microsoft Ruleset 0.1.202 
AzSqlVmSize                    True    VM size is not memory-optimized          Microsoft Ruleset 0.1.202 
...

此範例示範如何取得適用於 Azure VM 實例上特定 SQL Server 的規則清單。

作用中的 Azure 訂用帳戶聯機會啟用 Azure 相關檢查(在此範例中為 AzSqlVmSize)。 第一行會連線到 Azure 帳戶,以從 Azure Resource Graph 取得數據。 第二行是選擇性的。

若要執行這些檢查,SQL 評定需要 Az.ResourceGraph 模組。

參數

-Check

一或多個檢查、檢查標識碼或標記。

針對每個檢查物件,Get-SqlAssessmentItem 會傳回該檢查是否支援輸入物件。

針對每個檢查標識碼,Get-SqlAssessmentItem 會傳回對應的檢查,如果支援輸入物件。

針對標籤,Get-SqlAssessmentItem 會傳回任何標記的檢查。

類型:String[]
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-Configuration

指定包含自定義組態的檔案路徑。 自定義檔案會依指定順序套用至預設組態。 範圍僅限於此 Cmdlet 調用。

類型:PSObject
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-FlattenOutput

指出此 Cmdlet 會產生類型為 Microsoft.SqlServer.Management.Assessment.Cmdlets.AssessmentNoteFlat 的簡單物件,而不是 Microsoft.SqlServer.Management.Assessment.Cmdlets.AssessmentNote

類型:SwitchParameter
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-InputObject

指定 SQL Server 物件或這類物件的路徑。 Cmdlet 會傳回此對象的適當檢查。 省略此參數時,目前的位置會當做輸入物件使用。 如果目前位置不是支援的 SQL Server 物件,Cmdlet 會發出錯誤訊號。

類型:PSObject
別名:Target
Position:10
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-MinSeverity

指定要尋找檢查的最低嚴重性層級。 例如,-MinSeverity High 時,將不會傳回中、低或資訊層級的檢查。

類型:SeverityLevel
別名:Severity
接受的值:Information, Low, Medium, High
Position:Named
預設值:Information
必要:False
接受管線輸入:False
接受萬用字元:False

-ProgressAction

決定 PowerShell 如何回應腳本、Cmdlet 或提供者所產生的進度更新,例如 Write-Progress Cmdlet 所產生的進度列。 Write-Progress Cmdlet 會建立顯示命令狀態的進度列。

類型:ActionPreference
別名:proga
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

輸入

System.String[]

Microsoft.SqlServer.Management.Smo.SqlSmoObject[]

輸出

Microsoft.SqlServer.Management.Assessment.ICheck