Get-SqlAssessmentItem
取得 SQL 評定最佳做法檢查,以用於所選SQL Server物件。
Syntax
Get-SqlAssessmentItem
[-Check <String[]>]
[[-InputObject] <PSObject>]
[-Configuration <PSObject>]
[-MinSeverity <SeverityLevel>]
[-FlattenOutput]
[<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
Get-Item 和 Get-ChildItem 等 Cmdlet。 此外,Cmdlet 也支援SQL Server PowerShell 提供者,因此它可以從其路徑取得物件。 路徑可以明確傳遞,否則會使用目前的路徑。
所選物件的檢查可用性會因SQL Server版本、平臺和物件類型而異。 此外,也會檢查是否以 或 master
之類的 tempdb
特定資料庫為目標。 您可以使用 -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 Module連線到 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
...
此範例示範透過管線接受一組SQL Server實例的Get-SqlAssessmentItem Cmdlet。
範例 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
...
此範例示範透過管線接受一組SQL Server實例的Get-SqlAssessmentItem Cmdlet。 只會處理名稱結尾為數字的實例。
範例 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 會傳回與其中任何標記的檢查。
Type: | String[] |
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。
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: | Information |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
輸入
System.String[]
Microsoft.SqlServer.Management.Smo.SqlSmoObject[]
輸出
Microsoft.SqlServer.Management.Assessment.ICheck