mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
32 KiB
32 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 [ ]:
# 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 [ ]:
# This approach leverages the Registered Servers feature of SSMS to obtain a list of SQL Servers, and run checks against them.
Get-ChildItem 'SQLSERVER:\SQLRegistration\Database Engine Server Group' |
Where-Object { $_.mode -ne 'd'} |
Invoke-SqlAssessment -FlattenOutput |
Write-SqlTableData -ServerInstance localhost -DatabaseName SQLAssessmentDemo -SchemaName Assessment -TableName Results -ForceIn [ ]:
<# This approach leverages the Central Management Server feature to obtain a list of SQL Servers #>
Get-ChildItem 'SQLSERVER:\SQLRegistration\Central Management Server Group' -Recurse |
Where-Object { $_.mode -ne 'd'} |
Invoke-SqlAssessment -FlattenOutput |
Write-SqlTableData -ServerInstance localhost -DatabaseName SQLAssessmentDemo -SchemaName Assessment -TableName Results -ForceIn [2]:
#Setup three parameters that are used in all the customization examples below
#$samplesPath='<replace this with the path to customization json files, e.g. "C:\SQLAsmnt\CustomizationSamples">'
$samplesPath='C:\SQLAsmnt\CustomizationSamples'
$samplesPath
#$serverInstance = Get-SqlInstance -ServerInstance 'localhost'
$serverInstance = Get-SqlInstance -ServerInstance '.\sql2017express'
$serverInstance
$sqlDbMaster = $serverInstance | Get-SqlDatabase -Name master
$sqlDbMasterIn [4]:
# 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 $samplesPath "DisableTF634.json")In [6]:
# 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 $samplesPath "DisableAllTF.json")In [8]:
# 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 $samplesPath "DisableAllTF.json"), $(join-path $samplesPath "EnablePerformance.json")In [10]:
# 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 $samplesPath "CustomRuleTSQLProbe.json")In [13]:
# Override threshold parameter
# CustomRuleThresholdChange.json defines a new threshold value for DBSpaceAvailable rule created above
Invoke-SqlAssessment $sqlDbMaster -configuration $(join-path $samplesPath "CustomRuleTSQLProbe.json"),$(join-path $samplesPath "CustomRuleThresholdChange.json")In [ ]:
#Create new rule with cmd probe type. It runs 'dir' cmd command and checks that resulted list is'n empty.
#Make sure that xp_cmdshell is enabled
Invoke-SqlAssessment $serverInstance -configuration $(join-path $samplesPath "CustomRuleCmdShellProbe.json")In [ ]:
#Create new rule with Powershell probe type.
#It runs query to get major PS version
#Make sure that xp_cmdshell is enabled and PS execution policy is RemoteSigned or Unrestricted.
Invoke-SqlAssessment $serverInstance -configuration $(join-path $samplesPath "CustomRulePowerShellProbe.json")In [ ]:
#Create new rule with Registry probe
#Make sure that xp_cmdshell is enabled
Invoke-SqlAssessment $serverInstance -configuration $(join-path $samplesPath "CustomRuleRegistryProbe.json")In [ ]:
#Create new rule with WMI probe
#Make sure that xp_cmdshell is enabled.
Invoke-SqlAssessment $serverInstance -configuration $(join-path $samplesPath "CustomRuleWmiProbe.json")In [ ]:
# Create a new rule with CLR probe. CustomRuleCLRProbe.json, in addition to a check with a CLR probe, contains an ovveride to disable all the rules of the DefaultRuleset.
# !!! Complete the prerequisites below before running this block.
# !!! There are 2 dlls in CustomizationSamples folder. Make sure that they both are not blocked: https://stackoverflow.com/questions/34400546/could-not-load-file-or-assembly-operation-is-not-supported-exception-from-hres/45221477
# !!! Then open CustomRuleCLRProbe.json in the same folder and make sure that assembly key contains the right path to TestsProbeLibrary.dll, double slashes are required.
# !!! You're all set. Run this block
Invoke-SqlAssessment $serverInstance -configuration $(join-path $samplesPath "CustomRuleCLRProbe.json")