Get-SqlAssessmentItem
获取可用于所选SQL Server对象的 SQL 评估最佳做法检查。
语法
Get-SqlAssessmentItem
[-Check <String[]>]
[[-InputObject] <PSObject>]
[-Configuration <PSObject>]
[-MinSeverity <SeverityLevel>]
[-FlattenOutput]
[<CommonParameters>]
说明
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 模块连接到 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
...
此示例演示接受SQL Server数据库路径的 Get-SqlAssessmentItem cmdlet。
示例 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,该 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 订阅连接 (AzSqlVmSize 启用 Azure 相关检查) 。 第一行连接到 Azure 帐户,以从 Azure Resource Graph获取数据。 第二行是可选的。
若要运行这些检查,SQL 评估需要 Az.ResourceGraph 模块。
参数
-Check
一个或多个检查、检查 ID 或标记。
对于每个检查对象,如果 Get-SqlAssessmentItem 支持输入对象,则返回该检查。
对于每个检查 ID,如果 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