Skip to content

When a volume is mounted after a module is loaded, that volume is not visible from the module code #2442

@jiria

Description

@jiria

Steps to reproduce

param($VHDPath = "${env:TEMP}\test.vhd")

function CreateVHD ($VHDPath, $Size)
{
  $drive = (New-VHD -path $vhdpath -SizeBytes $size -Dynamic   | `
              Mount-VHD -Passthru |  `
              get-disk -number {$_.DiskNumber} | `
              Initialize-Disk -PartitionStyle MBR -PassThru | `
              New-Partition -UseMaximumSize -AssignDriveLetter:$False -MbrType IFS | `
              Format-Volume -Confirm:$false -FileSystem NTFS -force | `
              get-partition | `
              Add-PartitionAccessPath -AssignDriveLetter -PassThru | `
              get-volume).DriveLetter

    $drive
}

$m = New-Module {
    function Test-DrivePresence
    {
        param ([String]$Path)

        Write-Output "Testing path: $Path"
        if (Test-Path $Path)
        {
            Write-Output "Exists"
        }
        else 
        {
            Write-Output "Does not exist"
        }

        Write-Output "Running Get-PSDrive"
        Get-PSDrive | Out-Host

        Write-Output "Testing path: $Path"
        if (Test-Path $Path)
        {
            Write-Output "Exists"
        }
        else 
        {
            Write-Output "Does not exist"
        }
    }

    Export-ModuleMember -Function Test-DrivePresence
}

Write-Output "Precondition: start from fresh PS session, do not have the media mounted"
try
{
    ipmo $m
    $drive = CreateVHD -VHDPath $VHDPath -Size 25mb
    Test-DrivePresence -Path "${drive}:"
}
finally
{
    Remove-Module $m
    Dismount-VHD $VHDPath
    del $VHDPath
}

Thanks goes to @lzybkr for making this repro script.

Expected behavior

  1. Load a module
  2. Mount a VHD
  3. VHD should be visible from the context of the module, without needing to run Get-PSDrive (e.g. when doing parameter validation using ValidateScript)

Actual behavior

  1. Load a module
  2. Mount a VHD
  3. The mounted volume is not visible, until Get-PSDrive is invoked. This is very non-intuitive.

Environment data

PS D:\temp\aaa.ps1> $psversiontable

Name                           Value
----                           -----
PSVersion                      5.1.14393.187
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.187
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions