#
# Syntax: & GetIndexes.ps1 "SQLServer\Instance" "Database" "OutputPathAndFileName"
#
# In order for PowerShell to run scripts, run Set-ExecutionPolicy RemoteSigned
#
# Notes:
# 1. If the specified output file already exists, it is deleted without warning.
# 2. If the specified output path does not exist, it is created.
#---------------------------------------------------------------------------------------------------
# PARAMETERS (Required)
#---------------------------------------------------------------------------------------------------
# $SQLInstance [string] - the name of the sql instance, in server\instance format.
# $Database [string] - the name of the database to script out indexes for.
# $FileName [string] - the location of the output file.
#---------------------------------------------------------------------------------------------------
# PARAMETERS (Optional)
#---------------------------------------------------------------------------------------------------
# $Schema [string] - the name of the schema to script out indexes for. If not specified, uses all.
# $Table [string] - the name of the table to script out indexes for. If not specified, uses all.
# $IncludeDrop [boolean] - set to true (default) to have the script generate drop statements and
# existance check prior to dropping.
# $AppendToFile [boolean] - set to true (default) to append to the file. False will overwrite the file.
# $IncludeHeaders [boolean] - set to true (default) to include scripting comments.
# $NoFileGroup [boolean] - set to true (default) to suppress filegroup placement options in generated script.
# $ToFileOnly [boolean] - set to true (default) to send generated script to the file only.
# Setting to false will also send the generated script to the screen.
#---------------------------------------------------------------------------------------------------
# MODIFICATION LOG
#---------------------------------------------------------------------------------------------------
#2012年08月31日 WGS Initial Creation.
#---------------------------------------------------------------------------------------------------
param(
[String]$SQLInstance,# the SQL instance to generate a script from, in Server\Instance format.
[String]$Database,# the database to generate the index scripts from.
[String]$Filename,# the output file for the generated scripts.
#Optional parameters follow
[String]$SchemaName,# if specified, restricts generated scripts to tables in this schema.
[String]$TableName,# if specified, restricts generated scripts to tables with this name.
[boolean]$IncludeDrop=$True,# set to $True to include drop statements
[boolean]$AppendToFile=$True,# set to $False to overwrite file
[boolean]$IncludeHeaders=$True,# set to $False to not have the scripting headers
[boolean]$NoFileGroup=$True,# set to $False to include filegroup placement in script
[boolean]$ToFileOnly=$True# set to $False to send results to screen also
)
cls
if((!$SQLInstance)-or(!$Database)-or(!$Filename))
{
Write-Warning'Syntax: & "GetIndexes.ps1" "SQLServer\Instance" "DatabaseName" "Output Path and Filename" '
Write-Warning'Syntax (Optional Parameters): '
Write-Warning' "SchemaName"'
Write-Warning' "TableName"'
Write-Warning' "Include Drop Statements" (boolean)'
Write-Warning' "Append each object''s script to file" (boolean)'
Write-Warning' "Include Script Headers" (boolean)'
Write-Warning' "Don''t include filegroup placement settings" (boolean)'
Write-Warning' "Send script output to file only" (boolean)'
Write-Warning' "Pass in boolean values as $True or $False"'
Write-Host""
}
else
{
# if the file already exists, delete it
if(Test-Path$Filename){Remove-Item$Filename}
# get the path that this file is in
$Path=Split-Path$Filename-Parent
# if the path doesn't exist, create it
if(!(Test-Path-path$Path)){New-Item$Path-TypeDirectory}
Write-Host"Output file: $Filename"
#Load SMO, connect to server and database
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')|out-null
$server=New-Object('Microsoft.SqlServer.Management.SMO.Server')"$SQLInstance"
$db=$server.Databases[$Database]
# this object is used to script the actual index
$Scriptr=New-Object('Microsoft.SqlServer.Management.Smo.Scripter')($server)
$Scriptr.Options.ScriptDrops=$False
$Scriptr.Options.Indexes=$True
$Scriptr.Options.IncludeHeaders=$IncludeHeaders
$Scriptr.Options.ClusteredIndexes=$True
$Scriptr.Options.NonClusteredIndexes=$True
$Scriptr.Options.XmlIndexes=$True
$Scriptr.Options.AppendToFile=$AppendToFile
$Scriptr.Options.FileName=$Filename
$Scriptr.Options.ToFileOnly=$ToFileOnly
$Scriptr.Options.NoFileGroup=$NoFileGroup
foreach($tablein$db.Tables)
{
if(((!$TableName)-or($TableName-eq$table.name))-and
((!$SchemaName)-or($SchemaName-eq$table.Schema)))
{
foreach($indexin$table.Indexes)
{
Write-Host"Database: $db; Table: $table; Index: $index"
if($IncludeDrop-eq$True)
{
$Scriptr.Options.IncludeIfNotExists=$True
$Scriptr.Options.ScriptDrops=$True
$Scriptr.Script($index)
}
$Scriptr.Options.IncludeIfNotExists=$False
$Scriptr.Options.ScriptDrops=$False
$Scriptr.Script($index)
}
}
}
}
Thank you very much 🙂
Very useful script, It worked for me . I enhanced the script to script contraints, tables, procedures, functions.
No trackbacks yet.
This site uses Akismet to reduce spam. Learn how your comment data is processed.