Module scoping problem

This topic contains 2 replies, has 2 voices, and was last updated by  David Jones 2 years, 1 month ago.

  • Author
  • #35216

    David Jones

    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

    # Path to VHD(x) file

    # 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.
    # Drive letter of the mounted VHD is stored in $driveLetter – can be used by script blocks
    $virtualDisk = Mount-VHD $vhd -ReadOnly -Passthru
    $virtualDisk = Mount-VHD $vhd -Passthru
    # Workarround for new drive letters in script modules
    $null = Get-PSDrive
    $driveLetter = ($virtualDisk |
    Get-Disk |
    Get-Partition |
    & $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.

  • #35223

    Dave Wyatt

    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
  • #35224

    David Jones

    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.