Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 39d141e

Browse files
committed
Added code to clean up logs
1 parent c8d9585 commit 39d141e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

‎Powershell/CopyDatabase.ps1

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ $SourceDatabase = "Database to copy from"
66
$DestinationDB = "Database to copy to"
77
$fileName = "..\Schema.sql" #Path to the Schema.sql file included in this repo
88
$tempPath = "..\BCP\" #A temp directory on a drive with enough free space to save the bcp files during the export and import operations
9+
$logPath = "C:\Script_Utility\log\"
910

1011
#Run the Schema.sql file againist the source database to get the metadata for the import
1112
$DS = Invoke-Sqlcmd -ServerInstance $ServerName -User $UserName -Password $Password -Database $SourceDatabase -InputFile $fileName -As DataSet
@@ -18,24 +19,38 @@ foreach($sql in $DS.Tables[0].Rows){
1819
Invoke-Sqlcmd -ServerInstance $DestinationServer -Database $DestinationDB -MaxCharLength 8000 -Query $sql.SqlStatement
1920
Write-Output $sql.SqlStatement
2021

21-
if($sql.TableName -ne "Schema" -and $sql.TableName -ne "Alter" )
22+
#If we just created the table, let's import the data before applying an constraints or indexes
23+
if($sql.ScriptType -eq "Table" )
2224
{
2325
#add code to create format file
24-
$bcp = "bcp $($sql.SchemaName).$($sql.TableName) format nul -n -f $($tempPath)$($sql.TableName).fmt -S $($ServerName) -d $($SourceDatabase) -U $($UserName) -P $($Password) "
26+
$bcp = "bcp $($sql.SchemaName).$($sql.TableName) format nul -n -f $($tempPath)$($sql.TableName).fmt -S $($ServerName) -d $($SourceDatabase) -U $($UserName) -P $($Password) -e $($logPath)$($sql.TableName)_fmt_err.txt"
2527
Invoke-Expression $bcp
2628
Write-Output $bcp
2729

28-
$bcp = "bcp $($sql.SchemaName).$($sql.TableName) out $($tempPath)$($sql.TableName).bcp -S $($ServerName) -d $($SourceDatabase) -U $($UserName) -P $($Password) -E -n -t"
30+
$bcp = "bcp $($sql.SchemaName).$($sql.TableName) out $($tempPath)$($sql.TableName).bcp -S $($ServerName) -d $($SourceDatabase) -U $($UserName) -P $($Password) -E -n -t -e $($logPath)$($sql.TableName)_in_err.txt"
2931
Invoke-Expression $bcp
3032
Write-Output $bcp
3133

3234
#add code to import in to use format file
33-
$bcp = "bcp $($sql.SchemaName).$($sql.TableName) in $($tempPath)$($sql.TableName).bcp -S $($DestinationServer) -d $($DestinationDB) -T -t -E -f $($tempPath)$($sql.TableName).fmt"
35+
$bcp = "bcp $($sql.SchemaName).$($sql.TableName) in $($tempPath)$($sql.TableName).bcp -S $($DestinationServer) -d $($DestinationDB) -T -t -E -f $($tempPath)$($sql.TableName).fmt -e $($logPath)$($sql.TableName)_out_err.txt"
3436
Invoke-Expression $bcp
3537
Write-Output $bcp
3638

3739
#Remove temp data file before moving on
3840
remove-item -path "$($tempPath)$($sql.TableName).bcp"
3941
remove-item -path "$($tempPath)$($sql.TableName).fmt"
42+
43+
#Clean up Logs
44+
if((Get-Item "$($logPath)$($sql.TableName)_out_err.txt").Length -eq 0 ){
45+
Remove-Item "$($logPath)$($sql.TableName)_out_err.txt"
46+
}
47+
48+
if((Get-Item "$($logPath)$($sql.TableName)_in_err.txt").Length -eq 0 ){
49+
Remove-Item "$($logPath)$($sql.TableName)_in_err.txt"
50+
}
51+
52+
if((Get-Item "$($logPath)$($sql.TableName)_fmt_err.txt").Length -eq 0 ){
53+
Remove-Item "$($logPath)$($sql.TableName)_fmt_err.txt"
54+
}
4055
}
4156
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /