functions/Copy-SqlDatabase.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
Function Copy-SqlDatabase
{
<#
.SYNOPSIS
Migrates Sql Server databases from one Sql Server to another.
 
.DESCRIPTION
This script provides the ability to migrate databases using detach/copy/attach or backup/restore. This script works with named instances, clusters and Sql Express.
 
By default, databases will be migrated to the destination Sql Server's default data and log directories. You can override this by specifying -ReuseSourceFolderStructure. Filestreams and filegroups are also migrated. Safety is emphasized.
 
THIS CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
 
.PARAMETER Source
Source SQL Server.You must have sysadmin access and server version must be SQL Server version 2000 or greater.
 
.PARAMETER Destination
Destination Sql Server. You must have sysadmin access and server version must be SQL Server version 2000 or greater.
 
.PARAMETER SourceSqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
 
$scred = Get-Credential, this pass $scred object to the param.
 
Windows Authentication will be used if DestinationSqlCredential is not specified. To connect as a different Windows user, run PowerShell as that user.
 
.PARAMETER DestinationSqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
 
$dcred = Get-Credential, this pass this $dcred to the param.
 
Windows Authentication will be used if DestinationSqlCredential is not specified. To connect as a different Windows user, run PowerShell as that user.
 
.PARAMETER AllDatabases
This is a parameter that was included for safety, so you don't accidentally detach/attach all databases without specifying. Migrates user databases. Does not migrate system or support databases. Requires -BackupRestore or -DetachAttach.
 
.PARAMETER IncludeSupportDbs
Migration of ReportServer, ReportServerTempDb, SSIDb, and distribution databases if they exist. A logfile named $SOURCE-$DESTINATION-$date-Sqls.csv will be written to the current directory. Requires -BackupRestore or -DetachAttach.
 
.PARAMETER BackupRestore
Use the a Copy-Only Backup and Restore Method. This parameter requires that you specify -NetworkShare in a valid UNC format (\\server\share)
  
Backups will be immediately deleted after use unless -NoBackupCleanup is specified.
 
.PARAMETER DetachAttach
Uses the detach/copy/attach method to perform database migrations. No files are deleted on the source. If the destination attachment fails, the source database will be reattached. File copies are performed over administrative shares (\\server\x$\mssql) using BITS. If a database is being mirrored, the mirror will be broken prior to migration.
 
.PARAMETER Reattach
Reattaches all source databases after DetachAttach migration.
 
.PARAMETER ReuseSourceFolderStructure
By default, databases will be migrated to the destination Sql Server's default data and log directories. You can override this by specifying -ReuseSourceFolderStructure.
The same structure on the SOURCE will be kept exactly, so consider this if you're migrating between different versions and use part of Microsoft's default Sql structure (MSSql12.INSTANCE, etc)
 
* note, to reuse destination folder structure, specify -WithReplace
 
.PARAMETER NetworkShare
Specifies the network location for the backup files. The Sql Service service accounts must read/write permission to access this location.
 
.PARAMETER Exclude
Excludes specified databases when performing -AllDatabases migrations. This list is auto-populated for tab completion.
 
.PARAMETER Databases
Migrates ONLY specified databases. This list is auto-populated for tab completion. Multiple databases are allowed.
 
.PARAMETER SetSourceReadOnly
Sets all migrated databases to ReadOnly prior to detach/attach & backup/restore. If -Reattach is used, db is set to read-only after reattach.
 
.PARAMETER NoRecovery
Sets restore to NoRecovery. Ideal for staging.
  
.PARAMETER WithReplace
It's exactly WITH REPLACE. This is useful if you want to stage some complex file paths.
  
.PARAMETER NoBackupCleanup
By default, backups generated by the module will be deleted immediately after they are restored. Use this to skip.
 
.PARAMETER Force
Drops existing databases with matching names. If using -DetachAttach, -Force will break mirrors and drop dbs from Availability Groups.
 
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
 
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
 
.PARAMETER DbPipeline
Takes dbobject from pipeline
 
.PARAMETER NumberFiles
Number of files to split the backup. Default is 3.
 
.PARAMETER NoCopyOnly
By default, Copy-SqlDatabase backups are backed up with COPY_ONLY, which avoids breaking the LSN backup chain. This parameter will set CopyOnly to $false.
 
.NOTES
Tags: Migration, DisasterRecovery, Backup, Restore
Author: Chrissy LeMaire (@cl), netnerds.net
Requires: sysadmin access on SQL Servers
Limitations: Doesn't cover what it doesn't cover (replication, certificates, etc)
    Sql Server 2000 databases cannot be directly migrated to Sql Server 2012 and above.
    Logins within Sql Server 2012 and above logins cannot be migrated to Sql Server 2008 R2 and below.
 
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 
.LINK
https://dbatools.io/Copy-SqlDatabase
 
.EXAMPLE
Copy-SqlDatabase -Source sqlserver2014a -Destination sqlserver2014b -Database TestDB -BackupRestore -NetworkShare \\fileshare\sql\migration
 
Migrates a single user database TestDB using Backup and restore from instance sqlserver2014a to sqlserver2014b. Backup files are stored in \\fileshare\sql\migration.
 
.EXAMPLE
Copy-SqlDatabase -Source sqlserver2014a -Destination sqlcluster -DetachAttach -Reattach
 
Databases will be migrated from sqlserver2014a to sqlcluster using the detach/copy files/attach method.The following will be perfomed: kick all users out of the database, detach all data/log files, move files across the network over an admin share (\\SqlSERVER\M$\MSSql...), attach file on destination server, reattach at source. If the database files (*.mdf, *.ndf, *.ldf) on *destination* exist and aren't in use, they will be overwritten.
 
 
.EXAMPLE
Copy-SqlDatabase -Source sqlserver2014a -Destination sqlcluster -Exclude Northwind, pubs -IncludeSupportDbs -Force -BackupRestore -NetworkShare \\fileshare\sql\migration
 
Migrates all user databases except for Northwind and pubs by using backup/restore (copy-only). Backup files are stored in \\fileshare\sql\migration. If the database exists on the destination, it will be dropped prior to attach.
 
It also includes the support databases (ReportServer, ReportServerTempDb, distribution).
 
#>

[CmdletBinding(DefaultParameterSetName = "DbMigration", SupportsShouldProcess = $true)]
Param (
[parameter(Position = 1, Mandatory = $false)]
[object]$Source,
[parameter(Position = 2, Mandatory = $true)]
[object]$Destination,
# Skips to 4 because dynamic param Databases is su pposed to go here but it doesn't work.

[Alias("All")]
[parameter(Position = 4, ParameterSetName = "DbBackup")]
[parameter(Position = 4, ParameterSetName = "DbAttachDetach")]
[switch]$AllDatabases,
[parameter(Position = 5, Mandatory = $true, ParameterSetName = "DbAttachDetach")]
[switch]$DetachAttach,
[parameter(Position = 6, ParameterSetName = "DbAttachDetach")]
[switch]$Reattach,
[parameter(Position = 7, Mandatory = $true, ParameterSetName = "DbBackup")]
[switch]$BackupRestore,
[parameter(Position = 8, Mandatory = $true, ParameterSetName = "DbBackup",
   HelpMessage = "Specify a valid network share in the format \\server\share that can be accessed by your account and both Sql Server service accounts.")]
[string]$NetworkShare,
[parameter(Position = 9, ParameterSetName = "DbBackup")]
[switch]$WithReplace,
[parameter(Position = 10, ParameterSetName = "DbBackup")]
[switch]$NoRecovery,
[parameter(Position = 11, ParameterSetName = "DbBackup")]
[parameter(Position = 12, ParameterSetName = "DbAttachDetach")]
[switch]$SetSourceReadOnly,
[Alias("ReuseFolderstructure")]
[parameter(Position = 13, ParameterSetName = "DbBackup")]
[parameter(Position = 14, ParameterSetName = "DbAttachDetach")]
[switch]$ReuseSourceFolderStructure,
[parameter(Position = 15, ParameterSetName = "DbBackup")]
[parameter(Position = 16, ParameterSetName = "DbAttachDetach")]
[switch]$IncludeSupportDbs,
[parameter(Position = 17, ParameterSetName = "DbBackup")]
[switch]$NoBackupCleanup,
[parameter(Position = 18)]
[System.Management.Automation.PSCredential]$SourceSqlCredential,
[parameter(Position = 19)]
[System.Management.Automation.PSCredential]$DestinationSqlCredential,
[parameter(Position = 20)]
[switch]$Force,
[parameter(ValueFromPipeline = $True)]
[object]$DbPipeline,
[parameter(Position = 21, ParameterSetName = "DbBackup")]
[ValidateRange(1, 64)]
[int]$NumberFiles = 3,
        [switch]$NoCopyOnly
)

DynamicParam { if ($source) { return Get-ParamSqlDatabases -SqlServer $source -SqlCredential $SourceSqlCredential -NoSystem } }

BEGIN
{
# Global Database Function
Function Get-SqlFileStructure
{
$dbcollection = @{ };
$databaseProgressbar = 0

foreach ($db in $databaselist)
{
Write-Progress -Id 1 -Activity "Processing database file structure" -PercentComplete ($databaseProgressbar / $dbcount * 100) -Status "Processing $databaseProgressbar of $dbcount"
$dbname = $db.name
Write-Verbose $dbname

$databaseProgressbar++
$dbstatus = $db.status.toString()
if ($dbstatus.StartsWith("Normal") -eq $false) { continue }
$destinationfiles = @{ }; $sourcefiles = @{ }

$where = "Filetype <> 'LOG' and Filetype <> 'FULLTEXT'"

$datarows = $dbfiletable.Tables.Select("dbname = '$dbname' and $where")

# Data Files
foreach ($file in $datarows)
{
# Destination File Structure
$d = @{ }
if ($ReuseSourceFolderStructure)
{
$d.physical = $file.filename
}
elseif ($WithReplace)
{
$name = $file.name
$destfile = $remotedbfiletable.Tables[0].Select("dbname = '$dbname' and name = '$name'")
$d.physical = $destfile.filename

if ($null -eq $d.physical)
{
$directory = Get-SqlDefaultPaths $destserver data
$filename = Split-Path $file.filename -Leaf
$d.physical = "$directory\$filename"
}
}
else
{
$directory = Get-SqlDefaultPaths $destserver data
$filename = Split-Path $file.filename -Leaf
$d.physical = "$directory\$filename"
}
$d.logical = $file.name

$d.remotefilename = Join-AdminUNC $destnetbios $d.physical
$destinationfiles.add($file.name, $d)

# Source File Structure
$s = @{ }
$s.logical = $file.name
$s.physical = $file.filename
$s.remotefilename = Join-AdminUNC $sourcenetbios $s.physical
$sourcefiles.add($file.name, $s)
}

# Add support for Full Text Catalogs in Sql Server 2005 and below
if ($sourceserver.VersionMajor -lt 10)
{
try
{
$fttable = $null = $sourceserver.Databases[$dbname].ExecuteWithResults('sp_help_fulltext_catalogs')
$allrows = $fttable.Tables[0].rows
}
catch
{
# Nothing, it's just not enabled
}

foreach ($ftc in $allrows)
{
# Destination File Structure
$d = @{ }
$pre = "sysft_"
$name = $ftc.name
$physical = $ftc.Path # RootPath
$logical = "$pre$name"
if ($ReuseSourceFolderStructure)
{
$d.physical = $physical
}
else
{
$directory = Get-SqlDefaultPaths $destserver data
if ($destserver.VersionMajor -lt 10) { $directory = "$directory\FTDATA" }
$filename = Split-Path($physical) -leaf
$d.physical = "$directory\$filename"
}
$d.logical = $logical
$d.remotefilename = Join-AdminUNC $destnetbios $d.physical
$destinationfiles.add($logical, $d)

# Source File Structure
$s = @{ }
$pre = "sysft_"
$name = $ftc.name
$physical = $ftc.Path # RootPath
$logical = "$pre$name"

$s.logical = $logical
$s.physical = $physical
$s.remotefilename = Join-AdminUNC $sourcenetbios $s.physical
$sourcefiles.add($logical, $s)
}
}

$where = "Filetype = 'LOG'"

$datarows = $dbfiletable.Tables[0].Select("dbname = '$dbname' and $where")

# Log Files
foreach ($file in $datarows)
{
$d = @{ }
if ($ReuseSourceFolderStructure)
{
$d.physical = $file.filename
}
elseif ($WithReplace)
{
$name = $file.name
$destfile = $remotedbfiletable.Tables[0].Select("dbname = '$dbname' and name = '$name'")
$d.physical = $destfile.filename

if ($null -eq $d.physical)
{
$directory = Get-SqlDefaultPaths $destserver data
$filename = Split-Path $file.filename -Leaf
$d.physical = "$directory\$filename"
}
}
else
{
$directory = Get-SqlDefaultPaths $destserver log
$filename = Split-Path $file.filename -Leaf
$d.physical = "$directory\$filename"
}
$d.logical = $file.name
$d.remotefilename = Join-AdminUNC $destnetbios $d.physical
$destinationfiles.add($file.name, $d)

$s = @{ }
$s.logical = $file.name
$s.physical = $file.filename
$s.remotefilename = Join-AdminUNC $sourcenetbios $s.physical
$sourcefiles.add($file.name, $s)
}

$location = @{ }
$location.add("Destination", $destinationfiles)
$location.add("Source", $sourcefiles)
$dbcollection.Add($($db.name), $location)
}

$filestructure = [pscustomobject]@{ "databases" = $dbcollection }
Write-Progress -id 1 -Activity "Processing database file structure" -Status "Completed" -Completed
return $filestructure
}
# Backup Restore
Function Backup-SqlDatabase
{
[CmdletBinding()]
param (
[object]$server,
[string]$dbname,
[string]$backupfile,
[int]$numberfiles
)

$server.ConnectionContext.StatementTimeout = 0
$backup = New-Object "Microsoft.SqlServer.Management.Smo.Backup"
$backup.Database = $dbname
$backup.Action = "Database"
$backup.CopyOnly = $CopyOnly
$val = 0

while ($val -lt $numberfiles)
{
$device = New-Object "Microsoft.SqlServer.Management.Smo.BackupDeviceItem"
$device.DeviceType = "File"
$device.Name = $backupfile.Replace(".bak", "-$val.bak")
$backup.Devices.Add($device)
$val++
}

$percent = [Microsoft.SqlServer.Management.Smo.PercentCompleteEventHandler] {
Write-Progress -id 1 -activity "Backing up database $dbname to $backupfile" -percentcomplete $_.Percent -status ([System.String]::Format("Progress: {0} %", $_.Percent))
}
$backup.add_PercentComplete($percent)
$backup.PercentCompleteNotification = 1
$backup.add_Complete($complete)

Write-Progress -id 1 -activity "Backing up database $dbname to $backupfile" -percentcomplete 0 -status ([System.String]::Format("Progress: {0} %", 0))
Write-Output "Backing up $dbname"

try
{
$backup.SqlBackup($server)
Write-Progress -id 1 -activity "Backing up database $dbname to $backupfile" -status "Complete" -Completed
Write-Output "Backup succeeded"
return $true
}
catch
{
Write-Progress -id 1 -activity "Backup" -status "Failed" -completed
Write-Exception $_
return $false
}
}

Function Restore-SqlDatabase
{
[CmdletBinding()]
param (
[object]$server,
[string]$dbname,
[string]$backupfile,
[object]$filestructure,
[int]$numberfiles
)

$servername = $server.name
$server.ConnectionContext.StatementTimeout = 0
$restore = New-Object Microsoft.SqlServer.Management.Smo.Restore

if ($WithReplace -or $server.databases[$dbname] -eq $null)
{
foreach ($file in $filestructure.databases[$dbname].destination.values)
{
$movefile = New-Object "Microsoft.SqlServer.Management.Smo.RelocateFile"
$movefile.LogicalFileName = $file.logical
$movefile.PhysicalFileName = $file.physical
$null = $restore.RelocateFiles.Add($movefile)
}
}

Write-Output "Restoring $dbname to $servername"

try
{

$percent = [Microsoft.SqlServer.Management.Smo.PercentCompleteEventHandler] {
Write-Progress -id 1 -activity "Restoring $dbname to $servername" -percentcomplete $_.Percent -status ([System.String]::Format("Progress: {0} %", $_.Percent))
}
$restore.add_PercentComplete($percent)
$restore.PercentCompleteNotification = 1
$restore.add_Complete($complete)
$restore.ReplaceDatabase = $true
$restore.Database = $dbname
$restore.Action = "Database"
$restore.NoRecovery = $NoRecovery
$val = 0
$filestodelete = @()

while ($val -lt $numberfiles)
{
$device = New-Object -TypeName Microsoft.SqlServer.Management.Smo.BackupDeviceItem
$device.devicetype = "File"
$device.name = $backupfile.Replace(".bak", "-$val.bak")
$restore.Devices.Add($device)
$filestodelete += $device.name
$val++
}

Write-Progress -id 1 -activity "Restoring $dbname to $servername" -percentcomplete 0 -status ([System.String]::Format("Progress: {0} %", 0))
$restore.sqlrestore($server)
Write-Progress -id 1 -activity "Restoring $dbname to $servername" -status "Complete" -Completed

If ($NoBackupCleanup -eq $false)
{
foreach ($backupfile in $filestodelete)
{
try
{
If (Test-Path $backupfile -ErrorAction Stop)
{
Write-Verbose "Deleting $backupfile"
Remove-Item $backupfile -ErrorAction Stop
}
}
catch
{
try
{
Write-Verbose "Trying alternate SQL method to delete $backupfile"
$sql = "EXEC master.sys.xp_delete_file 0, '$backupfile'"
Write-Debug $sql
$null = $server.ConnectionContext.ExecuteNonQuery($sql)
}
catch
{
Write-Warning "Cannot delete backup file $backupfile"

# Set NoBackupCleanup so that there's a warning at the end
$NoBackupCleanup = $true
}
}
}
}
return $true
}
catch
{
Write-Warning "Restore failed: $($_.Exception.InnerException.Message)"
Write-Exception $_
return $false
}
}

# Detach Attach
Function Dismount-SqlDatabase
{
[CmdletBinding()]
param (
[object]$server,
[string]$dbname
)

$database = $server.databases[$dbname]
if ($database.IsMirroringEnabled)
{
try
{
Write-Warning "Breaking mirror for $dbname"
$database.ChangeMirroringState([Microsoft.SqlServer.Management.Smo.MirroringOption]::Off)
$database.Alter()
$database.Refresh()
Write-Warning "Could not break mirror for $dbname. Skipping."

}
catch
{
Write-Exception $_
return $false
}
}

if ($database.AvailabilityGroupName.Length -gt 0)
{
$agname = $database.AvailabilityGroupName
Write-Output "Attempting remove from Availability Group $agname"
try
{
$server.AvailabilityGroups[$database.AvailabilityGroupName].AvailabilityDatabases[$dbname].Drop()
Write-Output "Successfully removed $dbname from detach from $agname on $($server.name)"
}
catch { Write-Error "Could not remove $dbname from $agname on $($server.name)"; Write-Exception $_; return $false }
}

Write-Output "Attempting detach from $dbname from $source"

####### Using Sql to detach does not modify the $database collection #######

$server.KillAllProcesses($dbname)

try
{
$sql = "ALTER DATABASE [$dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
Write-Verbose $sql
$null = $server.ConnectionContext.ExecuteNonQuery($sql)
Write-Output "Successfully set $dbname to single-user from $source"
}
catch
{
Write-Exception $_
}

try
{
$sql = "EXEC master.dbo.sp_detach_db N'$dbname'"
Write-Verbose $sql
$null = $server.ConnectionContext.ExecuteNonQuery($sql)
Write-Output "Successfully detached $dbname from $source"
}
catch
{
Write-Exception $_
}

}

Function Mount-SqlDatabase
{
[CmdletBinding()]
param (
[object]$server,
[string]$dbname,
[object]$filestructure,
[string]$dbowner
)

if ($server.Logins.Item($dbowner) -eq $null)
{
try
{
$dbowner = ($destserver.logins | Where-Object { $_.id -eq 1 }).Name
}
catch
{
$dbowner = "sa"
}
}
try
{
$null = $server.AttachDatabase($dbname, $filestructure, $dbowner, [Microsoft.SqlServer.Management.Smo.AttachOptions]::None)
return $true
}
catch { Write-Exception $_; return $false }
}

Function Start-SqlFileTransfer
{
<#
 
   SYNOPSIS
   Internal function. Uses BITS to transfer detached files (.mdf, .ndf, .ldf, and filegroups) to
   another server over admin UNC paths. Locations of data files are kept in the
   custom object generated by Get-SqlFileStructure
 
   #>

param (
[object]$filestructure,
[string]$dbname
)

$copydb = $filestructure.databases[$dbname]
$dbsource = $copydb.source
$dbdestination = $copydb.destination

foreach ($file in $dbsource.keys)
{
$remotefilename = $dbdestination[$file].remotefilename
$from = $dbsource[$file].remotefilename
try
{
if (Test-Path $from -pathtype container)
{
$null = New-Item -ItemType Directory -Path $remotefilename -Force
Start-BitsTransfer -Source "$from\*.*" -Destination $remotefilename

$directories = (Get-ChildItem -recurse $from | Where-Object { $_.PsIsContainer }).FullName
foreach ($directory in $directories)
{
$newdirectory = $directory.replace($from, $remotefilename)
$null = New-Item -ItemType Directory -Path $newdirectory -Force
Start-BitsTransfer -Source "$directory\*.*" -Destination $newdirectory
}
}
else
{
Write-Host "Copying $fn for $dbname"
Start-BitsTransfer -Source $from -Destination $remotefilename
}
$fn = Split-Path $($dbdestination[$file].physical) -leaf
}
catch
{
try
{
# Sometimes BITS trips out temporarily on cloned drives.
Start-BitsTransfer -Source $from -Destination $remotefilename
}
catch
{
Write-Warning "Start-BitsTransfer did not succeed. Now attempting with Copy-Item - no progress bar will be shown."
try
{
Copy-Item -Path $from -Destination $remotefilename -ErrorAction Stop
}
catch
{
Write-Warning "Access denied. This can happen for a number of reasons including issues with cloned disks."
Write-Warning "Alternatively, you may need to run PowerShell as Administrator, especially when running on localhost."
break
}
}
}
}
return $true
}

Function Start-SqlDetachAttach
{
<#
 
   .SYNOPSIS
   Internal function. Performs checks, then executes Dismount-SqlDatabase on a database, copies its files to the new server,
   then performs Mount-SqlDatabase. $sourceserver and $destserver are SMO server objects.
   $filestructure is a custom object generated by Get-SqlFileStructure
 
   #>

[CmdletBinding()]
param (
[object]$sourceserver,
[object]$destserver,
[object]$filestructure,
[string]$dbname
)

$destfilestructure = New-Object System.Collections.Specialized.StringCollection
$sourcefilestructure = New-Object System.Collections.Specialized.StringCollection
$dbowner = $sourceserver.databases[$dbname].owner

if ($dbowner -eq $null)
{
try
{
$dbowner = ($destserver.logins | Where-Object { $_.id -eq 1 }).Name
}
catch
{
$dbowner = "sa"
}
}

foreach ($file in $filestructure.databases[$dbname].destination.values) { $null = $destfilestructure.add($file.physical) }
foreach ($file in $filestructure.databases[$dbname].source.values) { $null = $sourcefilestructure.add($file.physical) }

$detachresult = Dismount-SqlDatabase $sourceserver $dbname

if ($detachresult)
{

$transfer = Start-SqlFileTransfer $filestructure $dbname
if ($transfer -eq $false) { Write-Warning "Could not copy files."; return "Could not copy files." }
$attachresult = Mount-SqlDatabase $destserver $dbname $destfilestructure $dbowner

if ($attachresult -eq $true)
{
# add to added dbs because ATTACH was successful
Write-Output "Successfully attached $dbname to $destination"
return $true
}
else
{
# add to failed because ATTACH was unsuccessful
Write-Warning "Could not attach $dbname."
return "Could not attach database."
}
}
else
{
# add to failed because DETACH was unsuccessful
Write-Warning "Could not detach $dbname."
return "Could not detach database."
}
}

}

PROCESS
{
$copyonly = !$NoCopyOnly
# Convert from RuntimeDefinedParameter object to regular array
$databases = $psboundparameters.Databases
$exclude = $psboundparameters.Exclude

if (($AllDatabases -or $IncludeSupportDbs -or $Databases) -and !$DetachAttach -and !$BackupRestore)
{
throw "You must specify -DetachAttach or -BackupRestore when migrating databases."
}

if ($DbPipeline.Length -gt 0)
{
$Source = $DbPipeline[0].parent.name
$Databases = $DbPipeline.name
}

if ($databases -contains "master" -or $databases -contains "msdb" -or $databases -contains "tempdb") { throw "Migrating system databases is not currently supported." }

if (!$AllDatabases -and !$IncludeSupportDbs -and !$Databases)
{
throw "You must specify a -AllDatabases or -Database to continue."
}

Write-Output "Attempting to connect to Sql Servers.."
$sourceserver = Connect-SqlServer -SqlServer $Source -SqlCredential $SourceSqlCredential
$destserver = Connect-SqlServer -SqlServer $Destination -SqlCredential $DestinationSqlCredential

if ($DetachAttach)
{
if ($sourceserver.netname -eq $env:COMPUTERNAME -or $destserver.netname -eq $env:COMPUTERNAME)
{
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "When running DetachAttach locally on the console, it's likely you'll need to Run As Administrator. Trying anyway."
}
}
}

$source = $sourceserver.DomainInstanceName
$destination = $destserver.DomainInstanceName

if ($NetworkShare.Length -gt 0)
{
if ($(Test-SqlPath -SqlServer $sourceserver -Path $NetworkShare) -eq $false)
{
Write-Warning "$Source may not be able to access $NetworkShare. Trying anyway."
}

if ($(Test-SqlPath -SqlServer $destserver -Path $NetworkShare) -eq $false)
{
Write-Warning "$Destination may not be able to access $NetworkShare. Trying anyway."
}

if ($networkshare.StartsWith('\\'))
{
$shareserver = ($networkshare -split "\\")[2]
$hostentry = ([Net.Dns]::GetHostEntry($shareserver)).HostName -split "\."

if ($shareserver -ne $hostentry[0])
{
Write-Warning "Using CNAME records for the network share may present an issue if an SPN has not been created. Trying anyway. If it doesn't work, use a different (A record) hostname."
}
}
}

Write-Output "Resolving NetBIOS names"
$sourcenetbios = Resolve-NetBiosName $sourceserver
$destnetbios = Resolve-NetBiosName $destserver

Write-Output "Performing SMO version check"
Invoke-SmoCheck -SqlServer $sourceserver
Invoke-SmoCheck -SqlServer $destserver

Write-Output "Checking to ensure the source isn't the same as the destination"
if ($source -eq $destination)
{
throw "Source and Destination Sql Servers instances are the same. Quitting."
}

if ($NetworkShare.Length -gt 0)
{
Write-Output "Checking to ensure network path is valid"
if (!($NetworkShare.StartsWith("\\")))
{
throw "Network share must be a valid UNC path (\\server\share)."
}

try
{
if (Test-Path $NetworkShare -ErrorAction Stop)
{
Write-Verbose "$networkshare share can be accessed."
}
}
catch
{
Write-Warning "$networkshare share cannot be accessed. Still trying anyway, in case the SQL Server service accounts have access."
}

}

Write-Output "Checking to ensure server is not SQL Server 7 or below"
if ($sourceserver.versionMajor -lt 8 -and $destserver.versionMajor -lt 8)
{
throw "This script can only be run on Sql Server 2000 and above. Quitting."
}

Write-Output "Checking to ensure detach/attach is not attempted on SQL Server 2000"
if ($destserver.versionMajor -lt 9 -and $DetachAttach)
{
throw "Detach/Attach not supported when destination Sql Server is version 2000. Quitting."
}

Write-Output "Checking to ensure SQL Server 2000 migration isn't directly attempted to SQL Server 2012"
if ($sourceserver.versionMajor -lt 9 -and $destserver.versionMajor -gt 10)
{
throw "Sql Server 2000 databases cannot be migrated to Sql Server versions 2012 and above. Quitting."
}

Write-Verbose "Warning if migration from 2005 to 2012 and above and attach/detach is used."
if ($sourceserver.versionMajor -eq 9 -and $destserver.versionMajor -gt 9 -and !$BackupRestore -and !$Force -and $DetachAttach)
{
throw "Backup and restore is the safest method for migrating from Sql Server 2005 to other Sql Server versions.
  Please use the -BackupRestore switch or override this requirement by specifying -Force."

}

if ($sourceserver.collation -ne $destserver.collation)
{
Write-Output "Warning on different collation"
Write-Warning "Collation on $Source, $($sourceserver.collation) differs from the $Destination, $($destserver.collation)."
}

Write-Output "Ensuring user databases exist (counting databases)"
$dbtotal = $sourceserver.Databases.count

if ($dbtotal -le 4)
{
throw "No user databases to migrate. Quitting."
}

Write-Output "Ensuring destination server version is equal to or greater than source"
if ([version]$sourceserver.ResourceVersionString -gt [version]$destserver.ResourceVersionString)
{
throw "Source Sql Server version build must be <= destination Sql Server for database migration."
}

# SMO's filestreamlevel is sometimes null
$sql = "select coalesce(SERVERPROPERTY('FilestreamConfiguredLevel'),0) as fs"
$sourcefilestream = $sourceserver.ConnectionContext.ExecuteScalar($sql)
$destfilestream = $destserver.ConnectionContext.ExecuteScalar($sql)
if ($sourcefilestream -gt 0 -and $destfilestream -eq 0) { $fswarning = $true }

Write-Output "Writing warning about filestream being enabled"
if ($fswarning)
{
Write-Warning "FILESTREAM enabled on $source but not $destination. Databases that use FILESTREAM will be skipped."
}

if ($DetachAttach -eq $true)
{
Write-Output "Checking access to remote directories"
$remotesourcepath = Join-AdminUNC $sourcenetbios (Get-SqlDefaultPaths $sourceserver data)

If ((Test-Path $remotesourcepath) -ne $true -and $DetachAttach)
{
Write-Warning "Can't access remote Sql directories on $source which is required to perform detach/copy/attach."
Write-Warning "You can manually try accessing $remotesourcepath to diagnose any issues."
Write-Warning "Halting database migration."
return
}

$remotedestpath = Join-AdminUNC $destnetbios (Get-SqlDefaultPaths $destserver data)
If ((Test-Path $remotedestpath) -ne $true -and $DetachAttach)
{
Write-Warning "Can't access remote Sql directories on $destination which is required to perform detach/copy/attach."
Write-Warning "You can manually try accessing $remotedestpath to diagnose any issues."
Write-Warning "Halting database migration."
return
}
}

if (($Databases -or $Exclude -or $IncludeSupportDbs) -and (!$DetachAttach -and !$BackupRestore))
{
throw "You did not select a migration method. Please use -BackupRestore or -DetachAttach"
}

if ((!$Databases -and !$AllDatabases -and !$IncludeSupportDbs) -and ($DetachAttach -or $BackupRestore))
{
throw "You did not select any databases to migrate. Please use -AllDatabases or -Databases or -IncludeSupportDbs"
}



Write-Output "Building database list"
$databaselist = New-Object System.Collections.ArrayList
$SupportDBs = "ReportServer", "ReportServerTempDB", "distribution"
foreach ($database in $sourceserver.Databases)
{
$dbname = $database.name
$dbowner = $database.Owner

if ($database.id -le 4) { continue }
if ($Databases -and $Databases -notcontains $dbname) { continue }
if ($IncludeSupportDBs -eq $false -and $SupportDBs -contains $dbname) { continue }
if ($IncludeSupportDBs -eq $true -and $SupportDBs -notcontains $dbname)
{
if ($AllDatabases -eq $false -and $Databases.length -eq 0) { continue }
}
$null = $databaselist.Add($database)
}

Write-Verbose "Performing count"
$dbcount = $databaselist.Count

Write-Output "Building file structure inventory for $dbcount databases"

if ($sourceserver.versionMajor -eq 8)
{
$sql = "select DB_NAME (dbid) as dbname, name, filename, CASE WHEN groupid = 0 THEN 'LOG' ELSE 'ROWS' END as filetype from sysaltfiles"
}
else
{
$sql = "SELECT db.name AS dbname, type_desc AS FileType, mf.name, Physical_Name AS filename FROM sys.master_files mf INNER JOIN sys.databases db ON db.database_id = mf.database_id"
}

$dbfiletable = $sourceserver.Databases['master'].ExecuteWithResults($sql)

if ($destserver.versionMajor -eq 8)
{
$sql = "select DB_NAME (dbid) as dbname, name, filename, CASE WHEN groupid = 0 THEN 'LOG' ELSE 'ROWS' END as filetype from sysaltfiles"
}
else
{
$sql = "SELECT db.name AS dbname, type_desc AS FileType, mf.name, Physical_Name AS filename FROM sys.master_files mf INNER JOIN sys.databases db ON db.database_id = mf.database_id"
}

$remotedbfiletable = $destserver.Databases['master'].ExecuteWithResults($sql)

$filestructure = Get-SqlFileStructure -sourceserver $sourceserver -destserver $destserver -databaselist $databaselist -ReuseSourceFolderStructure $ReuseSourceFolderStructure

$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
$started = Get-Date
$script:timenow = (Get-Date -uformat "%m%d%Y%H%M%S")

$alldbelapsed = [System.Diagnostics.Stopwatch]::StartNew()

if ($AllDatabases -or $Exclude.length -gt 0 -or $IncludeSupportDbs -or $Databases.length -gt 0)
{
foreach ($database in $databaselist)
{
$dbelapsed = [System.Diagnostics.Stopwatch]::StartNew()
$dbname = $database.name
$dbowner = $database.Owner

Write-Output "`n######### Database: $dbname #########"
$dbstart = Get-Date

if ($exclude -contains $dbname)
{
Write-Output "$dbname excluded. Skipping."
continue
}

Write-Verbose "Checking for accessibility"
if ($database.IsAccessible -eq $false)
{
Write-Warning "Skipping $dbname. Database is inaccessible."
continue
}

if ($fswarning)
{
$fsrows = $dbfiletable.Tables[0].Select("dbname = '$dbname' and FileType = 'FileStream'")

if ($fsrows.count -gt 0)
{
Write-Warning "Skipping $dbname (contains FILESTREAM)"
continue
}
}

if ($ReuseSourceFolderStructure)
{
$fgrows = $dbfiletable.Tables[0].Select("dbname = '$dbname' and FileType = 'ROWS'")[0]
$remotepath = Split-Path $fgrows
$remotepath = Join-AdminUNC $destnetbios $remotepath

if (!(Test-Path $remotepath)) { throw "Cannot resolve $remotepath. `n`nYou have specified ReuseSourceFolderStructure and exact folder structure does not exist. Halting script." }
}

Write-Verbose "Checking Availability Group status"
if ($database.AvailabilityGroupName.Length -gt 0 -and !$force -and $DetachAttach)
{
$agname = $database.AvailabilityGroupName
Write-Warning "Database is part of an Availability Group ($agname). Use -Force to drop from $agname and migrate. Alternatively, you can use the safer backup/restore method."
continue
}

$dbstatus = $database.status.toString()

if ($dbstatus.StartsWith("Normal") -eq $false)
{
Write-Warning "$dbname is not in a Normal state. Skipping."
continue
}

if ($database.ReplicationOptions -ne "None" -and $DetachAttach -eq $true)
{
Write-Warning "$dbname is part of replication. Skipping."
continue
}

if ($database.IsMirroringEnabled -and !$force -and $DetachAttach)
{
Write-Warning "Database is being mirrored. Use -Force to break mirror and migrate. Alternatively, you can use the safer backup/restore method."
continue
}

if (($destserver.Databases[$dbname] -ne $null) -and !$force -and !$WithReplace)
{
Write-Warning "Database exists at destination. Use -Force to drop and migrate. Aborting routine for this database."
continue
}
elseif ($destserver.Databases[$dbname] -ne $null -and $force)
{
If ($Pscmdlet.ShouldProcess($destination, "DROP DATABASE $dbname"))
{
Write-Output "$dbname already exists. -Force was specified. Dropping $dbname on $destination."
$dropresult = Remove-SqlDatabase $destserver $dbname

if ($dropresult -eq $false)
{
Write-Warning "Database could not be dropped. Aborting routine for this database"
continue
}
}
}

If ($Pscmdlet.ShouldProcess("console", "Showing start time"))
{
Write-Output "Started: $dbstart"
}

if ($sourceserver.versionMajor -ge 9)
{
$sourcedbownerchaining = $sourceserver.databases[$dbname].DatabaseOwnershipChaining
$sourcedbtrustworthy = $sourceserver.databases[$dbname].Trustworthy
$sourcedbbrokerenabled = $sourceserver.databases[$dbname].BrokerEnabled

}

$sourcedbreadonly = $sourceserver.Databases[$dbname].ReadOnly

if ($SetSourceReadOnly)
{
If ($Pscmdlet.ShouldProcess($source, "Set $dbname to read-only"))
{
Write-Output "Setting database to read-only"
$result = Update-SqldbReadOnly $sourceserver $dbname $true

if ($result -eq $false)
{
Write-Warning "Couldn't set database to read-only. Aborting routine for this database"
continue
}
}
}

if ($BackupRestore)
{
If ($Pscmdlet.ShouldProcess($destination, "Backup $dbname from $source and restoring."))
{
$filename = "$dbname-$timenow.bak"
$backupfile = Join-Path $networkshare $filename

$backupresult = Backup-SqlDatabase $sourceserver $dbname $backupfile $numberfiles

if ($backupresult -eq $false)
{
$serviceaccount = $sourceserver.ServiceAccount
Write-Warning "Backup Failed. Does Sql Server account $serviceaccount have access to $NetworkShare? Aborting routine for this database"
continue
}

$restoreresult = Restore-SqlDatabase $destserver $dbname $backupfile $filestructure $numberfiles

if ($restoreresult -eq $true)
{
Write-Output "Successfully restored $dbname to $destination"
}
else
{
if ($ReuseSourceFolderStructure)
{
Write-Warning "Failed to restore $dbname to $destination. You specified -ReuseSourceFolderStructure. Does the exact same destination directory structure exist?"
Write-Warning "Aborting routine for this database"
continue
}
else
{
Write-Warning "Failed to restore $dbname to $destination. Aborting routine for this database."
continue
}
}
}

$dbfinish = Get-Date

if ($norecovery -eq $false)
{
Write-Output "Updating database owner"
$result = Update-Sqldbowner $sourceserver $destserver -dbname $dbname
}

}

if ($DetachAttach)
{
$sourcefilestructure = New-Object System.Collections.Specialized.StringCollection
foreach ($file in $filestructure.databases[$dbname].source.values)
{
$null = $sourcefilestructure.add($file.physical)
}

$dbowner = $sourceserver.databases[$dbname].owner

if ($dbowner -eq $null)
{
$dbowner = Get-SaLoginName -SqlServer $destserver
}

If ($Pscmdlet.ShouldProcess($destination, "Detach $dbname from $source and attach, then update dbowner"))
{
$migrationresult = Start-SqlDetachAttach $sourceserver $destserver $filestructure $dbname

$dbfinish = Get-Date

if ($reattach -eq $true)
{
$null = ($sourceserver.databases).Refresh()
$result = Mount-SqlDatabase $sourceserver $dbname $sourcefilestructure $dbowner

if ($result -eq $true)
{
$sourceserver.databases[$dbname].DatabaseOwnershipChaining = $sourcedbownerchaining
$sourceserver.databases[$dbname].Trustworthy = $sourcedbtrustworthy
$sourceserver.databases[$dbname].BrokerEnabled = $sourcedbbrokerenabled
$sourceserver.databases[$dbname].alter()

if ($SetSourceReadOnly)
{
$null = Update-SqldbReadOnly $sourceserver $dbname $true
}
else
{
$null = Update-SqldbReadOnly $sourceserver $dbname $sourcedbreadonly
}

Write-Output "Successfully reattached $dbname to $source"
}
else
{
Write-Warning "Could not reattach $dbname to $source."
}
}


if ($migrationresult -eq $true)
{
Write-Output "Successfully attached $dbname to $destination"
}
else
{
Write-Warning "Failed to attach $dbname to $destination. Aborting routine for this database."
continue
}
}
}

# restore poentially lost settings

if ($destserver.versionMajor -ge 9 -and $norecovery -eq $false)
{
if ($sourcedbownerchaining -ne $destserver.databases[$dbname].DatabaseOwnershipChaining)
{
If ($Pscmdlet.ShouldProcess($destination, "Updating DatabaseOwnershipChaining on $dbname"))
{
try
{
$destserver.databases[$dbname].DatabaseOwnershipChaining = $sourcedbownerchaining
$destserver.databases[$dbname].Alter()
Write-Output "Successfully updated DatabaseOwnershipChaining for $sourcedbownerchaining on $dbname on $destination"
}
catch
{
Write-Warning "Failed to update DatabaseOwnershipChaining for $sourcedbownerchaining on $dbname on $destination"
Write-Exception $_
}
}
}

if ($sourcedbtrustworthy -ne $destserver.databases[$dbname].Trustworthy)
{
If ($Pscmdlet.ShouldProcess($destination, "Updating Trustworthy on $dbname"))
{
try
{
$destserver.databases[$dbname].Trustworthy = $sourcedbtrustworthy
$destserver.databases[$dbname].alter()
Write-Output "Successfully updated Trustworthy to $sourcedbtrustworthy for $dbname on $destination"
}
catch
{
Write-Warning "Failed to update Trustworthy to $sourcedbtrustworthy for $dbname on $destination"
Write-Exception $_
}
}
}

if ($sourcedbbrokerenabled -ne $destserver.databases[$dbname].BrokerEnabled)
{
If ($Pscmdlet.ShouldProcess($destination, "Updating BrokerEnabled on $dbname"))
{
try
{
$destserver.databases[$dbname].BrokerEnabled = $sourcedbbrokerenabled
$destserver.databases[$dbname].Alter()
Write-Output "Successfully updated BrokerEnabled to $sourcedbbrokerenabled for $dbname on $destination"
}
catch
{
Write-Warning "Failed to update BrokerEnabled to $sourcedbbrokerenabled for $dbname on $destination"
Write-Exception $_
}
}
}
}

if ($sourcedbreadonly -ne $destserver.databases[$dbname].ReadOnly -and $norecovery -eq $false)
{
If ($Pscmdlet.ShouldProcess($destination, "Updating ReadOnly status on $dbname"))
{
$update = Update-SqldbReadOnly $destserver $dbname $sourcedbreadonly
if ($update -eq $true)
{
"Successfully updated readonly status on $dbname"
}
else
{
Write-Warning "Failed to update ReadOnly status on $dbname"
Write-Exception $_
}
}
}


If ($Pscmdlet.ShouldProcess("console", "Showing elapsed time"))
{
$dbtotaltime = $dbfinish - $dbstart
$dbtotaltime = ($dbtotaltime.toString().Split(".")[0])

Write-Output "Finished: $dbfinish"
Write-Output "Elapsed time: $dbtotaltime"
}
} # end db by db processing
}
}

END
{
If ($Pscmdlet.ShouldProcess("console", "Showing migration time elapsed"))
{
$totaltime = ($elapsed.Elapsed.toString().Split(".")[0])

Write-Output "`nDatabase migration finished"
Write-Output "Migration started: $started"
Write-Output "Migration completed: $(Get-Date)"
Write-Output "Total Elapsed time: $totaltime"

if ($networkshare.length -gt 0 -and $NoBackupCleanup)
{
Write-Warning "Backups still exist at $networkshare."
}
}

$sourceserver.ConnectionContext.Disconnect()
$destserver.ConnectionContext.Disconnect()
}
}