|
270 | 270 | if (-not $CloneName) { |
271 | 271 | $cloneDatabase = $parentVhdFile |
272 | 272 | $CloneName = $parentVhdFile |
273 | | - $mountDirectory = "$($parentVhdFile)_$random" |
| 273 | + $mountDirectory = "$($parentVhdFile)_$($random)" |
274 | 274 | } |
275 | 275 | elseif ($CloneName) { |
276 | 276 | $cloneDatabase = $CloneName |
277 | | - $mountDirectory = "$($CloneName)_$random" |
| 277 | + $mountDirectory = "$($CloneName)_$($random)" |
278 | 278 | } |
279 | 279 |
|
280 | 280 | # Check if the database is already present |
|
319 | 319 | else { |
320 | 320 | $command = [ScriptBlock]::Create("Test-Path -Path `"$Destination\$CloneName.vhdx`"") |
321 | 321 | $result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
322 | | - if (-not $result) { |
| 322 | + if ($result) { |
323 | 323 | Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue |
324 | 324 | } |
325 | 325 | } |
|
360 | 360 | $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
361 | 361 |
|
362 | 362 | # Get the disk based on the name of the vhd |
363 | | - $commandText = "Get-Disk | Where-Object {$_.Location -eq `"$Destination\$CloneName.vhdx`"}" |
| 363 | + #$commandText = 'Get-Disk | Where-Object {$_.Location -eq' + " `"$Destination\$CloneName.vhdx`"}" |
| 364 | + $commandText = "Get-Vhd -Path `"$Destination\$CloneName.vhdx`"" |
364 | 365 | $command = [ScriptBlock]::Create($commandText) |
365 | 366 | $disk = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
366 | 367 | } |
|
391 | 392 | $null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction Ignore |
392 | 393 | } |
393 | 394 | else { |
394 | | - $command = [ScriptBlock]::Create("Get-Partition -Disk $disk") |
| 395 | + $command = [ScriptBlock]::Create("Get-Partition -DiskNumber $($disk.Number)") |
395 | 396 | $partition = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
396 | 397 |
|
397 | 398 | $command = [ScriptBlock]::Create("Add-PartitionAccessPath -DiskNumber $($disk.Number) -PartitionNumber $($partition[1].PartitionNumber) -AccessPath $accessPath -ErrorAction Ignore") |
|
409 | 410 | $databaseFiles = Get-ChildItem -Path $accessPath -Recurse | Where-Object {-not $_.PSIsContainer} |
410 | 411 | } |
411 | 412 | else { |
412 | | - $command = [ScriptBlock]::Create("Get-ChildItem -Path $accessPath -Recurse | Where-Object {-not $($_.PSIsContainer)}") |
| 413 | + $commandText = "Get-ChildItem -Path $accessPath -Recurse |" + 'Where-Object {-not $_.PSIsContainer}' |
| 414 | + $command = [ScriptBlock]::Create($commandText) |
413 | 415 | $databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential |
414 | 416 | } |
415 | 417 |
|
|
425 | 427 | # Mount the database |
426 | 428 | try { |
427 | 429 | Write-PSFMessage -Message "Mounting database from clone" -Level Verbose |
428 | | - |
429 | | - # Check if computer is local |
430 | | - if ($computer.IsLocalhost) { |
431 | | - $null = Mount-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $cloneDatabase -FileStructure $dbFileStructure |
432 | | - } |
433 | | - else { |
434 | | - $command = [ScriptBlock]::Create("Mount-DbaDatabase -SqlInstance $SqlInstance -Database $cloneDatabase -FileStructure $dbFileStructure") |
435 | | - $null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $SqlCredential |
436 | | - } |
| 430 | + $null = Mount-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $cloneDatabase -FileStructure $dbFileStructure |
437 | 431 | } |
438 | 432 | catch { |
439 | 433 | Stop-PSFFunction -Message "Couldn't mount database $cloneDatabase" -Target $SqlInstance -Continue |
|
0 commit comments