Home Forums Programming Powershell Need to generate 1 script per object (indexes) RE: Need to generate 1 script per object (indexes)

  • The method used for that script has the objects associated with the table included in the table script, just as it would if you scripted the object in SSMS. If you want a script for each object you will need to traverse every object under the Database namespace with SMO.

    To grab the indexes for example you can create an object for the indexes and iterate over each one. Something like this should work:

    $ix = $db.Tables | select -ExpandProperty Indexes

    foreach ($i in $ix) {

    $i.Script() | Out-File "$output_path\$i.sql"

    }

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton