mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
23 KiB
23 KiB
In [2]:
# Uncomment and run Install-Module only the first time
# Install-Module -Name SqlServer -AllowClobber -Force
Import-Module -Name SqlServer
Get-ModuleIn [6]:
# Option 1
Get-SqlInstance -ServerInstance 'localhost' | Invoke-SqlAssessmentIn [8]:
# Option 2
$serverInstance = Get-SqlInstance -ServerInstance 'localhost'
Invoke-SqlAssessment $serverInstanceIn [10]:
# Option 3
Get-Item SQLSERVER:\SQL\localhost\default | Invoke-SqlAssessmentIn [12]:
# Option 4
Invoke-SqlAssessment SQLSERVER:\SQL\localhost\defaultIn [14]:
# Option 5
cd SQLSERVER:\SQL\localhost\default
Invoke-SqlAssessment -VerboseIn [16]:
# Option 6
cd SQLSERVER:\SQL\localhost
Get-Item default | Invoke-SqlAssessmentIn [0]:
# Option 7 - passing registered servers to the cmdlet
Get-ChildItem 'SQLSERVER:\SQLRegistration\Database Engine Server Group' | WHERE { $_.Mode -ne 'D'} | Invoke-SqlAssessmentIn [18]:
# Option 1
$database = Get-SqlDatabase -ServerInstance 'localhost' -Name master
Invoke-SqlAssessment $database -VerboseIn [20]:
# Option 2
Invoke-SqlAssessment SQLSERVER:\SQL\localhost\default\Databases\master -VerboseIn [22]:
# Option 3
cd SQLSERVER:\SQL\localhost\default\Databases\master
Invoke-SqlAssessmentIn [26]:
# Get recommendations for all databases on local instance:
Get-SqlDatabase -ServerInstance 'localhost' | Invoke-SqlAssessmentIn [28]:
# Get all rules available for an object:
$serverInstance = Get-SqlInstance -ServerInstance 'localhost'
Get-SqlAssessmentItem $serverInstance | Select Id, DescriptionIn [30]:
# Get all rules by a specific tag
$serverInstance = Get-SqlInstance -ServerInstance 'localhost'
Get-SqlAssessmentItem $serverInstance -Check TraceFlagIn [32]:
# Run a rule by its name
$serverInstance = Get-SqlInstance -ServerInstance 'localhost'
Invoke-SqlAssessment $serverInstance -Check TF634In [36]:
# Run a group of rules using their tag
$database = Get-SqlDatabase -ServerInstance 'localhost'
Invoke-SqlAssessment $database -Check BackupIn [38]:
Get-SqlInstance -ServerInstance 'localhost' | Invoke-SqlAssessment -FlattenOutput |
Write-SqlTableData -ServerInstance 'localhost' -DatabaseName SQLAssessmentDemo -SchemaName Assessment -TableName Results -ForceIn [2]:
#Setup two parameters that are used in all the customization examples below
#$binariesPath='<replace this with the path to customization json files, e.g. "C:\SQLAsmnt\CustomizationSamples">'
$binariesPath='C:\SQLAsmnt\CustomizationSamples'
$binariesPath
$serverInstance = Get-SqlInstance -ServerInstance 'localhost'
$serverInstance
$sqlDbMaster = $serverInstance | Get-SqlDatabase -Name master
$sqlDbMaster
In [7]:
# Disable a single rule using its ID (TF634)
# To see this in action, make sure you have trace flag 634 turned on in the instance you are testing. Otherwise this rule will not fire even when enabled.
# You will see that TF634 is not enabled (On=False)
Get-SqlAssessmentItem $serverInstance -Configuration $(join-path $binariesPath "DisableTF634.json")In [9]:
# Disable all Trace Flag rules using a tag
# You will see that all TF rules are set to False (disabled)
Get-SqlAssessmentItem $serverInstance -Configuration $(join-path $binariesPath "DisableAllTF.json")In [11]:
# Combine configurations
# This example disables all trace flag rules except for performance-related ones using tags.
# The order of json files is important. First we disable all TF rules, then enable performance rules which re-enables performance-related TF rules.
Get-SqlAssessmentItem $serverInstance -Configuration $(join-path $binariesPath "DisableAllTF.json"), $(join-path $binariesPath "EnablePerformance.json")In [45]:
# Create a new rule with TSQL probe
# This rule applies to databases and uses a TSQL statement to get the data for the rule.
Invoke-SqlAssessment $sqlDbMaster -configuration $(join-path $binariesPath "CustomRuleTSQLProbe.json")In [106]:
# Override threshold parameter
# CustomRuleThresholdChange.json defines a new threshold value for DBSpaceAvailable rule created above
Invoke-SqlAssessment $sqlDbMaster -configuration $(join-path $binariesPath "CustomRuleTSQLProbe.json"),$(join-path $binariesPath "CustomRuleThresholdChange.json")In [116]:
# Create a new rule with CLR probe
# !!! You need to set the path to the dll in CustomRuleCLRProbe.json file first
Invoke-SqlAssessment $serverInstance -configuration $(join-path $binariesPath "CustomRuleCLRProbe.json")