|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Gets the list of files contained in a SQL Server backup file. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + This command reads and returns the list of database files contained in |
| 7 | + a SQL Server backup file using SQL Server Management Objects (SMO). This |
| 8 | + is useful for understanding the file structure of a backup before |
| 9 | + performing a restore operation, especially when file relocation is needed. |
| 10 | +
|
| 11 | + .PARAMETER ServerObject |
| 12 | + Specifies the current server connection object. |
| 13 | +
|
| 14 | + .PARAMETER BackupFile |
| 15 | + Specifies the full path to the backup file to read. |
| 16 | +
|
| 17 | + .PARAMETER FileNumber |
| 18 | + Specifies the backup set number to read when the backup file contains |
| 19 | + multiple backup sets. Default is 1. |
| 20 | +
|
| 21 | + .EXAMPLE |
| 22 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 23 | + $serverObject | Get-SqlDscBackupFileList -BackupFile 'C:\Backups\MyDatabase.bak' |
| 24 | +
|
| 25 | + Gets the list of files contained in the specified backup file. |
| 26 | +
|
| 27 | + .EXAMPLE |
| 28 | + $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' |
| 29 | + $files = $serverObject | Get-SqlDscBackupFileList -BackupFile 'C:\Backups\MyDatabase.bak' |
| 30 | + $relocateFiles = $files | ForEach-Object { |
| 31 | + $newPath = if ($_.Type -eq 'L') { 'L:\SQLLogs' } else { 'D:\SQLData' } |
| 32 | + [Microsoft.SqlServer.Management.Smo.RelocateFile]::new( |
| 33 | + $_.LogicalName, |
| 34 | + (Join-Path -Path $newPath -ChildPath ([System.IO.Path]::GetFileName($_.PhysicalName))) |
| 35 | + ) |
| 36 | + } |
| 37 | + $serverObject | Restore-SqlDscDatabase -Name 'MyDatabase' -BackupFile 'C:\Backups\MyDatabase.bak' -RelocateFile $relocateFiles |
| 38 | +
|
| 39 | + Gets the file list and creates RelocateFile objects for a restore |
| 40 | + operation that moves files to different directories. |
| 41 | +
|
| 42 | + .INPUTS |
| 43 | + `Microsoft.SqlServer.Management.Smo.Server` |
| 44 | +
|
| 45 | + Server object accepted from the pipeline. |
| 46 | +
|
| 47 | + .OUTPUTS |
| 48 | + `BackupFileSpec[]` |
| 49 | +
|
| 50 | + Returns an array of BackupFileSpec objects describing each file in the backup. |
| 51 | +#> |
| 52 | +function Get-SqlDscBackupFileList |
| 53 | +{ |
| 54 | + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')] |
| 55 | + [OutputType([BackupFileSpec[]])] |
| 56 | + [CmdletBinding()] |
| 57 | + param |
| 58 | + ( |
| 59 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 60 | + [Microsoft.SqlServer.Management.Smo.Server] |
| 61 | + $ServerObject, |
| 62 | + |
| 63 | + [Parameter(Mandatory = $true)] |
| 64 | + [ValidateNotNullOrEmpty()] |
| 65 | + [System.String] |
| 66 | + $BackupFile, |
| 67 | + |
| 68 | + [Parameter()] |
| 69 | + [ValidateRange(1, 2147483647)] |
| 70 | + [System.Int32] |
| 71 | + $FileNumber |
| 72 | + ) |
| 73 | + |
| 74 | + process |
| 75 | + { |
| 76 | + Write-Debug -Message ($script:localizedData.Get_SqlDscBackupFileList_Reading -f $BackupFile) |
| 77 | + |
| 78 | + try |
| 79 | + { |
| 80 | + # Create the restore object to read file list |
| 81 | + $restore = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Restore' |
| 82 | + |
| 83 | + # Create and add the backup device |
| 84 | + $backupDevice = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.BackupDeviceItem' -ArgumentList $BackupFile, 'File' |
| 85 | + $restore.Devices.Add($backupDevice) |
| 86 | + |
| 87 | + if ($PSBoundParameters.ContainsKey('FileNumber')) |
| 88 | + { |
| 89 | + $restore.FileNumber = $FileNumber |
| 90 | + } |
| 91 | + |
| 92 | + # Read the file list from the backup |
| 93 | + $fileList = $restore.ReadFileList($ServerObject) |
| 94 | + |
| 95 | + # Convert DataTable rows to BackupFileSpec objects |
| 96 | + $result = foreach ($row in $fileList.Rows) |
| 97 | + { |
| 98 | + [BackupFileSpec]::new( |
| 99 | + $row['LogicalName'], |
| 100 | + $row['PhysicalName'], |
| 101 | + $row['Type'], |
| 102 | + $row['FileGroupName'], |
| 103 | + [System.Int64] $row['Size'], |
| 104 | + [System.Int64] $row['MaxSize'] |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + return $result |
| 109 | + } |
| 110 | + catch |
| 111 | + { |
| 112 | + $errorMessage = $script:localizedData.Get_SqlDscBackupFileList_Failed -f $BackupFile |
| 113 | + |
| 114 | + $PSCmdlet.ThrowTerminatingError( |
| 115 | + [System.Management.Automation.ErrorRecord]::new( |
| 116 | + [System.InvalidOperationException]::new($errorMessage, $_.Exception), |
| 117 | + 'GSBFL0002', # cspell: disable-line |
| 118 | + [System.Management.Automation.ErrorCategory]::InvalidOperation, |
| 119 | + $BackupFile |
| 120 | + ) |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments