Running DBCC commands
This recipe shows you some of the DBCC commands that can be run using PowerShell.
How to do it...
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Import the
SQLPS
module and create a new SMO Server object, as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "KERRIGAN" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Some DBCC commands are built into SMO, so you can just call the methods:
$databasename = "AdventureWorks2008R2" $database = $server.Databases[$databasename] #RepairType Values: AllowDataLost, Fast, None, Rebuild $database.CheckTables([Microsoft.SqlServer.Management.Smo.RepairType]::None)
How it works...
Not all DBCC commands are wrapped in SMO methods. Some of the available methods on a database level are:
CheckAllocations
CheckCatalog
CheckTables
To invoke the SMO DBCC...