PoshCode Logo PowerShell Code Repository

Script-Object.ps1 by Justin Dearing 6 years ago (modification of post by Justin Dearing view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2587"></script>download | new post

A powershell script that will create the CREATE DDL for any object in a SQL Server database. It requires the open source Atlantis.SchemaEngine.dll available at http://www.atlantis-interactive.co.uk/blog/post/2011/02/24/Free-SQL-Server-Schema-Synchronisation-Engine-announcing-the-release-of-the-AtlantisSchemaEngine-source-code.aspx

  1. #Copyright (c) 2011 Justin Dearing
  2. #
  3. #Permission is hereby granted, free of charge, to any person obtaining a copy
  4. #of this software and associated documentation files (the "Software"), to deal
  5. #in the Software without restriction, including without limitation the rights
  6. #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. #copies of the Software, and to permit persons to whom the Software is
  8. #furnished to do so, subject to the following conditions:
  9. #
  10. #The above copyright notice and this permission notice shall be included in
  11. #all copies or substantial portions of the Software.
  12. #
  13. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. #THE SOFTWARE.
  20.  
  21. # The authoritative version is https://github.com/justaprogrammer/PowerShellScripts/blob/master/Script-Object.ps1.
  22. # If you have a change I'd love to know about it to consider merging it into github.
  23. param(
  24.     [Parameter(Mandatory=$true, HelpMessage='The name of the database object we wish to script')]
  25.     [string] $ObjectName,
  26.     [string] $Path = "$($ObjectName).sql",
  27.     [string] $ConnectionString = 'Data Source=.\sqlexpress2k8R2;Initial Catalog=master;Integrated Security=SSPI;',
  28.     [string] $AtlantisSchemaEngineBaseDir = 'F:\src\Atlantis.SchemaEngine\' # Adjust for your environment
  29. );
  30.  
  31. Add-Type -Path "$($AtlantisSchemaEngineBaseDir)\Atlantis.SchemaEngine\bin\Debug\Atlantis.SchemaEngine.dll"
  32. $schemaReader = [Atlantis.SchemaEngine.Container.SQLServer.SQLServerSchemaReaderFactory]::GetSpecificSQLServerSchemaReader($ConnectionString, [Atlantis.SchemaEngine.Enumerations.ContainerMode]::Navigation)
  33. $dbObjects = $schemaReader.ReadObjects() | Where-Object { $_.ObjectName,$_.ObjectDesriptiveName,$_.ObjectQualifiedName -contains $ObjectName };
  34. if ($dbObjects -eq $null) {
  35.     Throw New-Object System.ArgumentException "Object `"$($ObjectName)`" not found.",'-ObjectName';
  36. }
  37. $dbObjects.Script([Atlantis.SchemaEngine.Enumerations.ScriptGenerationType]::Create, (New-Object Atlantis.SchemaEngine.Configuration.GenerationOptions)).Scripts

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me