Guest User

Untitled

a guest
Feb 18th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. # Usage: powershell MSSQLExportSchema.ps1 "SERVERNAME" "DATABASE" "C:\<YourOutputPath>"
  2.  
  3.  
  4. # Start Script
  5. #Set-ExecutionPolicy RemoteSigned
  6.  
  7. # Set-ExecutionPolicy -ExecutionPolicy:Unrestricted -Scope:LocalMachine
  8. function GenerateDBScript([string]$serverName, [string]$dbname, [string]$scriptpath)
  9. {
  10. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
  11. [System.Reflection.Assembly]::LoadWithPartialName("System.Data") | Out-Null
  12. $srv = new-object "Microsoft.SqlServer.Management.SMO.Server" $serverName
  13. $srv.SetDefaultInitFields([Microsoft.SqlServer.Management.SMO.View], "IsSystemObject")
  14. $db = New-Object "Microsoft.SqlServer.Management.SMO.Database"
  15. $db = $srv.Databases[$dbname]
  16. $scr = New-Object "Microsoft.SqlServer.Management.Smo.Scripter"
  17. $deptype = New-Object "Microsoft.SqlServer.Management.Smo.DependencyType"
  18. $scr.Server = $srv
  19. $options = New-Object "Microsoft.SqlServer.Management.SMO.ScriptingOptions"
  20. $options.AllowSystemObjects = $false
  21. $options.IncludeDatabaseContext = $true
  22. $options.IncludeIfNotExists = $false
  23. $options.ClusteredIndexes = $true
  24. $options.Default = $true
  25. #$options.SchemaQualify = $true
  26. $options.ScriptOwner = $true
  27. $options.DriAll = $true
  28. $options.Indexes = $true
  29. $options.NonClusteredIndexes = $true
  30. $options.IncludeHeaders = $false
  31. $options.ToFileOnly = $true
  32. $options.AppendToFile = $false
  33. $options.ScriptDrops = $false
  34. $options.ScriptSchema = $true
  35.  
  36. # Set options for SMO.Scripter
  37. $scr.Options = $options
  38.  
  39. #=============
  40. # Tables
  41. #=============
  42.  
  43.  
  44. Foreach ($tb in $db.Tables)
  45. {
  46. If ($tb.IsSystemObject -eq $FALSE)
  47. {
  48. $options.FileName = $scriptpath + "\$($dbname)\TABLES\$($tb.Schema).$($tb.Name).sql"
  49. New-Item $options.FileName -type file -force | Out-Null
  50. $smoObjects = New-Object Microsoft.SqlServer.Management.Smo.UrnCollection
  51. $smoObjects.Add($tb.Urn)
  52. $scr.Script($smoObjects)
  53. }
  54. }
  55.  
  56. #=============
  57. # Views
  58. #=============
  59. $views = $db.Views | where {$_.IsSystemObject -eq $false}
  60. Foreach ($view in $views)
  61. {
  62. if ($views -ne $null)
  63. {
  64. $options.FileName = $scriptpath + "\$($dbname)\VIEWS\$($view.Schema).$($view.Name).sql"
  65. New-Item $options.FileName -type file -force | Out-Null
  66. $scr.Script($view)
  67. }
  68. }
  69.  
  70. #=============
  71. # StoredProcedures
  72. #=============
  73. $StoredProcedures = $db.StoredProcedures | where {$_.IsSystemObject -eq $false}
  74. Foreach ($StoredProcedure in $StoredProcedures)
  75. {
  76. if ($StoredProcedures -ne $null)
  77. {
  78. $options.FileName = $scriptpath + "\$($dbname)\STORED_PROCEDURES\$($StoredProcedure.Schema).$($StoredProcedure.Name).sql"
  79. New-Item $options.FileName -type file -force | Out-Null
  80. $scr.Script($StoredProcedure)
  81. }
  82. }
  83.  
  84. #=============
  85. # Functions
  86. #=============
  87. $UserDefinedFunctions = $db.UserDefinedFunctions | where {$_.IsSystemObject -eq $false}
  88. Foreach ($function in $UserDefinedFunctions)
  89. {
  90. if ($UserDefinedFunctions -ne $null)
  91. {
  92. $options.FileName = $scriptpath + "\$($dbname)\FUNCTIONS\$($function.Schema).$($function.Name).sql"
  93. New-Item $options.FileName -type file -force | Out-Null
  94. $scr.Script($function)
  95. }
  96. }
  97. $PartitionFunctions = $db.PartitionFunctions | where {$_.IsSystemObject -eq $false}
  98. Foreach ($function in $PartitionFunctions)
  99. {
  100. if ($UserDefinedFunctions -ne $null)
  101. {
  102. $options.FileName = $scriptpath + "\$($dbname)\FUNCTIONS_PARTITION\$($function.Schema).$($function.Name).sql"
  103. New-Item $options.FileName -type file -force | Out-Null
  104. $scr.Script($function)
  105. }
  106. }
  107.  
  108. #=============
  109. # DBTriggers
  110. #=============
  111. $DBTriggers = $db.Triggers
  112. foreach ($trigger in $db.triggers)
  113. {
  114. if ($DBTriggers -ne $null)
  115. {
  116. $options.FileName = $scriptpath + "\$($dbname)\TRIGGERS\$($trigger.Schema).$($trigger.Name).sql"
  117. New-Item $options.FileName -type file -force | Out-Null
  118. $scr.Script($DBTriggers)
  119. }
  120. }
  121.  
  122. #=============
  123. # Table Triggers
  124. #=============
  125. Foreach ($tb in $db.Tables)
  126. {
  127. if($tb.triggers -ne $null)
  128. {
  129. foreach ($trigger in $tb.triggers)
  130. {
  131. $options.FileName = $scriptpath + "\$($dbname)\TRIGGERS\$($tb.Schema).$($tb.Name).$($trigger.Name).sql"
  132. New-Item $options.FileName -type file -force | Out-Null
  133. $scr.Script($trigger)
  134. }
  135. }
  136. }
  137. }
  138.  
  139. #=============
  140. # Execute
  141. #=============
  142. GenerateDBScript $args[0] $args[1] $args[2]
Add Comment
Please, Sign In to add comment