Skip to content

Commit 21d2212

Browse files
committed
Added remote support
1 parent 8bc4239 commit 21d2212

1 file changed

Lines changed: 81 additions & 24 deletions

File tree

functions/New-PSDCImage.ps1

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function New-PSDCImage {
2-
<#
2+
<#
33
.SYNOPSIS
44
New-PSDCImage creates a new image
55
@@ -165,6 +165,7 @@
165165
# Get the local path from the network path
166166
if (-not $ImageLocalPath) {
167167
try {
168+
# Check if computer is local
168169
if ($computer.IsLocalhost) {
169170
$ImageLocalPath = Convert-PSDCLocalUncPathToLocalPath -UncPath $ImageNetworkPath
170171
}
@@ -265,7 +266,16 @@
265266
# try to create the new VHD
266267
try {
267268
Write-PSFMessage -Message "Create the vhd $imageName.vhdx" -Level Verbose
268-
$null = New-PSDCVhdDisk -Destination $imagePath -FileName "$imageName.vhdx"
269+
270+
# Check if computer is local
271+
if ($computer.IsLocalhost) {
272+
$null = New-PSDCVhdDisk -Destination $imagePath -FileName "$imageName.vhdx"
273+
}
274+
else {
275+
$command = [ScriptBlock]::Create("$null = New-PSDCVhdDisk -Destination $imagePath -FileName '$imageName.vhdx'")
276+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
277+
}
278+
269279
}
270280
catch {
271281
Stop-PSFFunction -Message "Couldn't create vhd $imageName" -Target "$imageName.vhd" -ErrorRecord $_ -Continue
@@ -275,7 +285,14 @@
275285
try {
276286
Write-PSFMessage -Message "Initializing the vhd $imageName.vhd" -Level Verbose
277287

278-
$diskResult = Initialize-PSDCVhdDisk -Path $vhdPath -Credential $DestinationCredential
288+
# Check if computer is local
289+
if ($computer.IsLocalhost) {
290+
$diskResult = Initialize-PSDCVhdDisk -Path $vhdPath -Credential $DestinationCredential
291+
}
292+
else {
293+
$command = [ScriptBlock]::Create("Initialize-PSDCVhdDisk -Path $vhdPath -Credential $DestinationCredential")
294+
$diskResult = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
295+
}
279296
}
280297
catch {
281298
Stop-PSFFunction -Message "Couldn't initialize vhd $vhdPath" -Target $imageName -ErrorRecord $_ -Continue
@@ -287,7 +304,14 @@
287304
# Check if access path is already present
288305
if (-not (Test-Path -Path $accessPath)) {
289306
try {
290-
$null = New-Item -Path $accessPath -ItemType Directory -Force:$Force
307+
# Check if computer is local
308+
if ($computer.IsLocalhost) {
309+
$null = New-Item -Path $accessPath -ItemType Directory -Force:$Force
310+
}
311+
else {
312+
$command = [ScriptBlock]::Create("New-Item -Path $accessPath -ItemType Directory -Force:$Force")
313+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
314+
}
291315
}
292316
catch {
293317
Stop-PSFFunction -Message "Couldn't create access path directory" -ErrorRecord $_ -Target $accessPath -Continue
@@ -299,7 +323,14 @@
299323
$partition = $diskResult.Partition
300324

301325
# Add the access path to the mounted disk
302-
$null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction SilentlyContinue
326+
if ($computer.IsLocalhost) {
327+
$null = Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction SilentlyContinue
328+
}
329+
else {
330+
$command = [ScriptBlock]::Create("Add-PartitionAccessPath -DiskNumber $disk.Number -PartitionNumber $partition[1].PartitionNumber -AccessPath $accessPath -ErrorAction SilentlyContinue")
331+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
332+
}
333+
303334
}
304335
catch {
305336
Stop-PSFFunction -Message "Couldn't create access path for partition" -ErrorRecord $_ -Target $diskResult.partition
@@ -309,21 +340,39 @@
309340
$imageDataFolder = "$($imagePath)\$imageName\Data"
310341
$imageLogFolder = "$($imagePath)\$imageName\Log"
311342

312-
# Check if image folder structure exist
343+
# Check if image data folder exist
313344
if (-not (Test-Path -Path $imageDataFolder)) {
314345
try {
315346
Write-PSFMessage -Message "Creating data folder for image" -Level Verbose
316-
$null = New-Item -Path $imageDataFolder -ItemType Directory
347+
348+
# Check if computer is local
349+
if ($computer.IsLocalhost) {
350+
$null = New-Item -Path $imageDataFolder -ItemType Directory
351+
}
352+
else {
353+
$command = [ScriptBlock]::Create("New-Item -Path $imageDataFolder -ItemType Directory")
354+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
355+
}
317356
}
318357
catch {
319358
Stop-PSFFunction -Message "Couldn't create image data folder" -Target $imageName -ErrorRecord $_ -Continue
320359
}
321360
}
322361

362+
# Test if the image log folder exists
323363
if (-not (Test-Path -Path $imageLogFolder)) {
324364
try {
325365
Write-PSFMessage -Message "Creating transaction log folder for image" -Level Verbose
326-
$null = New-Item -Path $imageLogFolder -ItemType Directory
366+
367+
# Check if computer is local
368+
if ($computer.IsLocalhost) {
369+
$null = New-Item -Path $imageLogFolder -ItemType Directory
370+
}
371+
else {
372+
$command = [ScriptBlock]::Create("New-Item -Path $imageLogFolder -ItemType Directory")
373+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
374+
}
375+
327376
}
328377
catch {
329378
Stop-PSFFunction -Message "Couldn't create image data folder" -Target $imageName -ErrorRecord $_ -Continue
@@ -336,7 +385,6 @@
336385
# Restore database to image folder
337386
try {
338387
Write-PSFMessage -Message "Restoring database $db on $DestinationSqlInstance" -Level Verbose
339-
340388
$restore = Restore-DbaDatabase -SqlInstance $DestinationSqlInstance -SqlCredential $DestinationSqlCredential `
341389
-DatabaseName $tempDbName -Path $lastFullBackup `
342390
-DestinationDataDirectory $imageDataFolder `
@@ -349,7 +397,6 @@
349397
# Detach database
350398
try {
351399
Write-PSFMessage -Message "Detaching database $tempDbName on $DestinationSqlInstance" -Level Verbose
352-
353400
$null = Dismount-DbaDatabase -SqlInstance $DestinationSqlInstance -Database $tempDbName -SqlCredential $DestinationSqlCredential
354401
}
355402
catch {
@@ -360,11 +407,21 @@
360407
try {
361408
Write-PSFMessage -Message "Dismounting vhd" -Level Verbose
362409

363-
# Dismount the VHD
364-
$null = Dismount-VHD -Path $vhdPath
410+
# Check if computer is local
411+
if ($computer.IsLocalhost) {
412+
# Dismount the VHD
413+
$null = Dismount-VHD -Path $vhdPath
365414

366-
# Remove the access path
367-
$null = Remove-Item -Path $accessPath -Force
415+
# Remove the access path
416+
$null = Remove-Item -Path $accessPath -Force
417+
}
418+
else {
419+
$command = [ScriptBlock]::Create("Dismount-VHD -Path $vhdPath")
420+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
421+
422+
$command = [ScriptBlock]::Create("Remove-Item -Path $accessPath -Force")
423+
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $DestinationCredential
424+
}
368425
}
369426
catch {
370427
Stop-PSFFunction -Message "Couldn't dismount vhd" -Target $imageName -ErrorRecord $_ -Continue
@@ -377,16 +434,16 @@
377434
$databaseTS = $lastFullBackup.Start
378435

379436
$query = "
380-
DECLARE @ImageID INT;
381-
EXECUTE dbo.Image_New @ImageID = @ImageID OUTPUT, -- int
382-
@ImageName = '$imageName', -- varchar(100)
383-
@ImageLocation = '$imageLocation', -- varchar(255)
384-
@SizeMB = $sizeMB, -- int
385-
@DatabaseName = '$databaseName', -- varchar(100)
386-
@DatabaseTimestamp = '$databaseTS' -- datetime
387-
388-
SELECT @ImageID as ImageID
389-
"
437+
DECLARE @ImageID INT;
438+
EXECUTE dbo.Image_New @ImageID = @ImageID OUTPUT, -- int
439+
@ImageName = '$imageName', -- varchar(100)
440+
@ImageLocation = '$imageLocation', -- varchar(255)
441+
@SizeMB = $sizeMB, -- int
442+
@DatabaseName = '$databaseName', -- varchar(100)
443+
@DatabaseTimestamp = '$databaseTS' -- datetime
444+
445+
SELECT @ImageID as ImageID
446+
"
390447

391448
Write-PSFMessage -Message "Query New Image`n$query" -Level Debug
392449

0 commit comments

Comments
 (0)