Skip to content

Commit 8029937

Browse files
committed
Added check for config run. Added config for diskpart script file. Replaced hyper-v dependency
1 parent 082c4cb commit 8029937

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

functions/clone/New-PSDCClone.ps1

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@
110110
)
111111

112112
begin {
113+
# Check if the setup has ran
114+
if (-not (Get-PSFConfigValue -FullName psdatabaseclone.setup.status)) {
115+
Stop-PSFFunction -Message "The module setup has NOT yet successfully run. Please run 'Set-PSDCConfiguration'"
116+
return
117+
}
118+
113119
# Get the information store
114120
$informationStore = Get-PSFConfigValue -FullName psdatabaseclone.informationstore.mode
115121

@@ -145,6 +151,9 @@
145151
if ($Disabled) {
146152
$active = 0
147153
}
154+
155+
# Set the location where to save the diskpart command
156+
$diskpartScriptFile = Get-PSFConfigValue -FullName psdatabaseclone.diskpart.scriptfile -Fallback "$env:APPDATA\psdatabaseclone\diskpartcommand.txt"
148157
}
149158

150159
process {
@@ -351,9 +360,15 @@
351360
try {
352361
Write-PSFMessage -Message "Creating clone from $ParentVhd" -Level Verbose
353362

363+
$command = "create vdisk file='$Destination\$CloneName.vhdx' parent='$ParentVhd'"
364+
354365
# Check if computer is local
355366
if ($computer.IsLocalhost) {
356-
$vhd = New-VHD -ParentPath $ParentVhd -Path "$Destination\$CloneName.vhdx" -Differencing
367+
# Set the content of the diskpart script file
368+
Set-Content -Path $diskpartScriptFile -Value $command -Force
369+
370+
$script = [ScriptBlock]::Create("diskpart /s $diskpartScriptFile")
371+
$null = Invoke-PSFCommand -ScriptBlock $script
357372
}
358373
else {
359374
$command = [ScriptBlock]::Create("New-VHD -ParentPath $ParentVhd -Path `"$Destination\$CloneName.vhdx`" -Differencing")
@@ -366,7 +381,7 @@
366381

367382
}
368383
catch {
369-
Stop-PSFFunction -Message "Could not create clone" -Target $vhd -Continue
384+
Stop-PSFFunction -Message "Could not create clone" -Target $vhd -Continue -ErrorRecord $_
370385
}
371386
}
372387

@@ -378,14 +393,14 @@
378393
# Check if computer is local
379394
if ($computer.IsLocalhost) {
380395
# Mount the disk
381-
$null = Mount-VHD -Path "$Destination\$CloneName.vhdx" -NoDriveLetter
396+
$null = Mount-DiskImage -ImagePath "$Destination\$CloneName.vhdx"
382397

383398
# Get the disk based on the name of the vhd
384399
$disk = Get-Disk | Where-Object {$_.Location -eq "$Destination\$CloneName.vhdx"}
385400
}
386401
else {
387402
# Mount the disk
388-
$command = [ScriptBlock]::Create("Mount-VHD -Path `"$Destination\$CloneName.vhdx`" -NoDriveLetter")
403+
$command = [ScriptBlock]::Create("Mount-DiskImage -ImagePath `"$Destination\$CloneName.vhdx`"")
389404
$null = Invoke-PSFCommand -ComputerName $computer -ScriptBlock $command -Credential $Credential
390405

391406
# Get the disk based on the name of the vhd

0 commit comments

Comments
 (0)