CheckDB-MSSQL-UsingADO


posted by Richard Giles
10-06-2008

Downloads: 423
File size: 1.8kB
Views: 1,661

Embed
CheckDB-MSSQL-UsingADO
  1. ## ===================================================================== 
  2. ## Title       : CheckDB-MSSQL-UsingADO 
  3. ## Description : Run a DBCC against specified server instance and database 
  4. ## Author      : Idera 
  5. ## Date        : 9/1/2008 
  6. ## Input       : -serverInstance <server\instance> 
  7. ##               -dbName <database name> 
  8. ##               -verbose  
  9. ##               -debug     
  10. ## Output      :  
  11. ## Usage            : PS> . CheckDB-MSSQL-UsingADO -serverInstance local -dbName master -v -d 
  12. ## Notes            : Adapted from Jakob Bindslet script 
  13. ## Tag            : SQL Server, ADO, CheckDB 
  14. ## Change log  : 
  15. ## ===================================================================== 
  16.  
  17. param 
  18.    [string]$serverInstance = "."
  19.       [string]$dbName = "AdventureWorks"
  20.     [switch]$verbose
  21.     [switch]$debug 
  22.  
  23. function main() 
  24.     if ($verbose) {$VerbosePreference = "Continue"
  25.     if ($debug) {$DebugPreference = "Continue"
  26.     CheckDB-MSSQL-UsingADO $serverInstance $dbName 
  27.  
  28. function CheckDB-MSSQL-UsingADO($serverInstance, $dbName
  29.     trap [Exception]  
  30.     
  31.         write-error $("TRAPPED: " + $_.Exception.Message); 
  32.         continue
  33.     
  34.  
  35.     $cn = new-object system.data.SqlClient.SqlConnection( ` 
  36.         "Data Source=$serverInstance;Integrated Security=SSPI;Initial Catalog=$dbName"); 
  37.     $ds = new-object System.Data.DataSet "dsCheckDB" 
  38.     $query = "DBCC CHECKDB($dbName) WITH TABLERESULTS" 
  39.     $da = new-object "System.Data.SqlClient.SqlDataAdapter" ($query, $cn
  40.     $da.Fill($ds
  41.  
  42.     $dtCheckDB = new-object "System.Data.DataTable" "dsCheckDB" 
  43.     $dtCheckDB = $ds.Tables[0] 
  44.     $dtCheckDB | Format-Table -autosize
  45.         -property Error, Level, State, MessageText, ` 
  46.                         RepairLevel, Status, DbId, ObjectId, ` 
  47.                         IndexId, PartitionId, AllocUnitId, File, ` 
  48.                         Page, Slot, RefFile, RefPage, RefSlot, Allocation 
  49.  
  50. main 
Filed under: , ,

Run a DBCC against a specified SQL Server instance and database

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.