Skip to content

Commit a968960

Browse files
authored
Merge pull request #8 from Fortee2/Working
Updated from powershell script to powershell module
2 parents f36fdc5 + eb8a5fe commit a968960

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

Powershell/CopyDatabase.ps1 renamed to Powershell/CopyDatabase.psm1

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
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+
136
function copy-database{
237
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
1550
)
1651
#Get Schema Data from the database
1752
$DS = Invoke-Sqlcmd -MaxCharLength 150000 -ServerInstance $ServerName -Database $SourceDatabase -InputFile $fileName -As DataSet
@@ -42,6 +77,7 @@ function copy-database{
4277
}
4378
}
4479
}
80+
Export-ModuleMember -Function copy-database
4581

4682
function copy-schema {
4783
param(
@@ -56,14 +92,14 @@ function copy-schema {
5692
{
5793
$error | Out-File -FilePath "$($ErrorFile)"
5894
$sql.SqlStatement | Out-File -Append -FilePath "$($ErrorFile)"
59-
95+
6096
$error.Clear()
6197
}
6298
Write-Output $Sql
6399
}
64100

65101
#TODO: Breakup into extract and import
66-
function copy-data{
102+
function copy-data{
67103
param(
68104
[string] $SchemaName,
69105
[string] $TableName,

0 commit comments

Comments
 (0)