Skip to content

Commit 506bac7

Browse files
committed
Added remote support. Fixed minor errors
1 parent 921a23a commit 506bac7

1 file changed

Lines changed: 59 additions & 19 deletions

File tree

functions/New-PSDCClone.ps1

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@
153153
# Setup the computer object
154154
$computer = [PsfComputer]$server.Name
155155

156+
if (-not $computer.IsLocalhost) {
157+
$command = [scriptblock]::Create("Import-Module PSDatabaseClone")
158+
159+
try {
160+
Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
161+
}
162+
catch {
163+
Stop-PSFFunction -Message "Couldn't import module remotely" -Target $command
164+
return
165+
}
166+
}
167+
156168
# Check destination
157169
if (-not $Destination) {
158170
$Destination = "$($server.DefaultFile)\clone"
@@ -168,7 +180,7 @@
168180
$Destination = Convert-PSDCLocalUncPathToLocalPath -UncPath $Destination -Credential $Credential
169181
}
170182
else {
171-
$command = [ScriptBlock]::Create("Convert-PSDCLocalUncPathToLocalPath -UncPath '$Destination'")
183+
$command = [ScriptBlock]::Create("Convert-PSDCLocalUncPathToLocalPath -UncPath `"$Destination`"")
172184
$Destination = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
173185
}
174186
}
@@ -184,9 +196,20 @@
184196
}
185197

186198
# Test if the destination can be reached
187-
if (-not (Test-Path -Path $Destination -Credential $Credential)) {
188-
Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance
199+
# Check if computer is local
200+
if ($computer.IsLocalhost) {
201+
if (-not (Test-Path -Path $Destination -Credential $Credential)) {
202+
Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance
203+
}
204+
}
205+
else {
206+
$command = [ScriptBlock]::Create("Test-Path -Path $Destination")
207+
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
208+
if (-not $result) {
209+
Stop-PSFFunction -Message "Could not find destination path $Destination" -Target $SqlInstance
210+
}
189211
}
212+
190213
}
191214

192215
# Loopt through all the databases
@@ -251,26 +274,42 @@
251274
$accessPath = "$Destination\$mountDirectory"
252275

253276
# Check if access path is already present
254-
if (-not (Test-Path -Path $accessPath -Credential $Credential)) {
255-
try {
256-
# Check if computer is local
257-
if ($computer.IsLocalhost) {
277+
if ($computer.IsLocalhost) {
278+
if (-not (Test-Path -Path $accessPath -Credential $Credential)) {
279+
try {
258280
$null = New-Item -Path $accessPath -ItemType Directory -Credential $Credential -Force
259281
}
260-
else {
261-
$command = [ScriptBlock]::Create("New-Item -Path $accessPath -ItemType Directory -Credential $Credential -Force")
262-
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
282+
catch {
283+
Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue
263284
}
264-
265285
}
266-
catch {
267-
Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue
286+
}
287+
else {
288+
$command = [ScriptBlock]::Create("Test-Path -Path $accessPath")
289+
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
290+
if (-not $result) {
291+
try {
292+
$command = [ScriptBlock]::Create("New-Item -Path $accessPath -ItemType Directory -Force")
293+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
294+
}
295+
catch {
296+
Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue
297+
}
268298
}
269299
}
270300

271301
# Check if the clone vhd does not yet exist
272-
if (Test-Path -Path "$Destination\$CloneName.vhdx" -Credential $DestinationCredential) {
273-
Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue
302+
if ($computer.IsLocalhost) {
303+
if (Test-Path -Path "$Destination\$CloneName.vhdx" -Credential $DestinationCredential) {
304+
Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue
305+
}
306+
}
307+
else {
308+
$command = [ScriptBlock]::Create("Test-Path -Path `"$Destination\$CloneName.vhdx`"")
309+
$result = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
310+
if (-not $result) {
311+
Stop-PSFFunction -Message "Clone $CloneName already exists" -Target $accessPath -Continue
312+
}
274313
}
275314

276315
# Create the new child vhd
@@ -282,7 +321,7 @@
282321
$vhd = New-VHD -ParentPath $ParentVhd -Path "$Destination\$CloneName.vhdx" -Differencing
283322
}
284323
else {
285-
$command = [ScriptBlock]::Create("New-VHD -ParentPath $ParentVhd -Path '$Destination\$CloneName.vhdx' -Differencing")
324+
$command = [ScriptBlock]::Create("New-VHD -ParentPath $ParentVhd -Path `"$Destination\$CloneName.vhdx`" -Differencing")
286325
$vhd = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
287326
}
288327

@@ -309,7 +348,8 @@
309348
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
310349

311350
# Get the disk based on the name of the vhd
312-
$command = [ScriptBlock]::Create("Get-Disk | Where-Object {$_.Location -eq `"$Destination\$CloneName.vhdx`"}")
351+
$commandText = "Get-Disk | Where-Object {$_.Location -eq `"$Destination\$CloneName.vhdx`"}"
352+
$command = [ScriptBlock]::Create($commandText)
313353
$disk = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
314354
}
315355
}
@@ -379,8 +419,8 @@
379419
$null = Mount-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $cloneDatabase -FileStructure $dbFileStructure
380420
}
381421
else {
382-
$command = [ScriptBlock]::Create("Mount-DbaDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $cloneDatabase -FileStructure $dbFileStructure")
383-
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
422+
$command = [ScriptBlock]::Create("Mount-DbaDatabase -SqlInstance $SqlInstance -Database $cloneDatabase -FileStructure $dbFileStructure")
423+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $SqlCredential
384424
}
385425
}
386426
catch {

0 commit comments

Comments
 (0)