Created
October 30, 2010 22:12
-
-
Save andrerocker/655798 to your computer and use it in GitHub Desktop.
Powershell Script to: Export all data from yours SQLServer like mysqldump
This file contains hidden or 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
$database_host = "<host>" | |
$database_name = "<database>" | |
$output_file = "<output_file>" | |
$user = "<username>" | |
$password = "<password>" | |
[system.reflection.assembly]::loadWithPartialName('Microsoft.SqlServer.SMO') | |
$server = new-object "Microsoft.SqlServer.Management.Smo.Server" $database_host | |
$server.connectionContext.loginSecure = $false | |
$server.connectionContext.set_Login($user) | |
$server.connectionContext.set_Password($password) | |
$database = $server.databases[$database_name] | |
$database.script() | out-file $output_file | |
$scripter = new-object "Microsoft.SqlServer.Management.Smo.Scripter" $server | |
$scripter.options.appendToFile = $false | |
$scripter.options.scriptData = $true | |
$scripter.options.includeIfNotExists = $true | |
$scripter.options.includeHeaders = $true | |
$scripter.options.indexes = $true | |
$scripter.options.withDependencies = $true | |
$scripter.options.toFileOnly = $true | |
$scripter.options.fileName = $output_file | |
$tables = $database.tables | |
foreach ($s in $scripter.enumScript($tables)) { write-host $s } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How-to export 100 rows (for eg.) each table in database, not all data ?