-
-
Save AndyDaSilva52/e612692c3b2b79e29823795f47b3e6c7 to your computer and use it in GitHub Desktop.
Export MSSQL schema with PowerShell. This script will export your schema definitions for tables, stored procs, triggers, functions and views to .sql files with Directory Hierarchy by type of Object
This file contains 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
# Usage: powershell MSSQLExportSchema.ps1 "SERVERNAME" "DATABASE" "C:\<YourOutputPath>" | |
# Start Script | |
#Set-ExecutionPolicy RemoteSigned | |
# Set-ExecutionPolicy -ExecutionPolicy:Unrestricted -Scope:LocalMachine | |
function GenerateDBScript([string]$serverName, [string]$dbname, [string]$scriptpath) | |
{ | |
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null | |
[System.Reflection.Assembly]::LoadWithPartialName("System.Data") | Out-Null | |
$srv = new-object "Microsoft.SqlServer.Management.SMO.Server" $serverName | |
$srv.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.View], "IsSystemObject") | |
$db = New-Object "Microsoft.SqlServer.Management.SMO.Database" | |
$db = $srv.Databases[$dbname] | |
$scr = New-Object "Microsoft.SqlServer.Management.Smo.Scripter" | |
$deptype = New-Object "Microsoft.SqlServer.Management.Smo.DependencyType" | |
$scr.Server = $srv | |
$options = New-Object "Microsoft.SqlServer.Management.SMO.ScriptingOptions" | |
$options.AllowSystemObjects = $false | |
$options.IncludeDatabaseContext = $true | |
$options.IncludeIfNotExists = $false | |
$options.ClusteredIndexes = $true | |
$options.Default = $true | |
#$options.SchemaQualify = $true | |
$options.ScriptOwner = $true | |
$options.DriAll = $true | |
$options.Indexes = $true | |
$options.NonClusteredIndexes = $true | |
$options.IncludeHeaders = $false | |
$options.ToFileOnly = $true | |
$options.AppendToFile = $false | |
$options.ScriptDrops = $false | |
$options.ScriptSchema = $true | |
# Set options for SMO.Scripter | |
$scr.Options = $options | |
#============= | |
# Tables | |
#============= | |
Foreach ($tb in $db.Tables) | |
{ | |
If ($tb.IsSystemObject -eq $FALSE) | |
{ | |
$options.FileName = $scriptpath + "\$($dbname)\TABLES\$($tb.Schema).$($tb.Name).sql" | |
New-Item $options.FileName -type file -force | Out-Null | |
$smoObjects = New-Object Microsoft.SqlServer.Management.Smo.UrnCollection | |
$smoObjects.Add($tb.Urn) | |
$scr.Script($smoObjects) | |
} | |
} | |
#============= | |
# Views | |
#============= | |
$views = $db.Views | where {$_.IsSystemObject -eq $false} | |
Foreach ($view in $views) | |
{ | |
if ($views -ne $null) | |
{ | |
$options.FileName = $scriptpath + "\$($dbname)\VIEWS\$($view.Schema).$($view.Name).sql" | |
New-Item $options.FileName -type file -force | Out-Null | |
$scr.Script($view) | |
} | |
} | |
#============= | |
# StoredProcedures | |
#============= | |
$StoredProcedures = $db.StoredProcedures | where {$_.IsSystemObject -eq $false} | |
Foreach ($StoredProcedure in $StoredProcedures) | |
{ | |
if ($StoredProcedures -ne $null) | |
{ | |
$options.FileName = $scriptpath + "\$($dbname)\STORED_PROCEDURES\$($StoredProcedure.Schema).$($StoredProcedure.Name).sql" | |
New-Item $options.FileName -type file -force | Out-Null | |
$scr.Script($StoredProcedure) | |
} | |
} | |
#============= | |
# Functions | |
#============= | |
$UserDefinedFunctions = $db.UserDefinedFunctions | where {$_.IsSystemObject -eq $false} | |
Foreach ($function in $UserDefinedFunctions) | |
{ | |
if ($UserDefinedFunctions -ne $null) | |
{ | |
$options.FileName = $scriptpath + "\$($dbname)\FUNCTIONS\$($function.Schema).$($function.Name).sql" | |
New-Item $options.FileName -type file -force | Out-Null | |
$scr.Script($function) | |
} | |
} | |
$PartitionFunctions = $db.PartitionFunctions | where {$_.IsSystemObject -eq $false} | |
Foreach ($function in $PartitionFunctions) | |
{ | |
if ($UserDefinedFunctions -ne $null) | |
{ | |
$options.FileName = $scriptpath + "\$($dbname)\FUNCTIONS_PARTITION\$($function.Schema).$($function.Name).sql" | |
New-Item $options.FileName -type file -force | Out-Null | |
$scr.Script($function) | |
} | |
} | |
#============= | |
# DBTriggers | |
#============= | |
$DBTriggers = $db.Triggers | |
foreach ($trigger in $db.triggers) | |
{ | |
if ($DBTriggers -ne $null) | |
{ | |
$options.FileName = $scriptpath + "\$($dbname)\TRIGGERS\$($trigger.Schema).$($trigger.Name).sql" | |
New-Item $options.FileName -type file -force | Out-Null | |
$scr.Script($DBTriggers) | |
} | |
} | |
#============= | |
# Table Triggers | |
#============= | |
Foreach ($tb in $db.Tables) | |
{ | |
if($tb.triggers -ne $null) | |
{ | |
foreach ($trigger in $tb.triggers) | |
{ | |
$options.FileName = $scriptpath + "\$($dbname)\TRIGGERS\$($tb.Schema).$($tb.Name).$($trigger.Name).sql" | |
New-Item $options.FileName -type file -force | Out-Null | |
$scr.Script($trigger) | |
} | |
} | |
} | |
} | |
#============= | |
# Execute | |
#============= | |
GenerateDBScript $args[0] $args[1] $args[2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This could be used to put files at a Git Repository to maintain the control of changes in the Database