This function works when used as a helper function inside a module or when dot sourced into the current session. But does not work when exported from a module
`
function Mount-VhdAndRunBlock
{
param
(
# Path to VHD(x) file
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]
$vhd,
# Script block to execute (Drive letter stored in $driveletter)
[Parameter(Mandatory=$true)]
[scriptblock]
$block,
# Mount the VHD(x) readonly, This is faster. Use when only reading files.
[switch]
$ReadOnly
)
# This function mounts a VHD, runs a script block and unmounts the VHD.
# Drive letter of the mounted VHD is stored in $driveLetter - can be used by script blocks
if($ReadOnly)
{
$virtualDisk = Mount-VHD $vhd -ReadOnly -Passthru
}
else
{
$virtualDisk = Mount-VHD $vhd -Passthru
}
# Workarround for new drive letters in script modules
$null = Get-PSDrive
$driveLetter = ($virtualDisk |
Get-Disk |
Get-Partition |
Get-Volume).DriveLetter
& $block
Dismount-VHD $vhd
# Wait 2 seconds for activity to clean up
Start-Sleep -Seconds 2
}
`
`
Mount-VhdAndRunBlock -vhd $ImagePath -block { $driveLetter } -ReadOnly
`
This should return the drive letter of the vhd’s mount point. but I get nothing.
I think this is a scoping issue but I’m lost on how to fix.