1
+ <#
2
+ . Synopsis
3
+ Copies one SQL Server database to another.
4
+
5
+ . Description
6
+ Copies schema, structure (tables, views, etc.), to an empty SQL Server databaase. Also supports copying data via bcp.
7
+
8
+ . Parameter ServerName
9
+ Database to copy from
10
+ . Parameter DestinationServer
11
+ Database to copy to
12
+ . Parameter SourceUser
13
+ Database User for the source database
14
+ . Parameter SourcePassword -
15
+ Password for the source database
16
+ . Parameter DestinationUser
17
+ Database User for the destination database
18
+ . Parameter DestinationPassword
19
+ Password for the destination database
20
+ . Parameter SourceDatabase
21
+ Name of the databse to copy too
22
+ . Parameter DestinationDB
23
+ Name of the destination database
24
+ . Parameter CopyData
25
+ Flag to trigger bulk copy of data to destination database
26
+ . Parameter fileName
27
+ Path to the Schema.sql file included in this repo ".\ExtractDB\Schema.sql"
28
+ . Parameter tempPath
29
+ A temp directory on a drive with enough free space to save the bcp files during the export and import operations ".\BCP\"
30
+ . Parameter logPath
31
+ Directory to write error logs too ".\log\"
32
+
33
+
34
+ #>
35
+
1
36
function copy-database {
2
37
param (
3
- $ServerName = " Localhost\sqlexpress " ,
4
- $DestinationServer = " localhost\sqlexpress " ,
5
- $SourceUser = " Database User " ,
6
- $SourcePassword = " Database User's Password " ,
7
- $DestinationUser = " Database User " ,
8
- $DestinationPassword = " Database User's Password " ,
9
- $SourceDatabase = " websearch " , # name of the databse to copy too
10
- $DestinationDB = " websearchSchema " , # name of the destination database
11
- $CopyData = $false , # flag to trigger bulk copy of data to destination database
12
- $fileName = " .\ExtractDB\Schema.sql" , # Path to the Schema.sql file included in this repo
13
- $tempPath = " .\BCP\ " , # A temp directory on a drive with enough free space to save the bcp files during the export and import operations
14
- $logPath = " .\log\ " # directory to write error logs too
38
+ $ServerName ,
39
+ $DestinationServer ,
40
+ $SourceUser ,
41
+ $SourcePassword ,
42
+ $DestinationUser ,
43
+ $DestinationPassword ,
44
+ $SourceDatabase ,
45
+ $DestinationDB ,
46
+ $CopyData ,
47
+ $fileName = " .. \ExtractDB\Schema.sql" ,
48
+ $tempPath ,
49
+ $logPath
15
50
)
16
51
# Get Schema Data from the database
17
52
$DS = Invoke-Sqlcmd - MaxCharLength 150000 - ServerInstance $ServerName - Database $SourceDatabase - InputFile $fileName -As DataSet
@@ -42,6 +77,7 @@ function copy-database{
42
77
}
43
78
}
44
79
}
80
+ Export-ModuleMember - Function copy-database
45
81
46
82
function copy-schema {
47
83
param (
@@ -56,14 +92,14 @@ function copy-schema {
56
92
{
57
93
$error | Out-File - FilePath " $ ( $ErrorFile ) "
58
94
$sql.SqlStatement | Out-File - Append - FilePath " $ ( $ErrorFile ) "
59
-
95
+
60
96
$error.Clear ()
61
97
}
62
98
Write-Output $Sql
63
99
}
64
100
65
101
# TODO: Breakup into extract and import
66
- function copy-data {
102
+ function copy-data {
67
103
param (
68
104
[string ] $SchemaName ,
69
105
[string ] $TableName ,
0 commit comments