Guest User

Untitled

a guest
May 28th, 2018
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. # Usage: powershell ExportSchema.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.DriAll = $true
  26. $options.Indexes = $true
  27. $options.NonClusteredIndexes = $true
  28. $options.IncludeHeaders = $false
  29. $options.ToFileOnly = $true
  30. $options.AppendToFile = $true
  31. $options.ScriptDrops = $false
  32.  
  33. # Set options for SMO.Scripter
  34. $scr.Options = $options
  35.  
  36. #=============
  37. # Tables
  38. #=============
  39. $options.FileName = $scriptpath + "\$($dbname)_tables.sql"
  40. New-Item $options.FileName -type file -force | Out-Null
  41. Foreach ($tb in $db.Tables)
  42. {
  43. If ($tb.IsSystemObject -eq $FALSE)
  44. {
  45. $smoObjects = New-Object Microsoft.SqlServer.Management.Smo.UrnCollection
  46. $smoObjects.Add($tb.Urn)
  47. $scr.Script($smoObjects)
  48. }
  49. }
  50.  
  51. #=============
  52. # Views
  53. #=============
  54. $options.FileName = $scriptpath + "\$($dbname)_views.sql"
  55. New-Item $options.FileName -type file -force | Out-Null
  56. $views = $db.Views | where {$_.IsSystemObject -eq $false}
  57. Foreach ($view in $views)
  58. {
  59. if ($views -ne $null)
  60. {
  61. $scr.Script($view)
  62. }
  63. }
  64.  
  65. #=============
  66. # StoredProcedures
  67. #=============
  68. $StoredProcedures = $db.StoredProcedures | where {$_.IsSystemObject -eq $false}
  69. $options.FileName = $scriptpath + "\$($dbname)_stored_procs.sql"
  70. New-Item $options.FileName -type file -force | Out-Null
  71. Foreach ($StoredProcedure in $StoredProcedures)
  72. {
  73. if ($StoredProcedures -ne $null)
  74. {
  75. $scr.Script($StoredProcedure)
  76. }
  77. }
  78.  
  79. #=============
  80. # Functions
  81. #=============
  82. $UserDefinedFunctions = $db.UserDefinedFunctions | where {$_.IsSystemObject -eq $false}
  83. $options.FileName = $scriptpath + "\$($dbname)_functions.sql"
  84. New-Item $options.FileName -type file -force | Out-Null
  85. Foreach ($function in $UserDefinedFunctions)
  86. {
  87. if ($UserDefinedFunctions -ne $null)
  88. {
  89. $scr.Script($function)
  90. }
  91. }
  92.  
  93. #=============
  94. # DBTriggers
  95. #=============
  96. $DBTriggers = $db.Triggers
  97. $options.FileName = $scriptpath + "\$($dbname)_db_triggers.sql"
  98. New-Item $options.FileName -type file -force | Out-Null
  99. foreach ($trigger in $db.triggers)
  100. {
  101. if ($DBTriggers -ne $null)
  102. {
  103. $scr.Script($DBTriggers)
  104. }
  105. }
  106.  
  107. #=============
  108. # Table Triggers
  109. #=============
  110. $options.FileName = $scriptpath + "\$($dbname)_table_triggers.sql"
  111. New-Item $options.FileName -type file -force | Out-Null
  112. Foreach ($tb in $db.Tables)
  113. {
  114. if($tb.triggers -ne $null)
  115. {
  116. foreach ($trigger in $tb.triggers)
  117. {
  118. $scr.Script($trigger)
  119. }
  120. }
  121. }
  122. }
  123.  
  124. #=============
  125. # Execute
  126. #=============
  127. $instance="your.instance.name"
  128. $database="daterberse"
  129. $sqluser=""
  130. $sqlpass=""
  131. $conn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection
  132. $conn.ConnectionString = "Data Source=$instance;Initial Catalog=$database;User ID=$sqluser;Password=$sqlpass;MultipleActiveResultSets=True;Application Name=Powershell"
  133.  
  134. GenerateDBScript $conn "$database" "C:\Users\Public\Export"
  135. #GenerateDBScript $args[0] $args[1] $args[2]
Add Comment
Please, Sign In to add comment