Click here to monitor SSC

SQL Server database maintenance using Powershell

Last post 05-04-2009, 6:44 PM by IanR. 3 replies.
Sort Posts: Previous Next
  •  02-12-2009, 4:59 PM Post number 71967

    • IanR is not online. Last active: 11-04-2009, 12:11 AM IanR
    • Top 500 Contributor
    • Joined on 02-13-2009
    • Level 1: Deep thought

    SQL Server database maintenance using Powershell

    I'm starting to use Powershell for all my administrative tasks, overnight backups & maintenance, and I'm wondering if anyone has scripted any DBCC functions in Powershell. From what I can see there is no method that corresponds to CheckDB, so I'm doing the following nightly

        $db.checkallocations(0)
        $db.checktables(0)
        $db.checkcatalog()

    and the following weekly

        $db.updateindexstatistics()

    I can't see anything around index refreshes.

    Any suggestions or comments would be appreciated.

  •  03-20-2009, 1:04 AM Post number 72545 in reply to post number 71967

    Re: SQL Server database maintenance using Powershell

    I have a function that has a number of SMO functions defined in it.  Maybe you use the same one.  One of the functions, Get-SqlDatabase, has a tables collection and for each table there is a method, RebuildIndexes.  So grab the tables collection, then iterate through that collection by:

    foreach ($t in $dbt) {$t.RebuildIndexes(90)}

    assuming that $dbt is your tables collection.  Works like a charm.

    Let me know if you need more info.

    David

     

  •  03-29-2009, 7:53 PM Post number 72649 in reply to post number 72545

    • IanR is not online. Last active: 11-04-2009, 12:11 AM IanR
    • Top 500 Contributor
    • Joined on 02-13-2009
    • Level 1: Deep thought

    Re: SQL Server database maintenance using Powershell

    Fantastic. Thanks. That's exactly what I was after. Do you know how to check the level of index fragmentation using Powershell?
  •  05-04-2009, 6:44 PM Post number 73341 in reply to post number 72649

    • IanR is not online. Last active: 11-04-2009, 12:11 AM IanR
    • Top 500 Contributor
    • Joined on 02-13-2009
    • Level 1: Deep thought

    Re: SQL Server database maintenance using Powershell

    Also, wondering if you have any insight into the following

    When I run CheckTables(0) on one of my databases I get the following error:

    Exception calling "CheckTables" with "1" argument(s): "Check tables failed for Database 'prd0Sylvan'. "
    At line:1 char:16
    + $db.CheckTables( <<<< 0)

    But if I run

       $tbls=$db.tables
        foreach ($tbl in $tbls) {$tbl.checktable()}

    it works fine.

View as RSS news feed in XML