mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
21 KiB
21 KiB
In [4]:
# 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 [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-SqlAssessment -VerboseIn [28]:
# Get all rules available for an object:
$serverInstance = Get-SqlInstance -ServerInstance 'localhost'
Get-SqlAssessmentItem $serverInstance | Select ID, Description | Format-TableIn [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 [40]:
# Setup two parameters that are used in all the customization examples below
# $binariesPath="<replace this with the path to customization sample json files>"
$binariesPath="C:\Users\ebrue\OneDrive - Microsoft\Monitoring\API\Custimization Sample JSON"
$serverInstance = Get-SqlInstance -ServerInstance 'localhost'In [74]:
# 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 [76]:
# 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 [80]:
# 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 [66]:
# Create new rule with TSQL probe
# This rule applies to databases and uses a TSQL statement to get the data for the rule.
$database = Get-SqlDatabase -ServerInstance 'localhost' -Name master
Invoke-SqlAssessment $database -configuration $(join-path $binariesPath "CustomRuleTSQLProbe.json")In [68]:
# Override threshold parameter
# CustomRuleThresholdChange.json defines a new threshold value for DBSpaceAvailable rule created above
$database = Get-SqlDatabase -ServerInstance 'localhost' -Name master
Invoke-SqlAssessment $database -configuration $(join-path $binariesPath "CustomRuleTSQLProbe.json"),$(join-path $binariesPath "CustomRuleThresholdChange.json")In [70]:
# 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")