Skip to content

Commit

Permalink
Get-DbaDbDetachedFileInfo (#5463)
Browse files Browse the repository at this point in the history
* Get-DbaDetachedDatabaseInfo - > Get-DbaDbDetachedFileInfo

fixes #5455

* wth

* rename Get-DbaDbDetachedFileInfo
  • Loading branch information
potatoqualitee committed May 2, 2019
1 parent 3104c25 commit d8e5752
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
2 changes: 1 addition & 1 deletion dbatools.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
'Copy-DbaAgentProxy',
'Copy-DbaAgentAlert',
'Copy-DbaStartupProcedure',
'Get-DbaDetachedDatabaseInfo',
'Get-DbaDbDetachedFileInfo',
'Copy-DbaAgentJobCategory',
'Test-DbaPath',
'Export-DbaLogin',
Expand Down
2 changes: 1 addition & 1 deletion dbatools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ $script:xplat = @(
'Copy-DbaAgentProxy',
'Copy-DbaAgentAlert',
'Copy-DbaStartupProcedure',
'Get-DbaDetachedDatabaseInfo',
'Get-DbaDbDetachedFileInfo',
'Copy-DbaAgentJobCategory',
'Test-DbaPath',
'Export-DbaLogin',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Get-DbaDetachedDatabaseInfo {
function Get-DbaDbDetachedFileInfo {
<#
.SYNOPSIS
Get detailed information about detached SQL Server database files.
Expand All @@ -21,6 +21,11 @@ function Get-DbaDetachedDatabaseInfo {
.PARAMETER Path
Specifies the path to the MDF file to be read. This path must be readable by the SQL Server service account. Ideally, the MDF will be located on the SQL Server itself, or on a network share to which the SQL Server service account has access.
.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, Detach
Author: Chrissy LeMaire (@cl), netnerds.net
Expand All @@ -30,55 +35,58 @@ function Get-DbaDetachedDatabaseInfo {
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Get-DbaDetachedDatabaseInfo
https://dbatools.io/Get-DbaDbDetachedFileInfo
.EXAMPLE
PS C:\> Get-DbaDetachedDatabaseInfo -SqlInstance sql2016 -Path M:\Archive\mydb.mdf
PS C:\> Get-DbaDbDetachedFileInfo -SqlInstance sql2016 -Path M:\Archive\mydb.mdf
Returns information about the detached database file M:\Archive\mydb.mdf using the SQL Server instance sql2016. The M drive is relative to the SQL Server instance.
#>

[CmdletBinding(DefaultParameterSetName = "Default")]
[CmdletBinding()]
param (
[parameter(Mandatory)]
[Alias("ServerInstance", "SqlServer")]
[DbaInstanceParameter]$SqlInstance,
[parameter(Mandatory)]
[Alias("Mdf")]
[string]$Path,
[PSCredential]$SqlCredential
[PSCredential]$SqlCredential,
[parameter(Mandatory, ValueFromPipeline)]
[Alias("Mdf", "FilePath", "FullName")]
[string[]]$Path,
[switch]$EnableException
)

begin {
function Get-MdfFileInfo {
try {
$server = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
} catch {
Stop-Function -Message "Error occurred while establishing connection to $instance" -Category ConnectionError -ErrorRecord $_ -Target $SqlInstance
return
}
$servername = $server.name
$serviceaccount = $server.ServiceAccount
}
process {
if (Test-FunctionInterrupt) { return }
foreach ($filepath in $Path) {
$datafiles = New-Object System.Collections.Specialized.StringCollection
$logfiles = New-Object System.Collections.Specialized.StringCollection

$servername = $server.name
$serviceaccount = $server.ServiceAccount

$exists = Test-DbaPath -SqlInstance $server -Path $Path

if ($exists -eq $false) {
throw "$servername cannot access the file $path. Does the file exist and does the service account ($serviceaccount) have access to the path?"
if (-not (Test-DbaPath -SqlInstance $server -Path $filepath)) {
Stop-Function -Message "$servername cannot access the file $filepath. Does the file exist and does the service account ($serviceaccount) have access to the path?" -Continue
}

try {
$detachedDatabaseInfo = $server.DetachedDatabaseInfo($path)
$detachedDatabaseInfo = $server.DetachedDatabaseInfo($filepath)
$dbname = ($detachedDatabaseInfo | Where-Object { $_.Property -eq "Database name" }).Value
$exactdbversion = ($detachedDatabaseInfo | Where-Object { $_.Property -eq "Database version" }).Value
$collationid = ($detachedDatabaseInfo | Where-Object { $_.Property -eq "Collation" }).Value
} catch {
throw "$servername cannot read the file $path. Is the database detached?"
Stop-Function -Message "$servername cannot read the file $filepath. Is the database detached?" -Continue
}

switch ($exactdbversion) {
869 { $dbversion = "SQL Server 2017" }
852 { $dbversion = "SQL Server 2016" }
829 { $dbversion = "SQL Server 2016 Prerelease" }
782 { $dbversion = "SQL Server 2014" }
706 { $dbversion = "SQL Server 2012" }
684 { $dbversion = "SQL Server 2012 CTP1" }
661 { $dbversion = "SQL Server 2008 R2" }
660 { $dbversion = "SQL Server 2008 R2" }
655 { $dbversion = "SQL Server 2008 SP2+" }
Expand All @@ -99,42 +107,30 @@ function Get-DbaDetachedDatabaseInfo {
$collation = $collationid
}

if ($collation.length -eq 0) { $collation = $collationid }
if (-not $collation) { $collation = $collationid }

try {
foreach ($file in $server.EnumDetachedDatabaseFiles($path)) {
foreach ($file in $server.EnumDetachedDatabaseFiles($filepath)) {
$datafiles += $file
}

foreach ($file in $server.EnumDetachedLogFiles($path)) {
foreach ($file in $server.EnumDetachedLogFiles($filepath)) {
$logfiles += $file
}
} catch {
throw "$servername unable to enumerate database or log structure information for $path"
Stop-Function -Message "$servername unable to enumerate database or log structure information for $filepath" -Continue
}

$mdfinfo = [pscustomobject]@{
[pscustomobject]@{
ComputerName = $SqlInstance.ComputerName
InstanceName = $SqlInstance.InstanceName
SqlInstance = $SqlInstance.InputObject
Name = $dbname
Version = $dbversion
ExactVersion = $exactdbversion
Collation = $collation
DataFiles = $datafiles
LogFiles = $logfiles
}

return $mdfinfo
}
}

process {

$server = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
$mdfinfo = Get-MdfFileInfo $server $path

}

end {
$server.ConnectionContext.Disconnect()
return $mdfinfo
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance','Path','SqlCredential'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Path', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand All @@ -31,7 +31,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
}

Context "Command actually works" {
$results = Get-DbaDetachedDatabaseInfo -SqlInstance $script:Instance2 -Path $path
$results = Get-DbaDbDetachedFileInfo -SqlInstance $script:Instance2 -Path $path
it "Gets Results" {
$results | Should Not Be $null
}
Expand Down

0 comments on commit d8e5752

Please sign in to comment.