# # Mount a virtual hard disk to a folder # function Mount-VHDToFolder { [CmdLetBinding()] param ( [Parameter(Mandatory=$true)][String]$Image, [Parameter(Mandatory=$true)][String]$MountFolder ) process { try { # Gets a mounted disk image $mountedDisk = Mount-DiskImage -ImagePath $Image -NoDriveLetter -PassThru -ErrorAction Stop | Get-DiskImage } catch { Write-Error "Failed to mount disk" return } # Get all of the partitions $partitions = Get-Partition -DiskNumber $mountedDisk.Number foreach($partition in $partitions) { $partFolder = Join-Path $MountFolder $partition.PartitionNumber # Clean up this folder if it exists if(Test-Path $partFolder) { rmdir -Force $partFolder } # Make the new folder mkdir $partFolder | Out-Null try { # Add the access path for the disk Add-PartitionAccessPath -InputObject $partition -AccessPath $partFolder -ErrorAction Stop } catch { Write-Warning "Could not add access path '$($partFolder)' for partition '$($partition.PartitionNumber)'" } } } }