Invoke-SqlAssessment

Runs SQL Assessment best practice checks for a chosen SQL Server object and returns their results.

Syntax

Invoke-SqlAssessment
      [-Check <Object[]>]
      [[-InputObject] <PSObject>]
      [-Configuration <PSObject>]
      [-MinSeverity <SeverityLevel>]
      [<CommonParameters>]

Description

The Invoke-SqlAssessment cmdlet runs an assessment for each input object and returns a list of best practice recommendations that should be applied to the specified objects. It's up to you to follow the given recommendations or not. Learn more about the SQL Assessment API on its Microsoft Docs page.

This cmdlet accepts the following input types:

  • Microsoft.SqlServer.Management.Smo.Server
  • Microsoft.SqlServer.Management.Smo.Database
  • Microsoft.SqlServer.Management.Smo.AvailabilityGroup
  • Microsoft.SqlServer.Management.Smo.FileGroup
  • String containing path to any object of the above types
  • Collection of objects

You can get input objects with SqlServer cmdlets like Get-SqlInstance and Get-SqlDatabase or basic PowerShell cmdlets like Get-Item and Get-ChildItem. Also, the cmdlet supports the SQL Server PowerShell provider, so it can obtain an object from its path. The path can be passed explicitly, otherwise the current path will be used.

Availability of a check for a chosen object varies on SQL Server version, platform, object type, and name. And besides, you can additionally filter checks with the parameters -MinSeverity and -Check.

You can get a list of checks applicable to the given SQL Server object with Get-SqlAssessmentItem cmdlet. Also, you can use this cmdlet's output as input for Invoke-SqlAssessment cmdlet.

The cmdlet runs only checks that are applicable to an input object. For example, database checks will not be run for a SQL Server instance or an availability group, even when specified in -Check list.

Custom configurations can be applied with the -Configuration parameter. Customization examples are available on Github.

Invoke-SqlAssessment cmdlet's output is a list of violated best practices for every given SQL Server object. Use Description property to learn about the best practice and Message property to find out how it can be solved. Also, every check result contains a link to online documentation, which will help you figure out the issue better.

Examples

Example 1: Invoke assessment for local default instance

PS:> Get-SqlInstance -ServerInstance 'localhost' | Invoke-SqlAssessment

TargetPath : Server[@Name='LOCAL']
HelpLink   : https://support.microsoft.com/help/321185/how-to-determine-the-version-edition-and-update-level-of-sql-server-an
             https://docs.microsoft.com/sql/database-engine/install-windows/install-sql-server-servicing-updates
Severity   : Warning
CheckName  : SQL Server instance is not up to date
CheckId    : SqlServer.Server.LatestCUInstalled
Message    : Product version 14.0.2014 is not the latest available. We recommend keeping your SQL Server up to date and install Service
             Packs and Cumulative Updates as they are released.
TargetType : Server

This example shows how to get all best practice recommendations for the default instance of SQL Server running on the current machine.

Example 2: Invoke assessment for local default instance

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

This example shows how to get all best practice recommendations for the default instance of SQL Server.

Example 3: Invoke assessment for an obect specified by path

PS:> Invoke-SqlAssessment SQLSERVER:\SQL\localhost\default

This example shows how to get all best practice recommendations for the default instance of SQL Server.

Example 4: Invoke assessment with custom configuration

PS:> cd SQLSERVER:\SQL\localhost
PS:> Get-Item default | Invoke-SqlAssessment -Configuration C:\SqlServerAssessment\profileA.json, C:\SqlServerAssessment\profileB.json

This example shows how to apply custom configuration to get a modified set of best practice recommendations. Custom configurations are described in JSON files. Visit SQL Assessment samples folder on Github to find out how to make customization.

Example 5: Invoke assessment for all instances on localhost

PS:> dir SQLSERVER:\SQL\localhost | Invoke-SqlAssessment

This example shows Invoke-SqlAssessment cmdlet accepting a set of SQL Server instances via pipeline.

Example 6: Invoke assessment for all instances with names ending with numbers

PS:> dir SQLSERVER:\SQL\localhost | Where { $_.Name -Match '.*\d+' } | Invoke-SqlAssessment

This example shows Invoke-SqlAssessment cmdlet accepting a set of SQL Server instances via pipeline. The set is filtered with the standard PowerShell facilities.

Example 7: Invoke assessment for a database by path

PS:> Invoke-SqlAssessment SQLSERVER:\SQL\localhost\default\Databases\master

This example shows Invoke-SqlAssessment cmdlet accepting the path to a SQL Server database.

Example 8: Detect critical issues for a database

PS:> cd SQLSERVER:\SQL\localhost\default\Databases\master
PS:> Invoke-SqlAssessment -MinSeverity Critical

This example shows Invoke-SqlAssessment cmdlet assessing the current location. Only critical issues are reported.

Example 9: Invoke assessment for all databases on local instance

PS:> Get-SqlDatabase -ServerInstance 'localhost\instance1' | Invoke-SqlAssessment

This example invokes assessment for all databases returned by Get-SqlDatabase cmdlet.

Example 10: Get checks by tag

PS:> Get-SqlInstance -ServerInstance . | Invoke-SqlAssessment -Check Backup -Verbose

This example shows Invoke-SqlAssessment cmdlet running all backup-related checks for every SQL Server instance on the local server.

Example 11: Get recommendations for interactively selected checks

PS:> $serverInstance = Get-SqlInstance -ServerInstance '(local)'
PS:> $checks = Get-SqlAssessmentItem $serverInstance | Select Name, Description | Out-GridView -PassThru
PS:> Invoke-SqlAssessment $serverInstance -Check $checks

The second line of this example shows obtaining checks for a $serverInstance, and selecting some of them interactively. Selected items are stored in an array variable, which then is used as input for Invoke-SqlAssessment cmdlet. Only picked checks run during the assessment process.

Parameters

-Check

One or more checks, check IDs, or tags. For every check object, Invoke-SqlAssessment runs that check if it supports input object. For every check ID, Invoke-SqlAssessment runs the corresponding check if it supports input object. For tags, Invoke-SqlAssessment runs checks with any of those tags.

Type:Object[]
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-Configuration

Specifies paths to files containing custom configuration. Customization files will be applied to default configuration in specified order. The scope is limited to this cmdlet invocation only.

Type:PSObject
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-InputObject

Specifies a SQL Server object or the path to such an object. The cmdlet runs assessment for this object. When this parameter is omitted, current location is used as input object. If current location is not a supported SQL Server object, the cmdlet signals an error.

Type:PSObject
Aliases:Target
Position:10
Default value:None
Accept pipeline input:True (ByValue)
Accept wildcard characters:False
-MinSeverity

Specifies minimum severity level for checks to be found.

For example, checks of Warning or Information levels will not be returned when -MinSeverity=Critical.

Possible values: Information, Warning, Critical

Type:SeverityLevel
Aliases:Severity
Accepted values:None, Information, Warning, Critical
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False

Inputs

System.String[]

Microsoft.SqlServer.Management.Smo.SqlSmoObject[]

Outputs

Microsoft.SqlServer.Assessment.Cmdlets.AssessmentNote