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
# Script block to execute (Drive letter stored in $driveletter)
# Mount the VHD(x) readonly, This is faster. Use when only reading files.
# This function mounts a VHD, runs a script block and unmounts the VHD.
# Wait 2 seconds for activity to clean up
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.
Script blocks are a bit funny; they stay bound to the caller's scope even when they're passed in to a module function as a parameter. Since you're defining $driveLetter in your module function, and the script block parameter is still bound to the caller, it can't resolve the $driveLetter variable.
This design can be a bit of a code smell; depends on what it is you're trying to do with that script block. However, you can create a copy of the script block that isn't bound to any particular scope like this, and invoke that instead:
$newScriptBlock = [scriptblock]::Create($block.ToString()) & $newScriptBlock
Thanks that worked perfectly. I figured you would know
Where can I find some good references for how scoping works?
You must be logged in to reply to this topic.