Skip to content

Commit

Permalink
support -Database properly fixes #5015
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee committed Jan 27, 2019
1 parent 0f96b9d commit de7e3be
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions functions/Invoke-DbaDbClone.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,17 @@ function Invoke-DbaDbClone {
if (Test-FunctionInterrupt) { return }

if ($SqlInstance) {
$InputObject += Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential
$InputObject += Get-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database
}

foreach ($db in $InputObject) {
$server = $db.Parent

$instance = $server.Name

if (-not (Test-Bound -ParameterName CloneDatabase)) {
$CloneDatabase = "$($db.Name)_clone"
}

if ($server.VersionMajor -eq 11 -and $server.Version -lt $sql2012min) {
Stop-Function -Message "Unsupported version for $instance. SQL Server 2012 SP4 and above required." -Target $server -Continue
}
Expand Down Expand Up @@ -203,7 +208,7 @@ function Invoke-DbaDbClone {
}

$dbName = $db.Name

foreach ($clonedb in $CloneDatabase) {
Write-Message -Level Verbose -Message "Cloning $clonedb from $db"
if ($server.Databases[$clonedb]) {
Expand All @@ -214,6 +219,7 @@ function Invoke-DbaDbClone {
$sql = "DBCC CLONEDATABASE('$dbName','$clonedb') $sqlWith"
Write-Message -Level Debug -Message "Sql Statement: $sql"
$null = $db.Query($sql)
$server.Databases.Refresh()
Get-DbaDatabase -SqlInstance $server -Database $clonedb
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
Expand Down

0 comments on commit de7e3be

Please sign in to comment.