diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index 59eea7673..c07473447 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -224,9 +224,7 @@ func (d *HypervPS4Driver) PreserveLegacyExportBehaviour(srcPath string, dstPath } func (d *HypervPS4Driver) MoveCreatedVHDsToOutputDir(srcPath string, dstPath string) error { - // Not implemented yet - err := fmt.Errorf("Not implemented yet") - return err + return hyperv.MoveCreatedVHDsToOutputDir(srcPath, dstPath) } func (d *HypervPS4Driver) CompactDisks(path string) (result string, err error) { diff --git a/builder/hyperv/common/step_collate_artifacts.go b/builder/hyperv/common/step_collate_artifacts.go index 5942c73ba..d98b42b38 100644 --- a/builder/hyperv/common/step_collate_artifacts.go +++ b/builder/hyperv/common/step_collate_artifacts.go @@ -28,7 +28,10 @@ func (s *StepCollateArtifacts) Run(_ context.Context, state multistep.StateBag) packerTempDir = v.(string) } // If the user has chosen to skip a full export of the VM the only - // artifacts that they are interested in will be the VHDs + // artifacts that they are interested in will be the VHDs. The + // called function searches for all disks under the given source + // directory and moves them to a 'Virtual Hard Disks' folder under + // the destination directory err := driver.MoveCreatedVHDsToOutputDir(packerTempDir, s.OutputDir) if err != nil { err = fmt.Errorf("Error moving VHDs from build dir to output dir: %s", err) diff --git a/common/powershell/hyperv/hyperv.go b/common/powershell/hyperv/hyperv.go index e21513c6d..8b267c8c5 100644 --- a/common/powershell/hyperv/hyperv.go +++ b/common/powershell/hyperv/hyperv.go @@ -721,6 +721,54 @@ if ( $((Get-Item $srcPath).GetFileSystemInfos().Count) -eq 0 ) { return err } +func MoveCreatedVHDsToOutputDir(srcPath, dstPath string) error { + + var script = ` +param([string]$srcPath, [string]$dstPath) + +# Validate the paths returning an error if the supplied path is empty +# or if the paths don't exist +$srcPath, $dstPath | % { + if ($_) { + if (! (Test-Path $_)) { + [System.Console]::Error.WriteLine("Path $_ does not exist") + exit + } + } else { + [System.Console]::Error.WriteLine("A supplied path is empty") + exit + } +} + +# Convert to absolute paths if required +$srcPathAbs = (Get-Item($srcPath)).FullName +$dstPathAbs = (Get-Item($dstPath)).FullName + +# Get the full path to all disks under the directory or exit if none are found +$disks = Get-ChildItem -Path $srcPathAbs -Recurse -Filter *.vhd* -ErrorAction SilentlyContinue | % { $_.FullName } +if ($disks.Length -eq 0) { + [System.Console]::Error.WriteLine("No disks found under $srcPathAbs") + exit +} + +# Set up directory for VHDs in the destination directory +$vhdDstDir = Join-Path -Path $dstPathAbs -ChildPath 'Virtual Hard Disks' +if (! (Test-Path $vhdDstDir)) { + New-Item -ItemType Directory -Force -Path $vhdDstDir +} + +# Move the disks +foreach ($disk in $disks) { + Move-Item -Path $disk -Destination $vhdDstDir +} +` + + var ps powershell.PowerShellCmd + err := ps.Run(script, srcPath, dstPath) + + return err +} + func CompactDisks(path string) (result string, err error) { var script = ` param([string]$srcPath)