functions/Remove-SqlDatabaseSafely.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
Function Remove-SqlDatabaseSafely
{
<#
.SYNOPSIS
Safely removes a SQL Database and creates an Agent Job to restore it

.DESCRIPTION
Performs a DBCC CHECKDB on the database, backs up the database with Checksum and verify only to a Final Backup location, creates an Agent Job to restore from that backup, Drops the database, runs the agent job to restore the database,
performs a DBCC CHECKDB and drops the database

By default the initial DBCC CHECKDB is performed
By default the jobs and databases are created on the same server. Use -DestinationServer to use a seperate server

It will start the SQL Agent Service on the Destination Server if it is not running

.PARAMETER SqlServer
The SQL Server instance holding the databases to be removed.You must have sysadmin access and server version must be SQL Server version 2000 or higher.

.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.

$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter.

Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user.

.PARAMETER DestinationServer
If specified this is the server that the Agent Jobs will be created on. By default this is the same server as the SQLServer.You must have sysadmin access and server version must be SQL Server version 2000 or higher.

.PARAMETER DBNames
The database name to remove or an array of database names eg $DBNames = 'DB1','DB2','DB3'

.PARAMETER NoDBCCCheck
If this switch is used the initial DBCC CHECK DB will be skipped. This will make the process quicker but will also create an agent job to restore a database backup containing a corrupt database.
A second DBCC CHECKDB is performed on the restored database so you will still be notified BUT USE THIS WITH CARE

.PARAMETER BackupFolder
Final (Golden) Backup Folder for storing the backups. Be Careful if you are using a source and destination server that you use the full UNC path eg \\SERVER1\BACKUPSHARE\

.PARAMETER JobOwner
The account that will own the Agent Jobs - Defaults to sa

.PARAMETER UseDefaultFilePaths
Use the instance default file paths for the mdf and ldf files to restore the database if not set will use the original file paths

.PARAMETER DBCCErrorFolder
FolderPath for DBCC Error Output - defaults to C:\temp

.PARAMETER AllDbs
Runs the script for every user databases on a server - Useful when decomissioning a server - That would need a DestinationServer set

.NOTES
Original Author: Rob Sewell @SQLDBAWithBeard, sqldbawithabeard.com
                 With huge thanks to Grant Fritchey and his verify your backups video
                 Take a look its only 3 minutes long
                 http://sqlps.io/backuprant

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/Remove-SqlDatabaseSafely

.EXAMPLE
Remove-SqlDatabaseSafely -SqlServer 'Fade2Black' -DBNames RideTheLightning -BackupFolder 'C:\MSSQL\Backup\Rationalised - DO NOT DELETE'

For the database RideTheLightning on the server Fade2Black Will perform a DBCC CHECKDB and if there are no errors
backup the database to the folder C:\MSSQL\Backup\Rationalised - DO NOT DELETE. It will then create an Agent Job to restore the database
from that backup. It will drop the database, run the agent job to restore it, perform a DBCC ChECK DB and then drop the database.

Any DBCC errors will be located in C:\Temp

.EXAMPLE
$DBNames = 'DemoNCIndex','RemoveTestDatabase'
Remove-SqlDatabaseSafely -SqlServer 'Fade2Black' -DBNames $DBNames -BackupFolder 'C:\MSSQL\Backup\Rationalised - DO NOT DELETE'

For the databases 'DemoNCIndex','RemoveTestDatabase' on the server Fade2Black Will perform a DBCC CHECKDB and if there are no errors
backup the database to the folder C:\MSSQL\Backup\Rationalised - DO NOT DELETE. It will then create an Agent Job for each database
to restore the database from that backup. It will drop the database, run the agent job, perform a DBCC ChECK DB and then drop the database

Any DBCC errors will be located in C:\Temp

.EXAMPLE
Remove-SqlDatabaseSafely -SqlServer 'Fade2Black' -DestinationServer JusticeForAll -DBNames RideTheLightning -BackupFolder '\\BACKUPSERVER\BACKUPSHARE\MSSQL\Rationalised - DO NOT DELETE'

For the database RideTheLightning on the server Fade2Black Will perform a DBCC CHECKDB and if there are no errors
backup the database to the folder \\BACKUPSERVER\BACKUPSHARE\MSSQL\Rationalised - DO NOT DELETE It will then create an Agent Job on the server
JusticeForAll to restore the database from that backup. It will drop the database on Fade2Black, run the agent job to restore it on JusticeForAll,
perform a DBCC ChECK DB and then drop the database

Any DBCC errors will be located in C:\Temp
.EXAMPLE
Remove-SqlDatabaseSafely -SqlServer IronMaiden -DBNames $DBNames -DestinationServer TheWildHearts -DBCCErrorFolder C:\DBCCErrors -BackupFolder z:\Backups -NoDBCCCheck -UseDefaultFilePaths -JobOwner 'THEBEARD\Rob'

For the databases $DBNames on the server IronMaiden Will NOT perform a DBCC CHECKDB
It will backup the databases to the folder Z:\Backups It will then create an Agent Job on the server with a Job Owner of THEBEARD\Rob
TheWildHearts to restore the database from that backup using the instance default filepaths.
It will drop the database on IronMaiden, run the agent job to restore it on TheWildHearts using the default file paths for the instance, perform
a DBCC ChECK DB and then drop the database

Any DBCC errors will be located in C:\Temp

#>

    [CmdletBinding(SupportsShouldProcess = $true)]
    Param (
        ## Source SQL Server - Requires sysadmin access
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlInstance")]
        [object]$SqlServer,
        ## SQL Login for servers

        [parameter(Mandatory = $false)]
        [object]$SqlCredential,
        ## Destination SQL Server for agent job, restores and DBCC - Requires ssyadmin access

        [parameter(Mandatory = $false)]
        [object]$DestinationServer = $SqlServer,
        ## The Name of the database or an array of database names eg $DBNames = 'DB1','DB2','DB3' For all users databases on a server use -AllDBs

        [parameter(Mandatory = $false)]
        $DBNames,
        ## If this switch is used the initial DBCC CHECK DB will be skipped. This will make the process quicker but will also create an agent job to restore a database backup containing a corrupt database.

        ## A second DBCC CHECKDB is performed on the restored database so you will still be notified BUT USE THIS WITH CARE

        [parameter(Mandatory = $false)]
        [switch]$NoDBCCCheck,
        ## Final (Golden) Backup Folder for storing the backups. Be Careful if you are using a source and destination server that you use the full UNC path eg \\SERVER1\BACKUPSHARE\

        [parameter(Mandatory = $true)]
        [string]$BackupFolder,
        ## Category Name for Agent Jobs -- defaults to Rationalisation

        [parameter(Mandatory = $false)]
        [string]$CategoryName = 'Rationalisation',
        ## Agent Job Owner - defaults to sa

        [parameter(Mandatory = $false)]
        [string]$JobOwner = 'sa',
        ## Use the instance default file paths for the mdf and ldf files to restore the database if not set will use the original file paths

        [switch]$UseDefaultFilePaths,
        ## FolderPath for DBCC Error Output - defaults to C:\temp

        [parameter(Mandatory = $false)]
        [string]$DBCCErrorFolder = 'C:\temp',
        # no trailing slash

        ## Performs action for All user databases on a server -

        [parameter(Mandatory = $false)]
        [switch]$AllDbs
    )
    DynamicParam { if ($sqlserver) { return Get-ParamSqlLogins -SqlServer $sqlserver -SqlCredential $SqlCredential } }
    BEGIN
    {
        # Load SMO
        
        [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
        [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO.SMOExtended")
        # please continue to use these variable names for consistency
        $sourceserver = Connect-SqlServer -SqlServer $sqlserver -SqlCredential $SqlCredential -ParameterConnection
        ## Incase we want to add this as an option later
        $BackupCompression = 'Default' # 'Default' 'on' or 'off'
        
        if ($SqlServer -ne $DestinationServer)
        {
            $DestServer = Connect-SqlServer -SqlServer $DestinationServer -SqlCredential $SqlCredential
        }
        else
        {
            $DestinationServer = $SqlServer
            $DestServer = $sourceserver
        }
        if ($AllDbs)
        {
            $DBNames = ($sourceserver.Databases | Where-Object{ $_.IsSystemObject -eq $false }).Name
        }
        $source = $sourceserver.DomainInstanceName
        $JobName = "Rationalised Final Database Restore for $DBName"
        $JobStepName1 = "Restore the $DBName database from Final Backup"
        if (!($DestServer.Logins | Where-Object{ $_.Name -eq $JobOwner }))
        {
            Write-Warning "$DestinationServer Doesnot contain the login $JobOwner - Please fix and try again - Aborting"
            break
        }
        if ($DestinationServer -notcontains '\')
        {
            $AgentService = Get-Service -ComputerName $DestinationServer -Name SQLSERVERAGENT
            if ($AgentService.Status -ne 'Running')
            {
                $AgentService.Start()
            }
            Start-Sleep -Seconds 5
            $AgentService = Get-Service -ComputerName $DestinationServer -Name SQLSERVERAGENT
            if ($AgentService.Status -ne 'Running')
            {
                Write-Warning "Cannot start Agent Service on $DestinationServer - Aborting"
                break
            }
        }
        Else
        {
            $s = $destinationserver.Split('\')[0]
            $Agent = $destinationserver.Split('\')[1]
            $AgentService = Get-Service -ComputerName $s -Name SQLAGENT*$Agent
            if ($AgentService.Status -ne 'Running')
            {
                $AgentService.Start()
            }
            Start-Sleep -Seconds 5
            $AgentService = Get-Service -ComputerName $s -Name SQLAGENT*$Agent
            if ($AgentService.Status -ne 'Running')
            {
                Write-Warning "Cannot start Agent Service on $DestinationServer - Aborting"
                break
            }
        }
        
        
<# if(!(Test-Path $BackupFolder))
{
Write-Warning "Can't access $BackupFolder Please check if $env:USERDOMAIN\$env:USERNAME has permissions"
break
}#>

        
        function Start-DBCCCheck
        {
            param ([string]$SQLServer,
                [string]$DBName)
            $srv = New-Object Microsoft.SqlServer.Management.Smo.Server $SQLServer
            $DB = $Srv.Databases[$DBName]
            $DBCCGood = $True
            try
            {
                $DB.CheckTables('None')
                Write-Output "DBCC CHECKDB finished successfully for $DBName on $SQLServer"
            }
            catch
            {
                $date = Get-Date -Format ddMMyyyy_HHmmss
                [string]$ErrorFile = $DBCCErrorFolder + '\DBCC_Errors_for_' + $DBName + '_' + $SQLServer + '_' + $Date + '.txt'
                
                if (!(Test-Path $DBCCErrorFolder))
                {
                    New-Item -Path $DBCCErrorFolder -ItemType Directory
                }
                $_.Exception.InnerException | Out-File -Append $ErrorFile
                $_.Exception.InnerException.InnerException.InnerException | Out-File -Append $ErrorFile
                Write-Warning "UH-OH!! $DBName DBCC CHECKDB Error - Check this file $ErrorFile"
                $DBCCGood = $false
                return $DBCCGood
                continue
            }
        }
        function New-SQLAgentJobCategory
        {
            param ([string]$CategoryName,
                $JobServer)
            if (!$JobServer.JobCategories[$CategoryName])
            {
                try
                {
                    Write-Output "Creating Agent Job Category $CategoryName"
                    $Category = New-Object Microsoft.SqlServer.Management.Smo.Agent.JobCategory
                    $Category.Parent = $JobServer
                    $Category.Name = $CategoryName
                    $Category.Create()
                    Write-Output "Created Agent Job Category $CategoryName"
                }
                catch
                {
                    Write-Output "Creating Agent Job Category $CategoryName"
                    Write-Warning "FAILED : To Create Agent Job Category $CategoryName - Aborting"
                    Write-Exception $_
                    continue
                }
            }
        }
        Function Restore-Database
        {
<#
    .SYNOPSIS
    Internal function. Restores .bak file to SQL database. Creates db if it doesn't exist. $filestructure is
    a custom object that contains logical and physical file locations.

    ALTERED To Add TSQL switch and remove norecovery switch default
#>

            [CmdletBinding()]
            param (
                [Parameter(Mandatory = $true)]
                [ValidateNotNullOrEmpty()]
                [Alias('ServerInstance', 'SqlInstance')]
                [object]$SqlServer,
                [Parameter(Mandatory = $true)]
                [ValidateNotNullOrEmpty()]
                [string]$dbname,
                [Parameter(Mandatory = $true)]
                [ValidateNotNullOrEmpty()]
                [string]$backupfile,
                [string]$filetype = 'Database',
                [Parameter(Mandatory = $true)]
                [ValidateNotNullOrEmpty()]
                [object]$filestructure,
                [switch]$norecovery,
                [System.Management.Automation.PSCredential]$SqlCredential,
                [switch]$TSQL = $false
            )
            
            $SQLServer = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
            $SQLServername = $SQLServer.name
            $SQLServer.ConnectionContext.StatementTimeout = 0
            $restore = New-Object 'Microsoft.SqlServer.Management.Smo.Restore'
            $restore.ReplaceDatabase = $true
            
            foreach ($file in $filestructure.values)
            {
                $movefile = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile'
                $movefile.LogicalFileName = $file.logical
                $movefile.PhysicalFileName = $file.physical
                $null = $restore.RelocateFiles.Add($movefile)
            }
            
            try
            {
                if ($TSQL)
                {
                    $restore.PercentCompleteNotification = 1
                    $restore.add_Complete($complete)
                    $restore.ReplaceDatabase = $true
                    $restore.Database = $dbname
                    $restore.Action = $filetype
                    $restore.NoRecovery = $norecovery
                    $device = New-Object -TypeName Microsoft.SqlServer.Management.Smo.BackupDeviceItem
                    $device.name = $backupfile
                    $device.devicetype = 'File'
                    $restore.Devices.Add($device)
                    $RestoreScript = $restore.script($SQLServer)
                    return $RestoreScript
                }
                else
                {
                    $percent = [Microsoft.SqlServer.Management.Smo.PercentCompleteEventHandler] {
                        Write-Progress -id 1 -activity "Restoring $dbname to $SQLServername" -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 = $filetype
                    $restore.NoRecovery = $norecovery
                    $device = New-Object -TypeName Microsoft.SqlServer.Management.Smo.BackupDeviceItem
                    $device.name = $backupfile
                    $device.devicetype = 'File'
                    $restore.Devices.Add($device)
                    
                    Write-Progress -id 1 -activity "Restoring $dbname to $SQLServername" -percentcomplete 0 -status ([System.String]::Format("Progress: {0} %", 0))
                    $restore.sqlrestore($SQLServer)
                    Write-Progress -id 1 -activity "Restoring $dbname to $SQLServername" -status 'Complete' -Completed
                    
                    return $true
                }
            }
            catch
            {
                Write-Error "Restore failed: $($_.Exception)"
                return $false
            }
        }
        
    }
    PROCESS
    {
        $Start = Get-Date
        Write-Output "Starting Rationalisation Script for $DBNames on $SQLServer to $DestinationServer"
        Write-Output "Started at $Start"
        foreach ($DBName in $DBNAMES)
        {
            $DBCCGood = $True
            $DB = $sourceserver.Databases[$DBName]
            $JobName = "Rationalised Database Restore Script for $DBName"
            $JobStepName1 = "Restore the $DBName database from Final Backup"
            if (!$DB)
            {
                Write-Warning "$DBName does not exist on $SQLServer"
                continue
            }
            $JobServer = $DestServer.JobServer
            If ($JobServer.Jobs[$JobName])
            {
                Write-Warning "FAILED : The Job $JobName already exists. Have you done this before? Rename the existing job and try again"
                continue
            }
            Write-Output "Starting Rationalisation of $DBName"
            ## If we want to DBCC before to abort if we have a corrupt database to start with
            if (!$NoDBCCCheck)
            {
                Write-Output "Starting DBCC CHECKDB for $DBName on $SQLServer"
                $DBCCGood = Start-DBCCCheck -SQLserver $SQLServer -DBName $DBName
            }
            ## If we have no DBCC errors or we havent run DBCC
            if ($DBCCGood)
            {
                Write-Output "DBCC Completed for $DBName on $SQLServer without Errors"
                Write-Output "Starting Backup for $DBName on $SqlServer"
                ## Take a Backup
                try
                {
                    $Backup = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Backup
                    $Backup.Action = [Microsoft.SQLServer.Management.SMO.BackupActionType]::Database
                    $Backup.BackupSetDescription = "Final Full Backup of $DBName Prior to Dropping"
                    $Backup.Database = $DBName
                    $Backup.Checksum = $True
                    if ($sourceserver.versionMajor -gt 9)
                    {
                        $Backup.CompressionOption = $BackupCompression
                    }
                    $FileName = $BackupFolder + '\' + $DbName + '_' + 'Final_Before_Drop_' + [DateTime]::Now.ToString('yyyyMMdd_HHmmss') + '.bak'
                    $DeviceType = [Microsoft.SqlServer.Management.Smo.DeviceType]::File
                    $BackupDevice = New-Object -TypeName Microsoft.SQLServer.Management.Smo.BackupDeviceItem($FileName, $DeviceType)
                    $Backup.Devices.Add($BackupDevice)
                    #Progress
                    $percent = [Microsoft.SqlServer.Management.Smo.PercentCompleteEventHandler] {
                        Write-Progress -id 1 -activity "Backing up database $DBName on $SqlServer to $FileName" -percentcomplete $_.Percent -status ([System.String]::Format("Progress: {0} %", $_.Percent))
                    }
                    $Backup.add_PercentComplete($percent)
                    $Backup.add_Complete($complete)
                    Write-Progress -id 1 -activity "Backing up database $DBName on $SqlServer to $FileName" -percentcomplete 0 -status ([System.String]::Format("Progress: {0} %", 0))
                    $Backup.SqlBackup($sourceserver)
                    $Backup.Devices.Remove($BackupDevice)
                    Write-Progress -id 1 -activity "Backing up database $DBName on $SqlServer to $FileName" -status "Complete" -Completed
                    Write-Output "Backup Completed for $DBName on $SqlServer "
                    Write-Output "Running Restore Verify only on Backup of $DBName on $SQLServer"
                    try
                    {
                        $restoreverify = New-Object 'Microsoft.SqlServer.Management.Smo.Restore'
                        $restoreverify.Database = $DBName
                        $restoreverify.Devices.AddDevice($FileName, $DeviceType)
                        $result = $restoreverify.SqlVerify($SqlServer)
                        if (!$Result)
                        {
                            Write-Warning "FAILED : Restore Verify Only failed for $FileName on $SqlServer - Aborting"
                            continue
                        }
                        Write-Output "Restore Verify Only for $FileName Succeeded "
                    }
                    catch
                    {
                        Write-Warning "FAILED : Restore Verify Only failed for $FileName on $SqlServer - Aborting"
                        Write-Exception $_
                        continue
                    }
                }
                catch
                {
                    Write-Warning "FAILED : To backup database $DBName on $SqlServer - Aborting"
                    Write-Exception $_
                    continue
                } # End Backup
                ## SUGGESTION - Could add restore database onto another server
                ## SUGGESTION - COuld also add create the agent job on another server
                Write-Output "Creating Automated Restore Job from Golden Backup for $DBName on $DestinationServer "
                try
                {
                    ## Create an agent job to restore the database
                    $Job = New-Object Microsoft.SqlServer.Management.Smo.Agent.Job $JobServer, $JobName
                    $Job.Name = $JobName
                    $Job.OwnerLoginName = $JobOwner
                    $Job.Description = "This job will restore the $DBName database using the final backup located at $FileName"
                    ## Create a Job Category
                    if (!$JobServer.JobCategories[$CategoryName])
                    {
                        New-SQLAgentJobCategory -JobServer $JobServer -CategoryName $CategoryName
                    }
                    $Job.Category = $CategoryName
                    try
                    {
                        Write-Output "Created Agent Job $JobName on $DestinationServer "
                        $Job.Create()
                    }
                    catch
                    {
                        Write-Warning "FAILED : To Create Agent Job $JobName on $DestinationServer - Aborting"
                        Write-Exception $_
                        continue
                    }
                    ## Create Job Step
                    ## Aarons Suggestion: In the restore script, add a comment block that tells the last known size of each file in the database.
                    ## Suggestion check for disk space before restore
                    ## Create Restore Script
                    try
                    {
                        $restore = New-Object 'Microsoft.SqlServer.Management.Smo.Restore'
                        $device = New-Object -TypeName Microsoft.SqlServer.Management.Smo.BackupDeviceItem $FileName, 'FILE'
                        $restore.Devices.Add($device)
                        try { $filelist = $restore.ReadFileList($DestServer) }
                        catch { throw 'File list could not be determined. This is likely due to connectivity issues or tiemouts with the SQL Server, the database version is incorrect, or the SQL Server service account does not have access to the file share. Script terminating.' }
                        $ReuseSourceFolderStructure = $true
                        if ($UseDefaultFilePaths)
                        {
                            $ReuseSourceFolderStructure = $false
                        }
                        $filestructure = Get-OfflineSqlFileStructure $DestServer $DBName $filelist $ReuseSourceFolderStructure
                        if ($filestructure -eq $false)
                        {
                            Write-Warning "$dbname contains FILESTREAM and filestreams are not supported by destination server. Skipping."
                            $skippedb[$dbname] = "Database contains FILESTREAM and filestreams are not supported by destination server."
                            continue
                        }
                        $JobStepCommmand = Restore-Database $DestServer $DBName $FileName "Database" $filestructure -TSQL -ErrorAction Stop
                        $JobStep = new-object Microsoft.SqlServer.Management.Smo.Agent.JobStep $Job, $JobStepName1
                        $JobStep.SubSystem = 'TransactSql' # 'PowerShell'
                        $JobStep.DatabaseName = 'master'
                        $JobStep.Command = $JobStepCommmand
                        $JobStep.OnSuccessAction = 'QuitWithSuccess'
                        $JobStep.OnFailAction = 'QuitWithFailure'
                        $JobStep.Create()
                        $JobStartStepid = $JobStep.ID
                        Write-Output "Created Agent JobStep $JobStepName1 on $DestinationServer "
                    }
                    catch
                    {
                        Write-Warning "FAILED : To Create Agent JobStep $JobStepName1 on $DestinationServer - Aborting"
                        Write-Exception $_
                        continue
                    }
                    $Job.ApplyToTargetServer($DestinationServer)
                    $Job.StartStepID = $JobStartStepid
                    $Job.Alter()
                }
                catch
                {
                    Write-Warning "FAILED : To Create Agent Job $JobName on $DestinationServer - Aborting"
                    Write-Exception $_
                    continue
                }
                ## Create Restore Script and add to Job Step
                
                
                ## Drop the database
                try
                {
                    $sourceserver.KillAllProcesses($dbname)
                    $DB.drop()
                    Write-Output "Dropped $DBName Database on $SQLServer prior to running the Agent Job"
                }
                catch
                {
                    Write-Warning "FAILED : To Drop database $DBName on $SQLServer - Aborting"
                    Write-Exception $_
                    continue
                }
                ## Remove the Logins who only have permissions to this database
                ## Run the restore job to restore it
                Write-Output "Starting $JobName on $DestinationServer "
                try
                {
                    $JOb = $DestServer.JobServer.Jobs[$JobName]
                    $Job.Start()
                    $Status = $Job.CurrentRunStatus
                    While ($Status -ne 'Idle')
                    {
                        Write-Output "Restore Job for $DBName on $DestinationServer is $Status"
                        $Job.Refresh()
                        $Status = $Job.CurrentRunStatus
                        Start-Sleep -Seconds 5
                    }
                    Write-Output "Restore JOb $JobName has completed on $DestinationServer "
                    Start-Sleep -Seconds 5 ## This is required to ensure the next DBCC Check succeeds
                }
                catch
                {
                    Write-Warning "FAILED : Restore JOb $JobName failed on $DestinationServer - Aborting"
                    Write-Exception $_
                    continue
                }
                If ($JOb.LastRunOutcome -ne 'Succeeded')
                {
                    Write-Warning "FAILED : Restore JOb $JobName failed on $DestinationServer - Aborting"
                    Write-Warning "Check the Agent Job History on $DestinationServer - If you have SSMS2016 July release or later"
                    Write-Warning "Get-SqlAgentJobHistory -JobName $jobName -ServerInstance $DestinationServer -OutcomesType Failed "
                    continue
                }
                
                ## Run a DBCC No choice here
                Write-Output "Starting DBCC CHECKDB for $DBName on $DestinationServer"
                Start-DBCCCheck -SQLserver $DestinationServer -DBName $DBName
                ## Drop the database
                try
                {
                    $srv = New-Object Microsoft.SqlServer.Management.Smo.Server $DestinationServer
                    $srv.KillAllProcesses($dbname)
                    $DB = $Srv.Databases[$DBName]
                    $DB.drop()
                    Write-Output "Dropped $DBName Database on $DestinationServer"
                }
                catch
                {
                    Write-Warning "FAILED : To Drop database $DBName on $DestinationServer - Aborting"
                    Write-Exception $_
                    continue
                }
                Write-Output "Rationalisation Finished for $DBName"
                ## Finish
                continue
            } # End DBCC if
            else
            {
                Write-Warning "DBCC errors for $DBName - So Aborting"
                continue
            } # End DBCC else
        } #End DB foreach
        
    } # End Process
    END
    {
        $sourceserver.ConnectionContext.Disconnect()
        $DestServer.ConnectionContext.Disconnect()
        If ($Pscmdlet.ShouldProcess("console", "Showing final message"))
        {
            $End = Get-Date
            Write-Output "Rationalisation Finished for $DBNames on $SQLServer to $DestinationServer"
            Write-Output "Finished at $End"
            $Duration = $End - $Start
            Write-Output "Script Duration : - $Duration"
        }
    }
}