Guest User

Untitled

a guest
Jun 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. $database_host = "<host>"
  2. $database_name = "<database>"
  3. $output_file = "<output_file>"
  4.  
  5. $user = "<username>"
  6. $password = "<password>"
  7.  
  8. [system.reflection.assembly]::loadWithPartialName('Microsoft.SqlServer.SMO')
  9. $server = new-object "Microsoft.SqlServer.Management.Smo.Server" $database_host
  10. $server.connectionContext.loginSecure = $false
  11. $server.connectionContext.set_Login($user)
  12. $server.connectionContext.set_Password($password)
  13.  
  14. $database = $server.databases[$database_name]
  15. $database.script() | out-file $output_file
  16.  
  17. $scripter = new-object "Microsoft.SqlServer.Management.Smo.Scripter" $server
  18. $scripter.options.appendToFile = $false
  19. $scripter.options.scriptData = $true
  20. $scripter.options.includeIfNotExists = $true
  21. $scripter.options.includeHeaders = $true
  22.  
  23. $scripter.options.indexes = $true
  24. $scripter.options.withDependencies = $true
  25.  
  26. $scripter.options.toFileOnly = $true
  27. $scripter.options.fileName = $output_file
  28.  
  29. $tables = $database.tables
  30.  
  31. foreach ($s in $scripter.enumScript($tables)) { write-host $s }
Add Comment
Please, Sign In to add comment