Created
July 28, 2015 15:23
-
-
Save Kevin-Bronsdijk/72eb514ddac70758144e to your computer and use it in GitHub Desktop.
database-integrity-history-reports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module SQLPS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module SQLPS -DisableNameChecking |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#replace this with your instance name | |
$instanceName = "Server\Instance" | |
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName | |
$server.ConnectionContext.ConnectTimeout = 2200 | |
$server.ConnectionContext.StatementTimeout = 2200 | |
#store results | |
$results = @() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#for all databases | |
$databases = $server.Databases | |
#just a single database | |
#$databaseName = "dbname" | |
#$databases = $server.Databases[$databasename] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
$statement = 'CheckCatalog' | |
# CheckCatalog SQL server 2012 | |
$database.CheckCatalog() | |
# CheckCatalog pre 2012 | |
#$database.CheckCatalog([Microsoft.SqlServer.Management.Smo.RepairType]::None) | |
$statement = 'CheckAllocations' | |
$database.CheckAllocations([Microsoft.SqlServer.Management.Smo.RepairType]::None) | |
$statement = 'CheckTables' | |
$database.CheckTables([Microsoft.SqlServer.Management.Smo.RepairType]::None) | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module SQLPS | |
#replace this with your instance name | |
$instanceName = "Server\Instance" | |
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName | |
$server.ConnectionContext.ConnectTimeout = 2200 | |
$server.ConnectionContext.StatementTimeout = 2200 | |
#store results | |
$results = @() | |
#for all databases | |
$databases = $server.Databases | |
#just a single database | |
#$databaseName = "dbname" | |
#$databases = $server.Databases[$databasename] | |
foreach ($database in $databases) { | |
$exception = 'x' | |
$statement | |
$name = $database.Name | |
Try | |
{ | |
$statement = 'CheckCatalog' | |
# CheckCatalog SQL server 2012 | |
$database.CheckCatalog() | |
# CheckCatalog pre 2012 | |
#$database.CheckCatalog([Microsoft.SqlServer.Management.Smo.RepairType]::None) | |
$statement = 'CheckAllocations' | |
$database.CheckAllocations([Microsoft.SqlServer.Management.Smo.RepairType]::None) | |
$statement = 'CheckTables' | |
$database.CheckTables([Microsoft.SqlServer.Management.Smo.RepairType]::None) | |
} | |
Catch | |
{ | |
$err = $_.Exception | |
while ( $err.InnerException ) | |
{ | |
$err = $err.InnerException | |
}; | |
$exception = $err.Message | |
} | |
$prop = @{ | |
'Database'= $name | |
'Exception'=$exception | |
'Statement'=$statement | |
'Date'=Get-Date | |
} | |
$result = New-Object -TypeName PSObject -Property $prop | |
$results = $results + $result | |
} | |
$results | Format-List | out-file C:\\Report.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment