New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New command - Get-DbaDbFileMapping #7578
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f4cd504
DbFileMap
potatoqualitee 3502d79
update manifest
potatoqualitee 4ab25d9
Create Get-DbaDbFileMap.Tests.ps1
potatoqualitee f34d854
remove a space. this is formatted properly, not sure whats going on
potatoqualitee c3a11ea
fix
potatoqualitee 9952dbf
sweet, this should fix it
potatoqualitee 6326027
updates
potatoqualitee f023c74
add example to restore-dbadatabase
potatoqualitee File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
function Get-DbaDbFileMapping { | ||
<# | ||
.SYNOPSIS | ||
Provides an easy way to build the FileMapping Hashtable for Restore-DbaDatabase from an existing database | ||
|
||
.DESCRIPTION | ||
Provides an easy way to build the FileMapping Hashtable for Restore-DbaDatabase from an existing database | ||
|
||
.PARAMETER SqlInstance | ||
The target SQL Server instance or instances. This can be a collection and receive pipeline input. | ||
|
||
.PARAMETER SqlCredential | ||
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). | ||
|
||
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. | ||
|
||
For MFA support, please use Connect-DbaInstance. | ||
|
||
.PARAMETER Database | ||
The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will be processed. | ||
|
||
.PARAMETER InputObject | ||
Database object piped in from Get-DbaDatabase | ||
|
||
.PARAMETER EnableException | ||
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. | ||
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. | ||
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch. | ||
|
||
.NOTES | ||
Tags: Database | ||
Author: Chrissy LeMaire (@cl), netnerds.net | Andreas Jordan (@JordanOrdix), ordix.de | ||
|
||
Website: https://dbatools.io | ||
Copyright: (c) 2021 by dbatools, licensed under MIT | ||
License: MIT https://opensource.org/licenses/MIT | ||
|
||
.LINK | ||
https://dbatools.io/Get-DbaDbFileMapping | ||
|
||
.EXAMPLE | ||
PS C:\> $filemap = Get-DbaDbFileMapping -SqlInstance sql2016 -Database test | ||
PS C:\> Get-ChildItem \\nas\db\backups\test | Restore-DbaDatabase -SqlInstance sql2019 -Database test -FileMapping $filemap.FileMapping | ||
|
||
Restores test to sql2019 using the file structure built from the existing database on sql2016 | ||
#> | ||
[CmdletBinding()] | ||
param ([parameter(ValueFromPipeline)] | ||
[DbaInstanceParameter[]]$SqlInstance, | ||
[PSCredential]$SqlCredential, | ||
[string[]]$Database, | ||
[parameter(ValueFromPipeline)] | ||
[Microsoft.SqlServer.Management.Smo.Database[]]$InputObject, | ||
[switch]$EnableException | ||
) | ||
process { | ||
if (Test-Bound -not 'SqlInstance', 'InputObject') { | ||
Write-Message -Level Warning -Message "You must specify either a SQL instance or supply an InputObject" | ||
return | ||
} | ||
|
||
if (Test-Bound -Not -ParameterName InputObject) { | ||
$InputObject += Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database | ||
} | ||
|
||
foreach ($db in $InputObject) { | ||
if ($db.IsAccessible) { | ||
Write-Message -Level Verbose -Message "Processing database: $db" | ||
$fileMap = @{ } | ||
|
||
foreach ($file in $db.FileGroups.Files) { | ||
$fileMap[$file.Name] = $file.FileName | ||
} | ||
foreach ($file in $db.LogFiles) { | ||
$fileMap[$file.Name] = $file.FileName | ||
} | ||
|
||
[PSCustomObject]@{ | ||
ComputerName = $db.ComputerName | ||
InstanceName = $db.InstanceName | ||
SqlInstance = $db.SqlInstance | ||
Database = $db.Name | ||
FileMapping = $fileMap | ||
} | ||
} else { | ||
Write-Message -Level Verbose -Message "Skipping processing of database: $db as database is not accessible" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") | ||
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan | ||
. "$PSScriptRoot\constants.ps1" | ||
|
||
Context "Validate parameters" { | ||
[array]$params = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($CommandName, 'Function')).Parameters.Keys | ||
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'InputObject', 'EnableException' | ||
|
||
It "Should only contain our specific parameters" { | ||
Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
||
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" { | ||
Context "Should return file information" { | ||
$results = Get-DbaDbFileMapping -SqlInstance $script:instance1 | ||
It "returns information about multiple databases" { | ||
$results.Database -contains "tempdb" | Should -Be $true | ||
$results.Database -contains "master" | Should -Be $true | ||
} | ||
} | ||
Context "Should return file information for a single database" { | ||
$results = Get-DbaDbFileMapping -SqlInstance $script:instance1 -Database tempdb | ||
It "returns information about tempdb" { | ||
$results.Database -contains "tempdb" | Should -Be $true | ||
$results.Database -contains "master" | Should -Be $false | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add this example to the restore command as well and then I'm good to merge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might replace the example I think the restore command has for manually building the hashtable object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add it in addition to -- I think it's important to show ppl how to build the hashtable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ty for the approval! Will merge once tests finish.