Skip to content

Commit 49a0871

Browse files
committed
Full support for remote execution added. FIxes #1
1 parent 394b121 commit 49a0871

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

functions/New-PSDCClone.ps1

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,11 @@
270270
if (-not $CloneName) {
271271
$cloneDatabase = $parentVhdFile
272272
$CloneName = $parentVhdFile
273-
$mountDirectory = "$($parentVhdFile)_$random"
273+
$mountDirectory = "$($parentVhdFile)_$($random)"
274274
}
275275
elseif ($CloneName) {
276276
$cloneDatabase = $CloneName
277-
$mountDirectory = "$($CloneName)_$random"
277+
$mountDirectory = "$($CloneName)_$($random)"
278278
}
279279

280280
# Check if the database is already present
@@ -319,7 +319,7 @@
319319
else {
320320
$command = [ScriptBlock]::Create("Test-Path -Path `"$Destination\$CloneName.vhdx`"")
321321
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
322-
if (-not $result) {
322+
if ($result) {
323323
Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue
324324
}
325325
}
@@ -360,7 +360,8 @@
360360
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
361361

362362
# 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`""
364365
$command = [ScriptBlock]::Create($commandText)
365366
$disk = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
366367
}
@@ -391,7 +392,7 @@
391392
$null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction Ignore
392393
}
393394
else {
394-
$command = [ScriptBlock]::Create("Get-Partition -Disk $disk")
395+
$command = [ScriptBlock]::Create("Get-Partition -DiskNumber $($disk.Number)")
395396
$partition = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
396397

397398
$command = [ScriptBlock]::Create("Add-PartitionAccessPath -DiskNumber $($disk.Number) -PartitionNumber $($partition[1].PartitionNumber) -AccessPath $accessPath -ErrorAction Ignore")
@@ -409,7 +410,8 @@
409410
$databaseFiles = Get-ChildItem -Path $accessPath -Recurse | Where-Object {-not $_.PSIsContainer}
410411
}
411412
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)
413415
$databaseFiles = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
414416
}
415417

@@ -425,15 +427,7 @@
425427
# Mount the database
426428
try {
427429
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
437431
}
438432
catch {
439433
Stop-PSFFunction -Message "Couldn't mount database $cloneDatabase" -Target $SqlInstance -Continue

0 commit comments

Comments
 (0)