Mount drives and access content from inside module function

Welcome Forums General PowerShell Q&A Mount drives and access content from inside module function

This topic contains 6 replies, has 2 voices, and was last updated by

 
Participant
3 years, 7 months ago.

  • Author
    Posts
  • #28819

    Participant
    Points: 4
    Rank: Member

    Example to recreate the issue.

    When mounting an VHDX either with Mount-WindowsImage or Mount-VHD from inside a function that is part of a module. I cant access the content of that VHDX.
    But if the function is dot sourced it works fine.

    This is on V4 and V5.
    Does anyone know of a workaround?

  • #28822

    Participant
    Points: 1
    Rank: Member

    Does it throw an error? If so could you post the error. Almost sounds like a scope issue, but I can't see anything that points to that off the bat.

  • #28825

    Participant
    Points: 4
    Rank: Member

    PS H:\downloads\temp2> .\Fails.ps1

    DriveLetter FileSystemLabel FileSystem DriveType HealthStatus OperationalStatus SizeRemaining Size
    ----------- --------------- ---------- --------- ------------ ----------------- ------------- ----
    Windows Recovery NTFS Fixed Healthy OK 1000.34 MB 1023.87 MB

    DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#2&1f4adffe&0&000004#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

    PartitionNumber DriveLetter Offset Size Type
    --------------- ----------- ------ ---- ----
    1 F 65536 1023.88 MB Recovery

    Out-File : Cannot find drive. A drive with the name 'F' does not exist.
    At H:\downloads\temp2\Test.psm1:22 char:18
    + 'test 123' | Out-File $file
    + ~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (F:String) [Out-File], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.OutFileCommand

    Get-ChildItem : Cannot find drive. A drive with the name 'F' does not exist.
    At H:\downloads\temp2\Test.psm1:23 char:5
    + Get-ChildItem $file
    + ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (F:String) [Get-ChildItem], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

    PS H:\downloads\temp2> .\Always-Work.ps1

    DriveLetter FileSystemLabel FileSystem DriveType HealthStatus OperationalStatus SizeRemaining Size
    ----------- --------------- ---------- --------- ------------ ----------------- ------------- ----
    Windows Recovery NTFS Fixed Healthy OK 1000.34 MB 1023.87 MB

    DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#2&1f4adffe&0&000005#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

    PartitionNumber DriveLetter Offset Size Type
    --------------- ----------- ------ ---- ----
    1 F 65536 1023.88 MB Recovery

    Directory: F:\

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a---- 8/20/2015 11:58 AM 22 temp.txt

    PS H:\downloads\temp2> .\Fails.ps1

    DriveLetter FileSystemLabel FileSystem DriveType HealthStatus OperationalStatus SizeRemaining Size
    ----------- --------------- ---------- --------- ------------ ----------------- ------------- ----
    Windows Recovery NTFS Fixed Healthy OK 1000.34 MB 1023.87 MB

    DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#2&1f4adffe&0&000006#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

    PartitionNumber DriveLetter Offset Size Type
    --------------- ----------- ------ ---- ----
    1 F 65536 1023.88 MB Recovery

    Directory: F:\

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a---- 8/20/2015 11:59 AM 22 temp.txt

  • #28831

    Participant
    Points: 4
    Rank: Member

    I found a connect from 2013 regarding this issue.

    https://connect.microsoft.com/PowerShell/Feedback/Details/804580

  • #28833

    Participant
    Points: 1
    Rank: Member

    Yup, I was about to go there to see if I could find the problem as a bug. Seems like there is a possible workaround posted on connect you could use, if you didn't already see that.

    Workaround that was posted for the connect Bug that was filed.

    $mount = Mount-DiskImage $isoPath -PassThru
    $driveLetter = ($mount | Get-Volume).DriveLetter
    
    # Have to use New-PSDrive so other cmdlets in this session can see the new drive
    New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root "$($driveLetter):\"
    
    // ...do things...
    
    Dismount-DiskImage $mount.ImagePath
    
  • #28834

    Participant
    Points: 4
    Rank: Member

    I'm going to try that once I get home.

    That fix begs the question. If a dot sourced function mounts a drive. then dismounts it. Does the PSDrive not get removed and the Module function just reuses it.

    More testing is in order.

  • #28839

    Participant
    Points: 4
    Rank: Member

    That workaround mentioned did work. but poking around if found that after assigning a letter to the volume all I need to do was run
    $null = get-psdrive
    and the drive would be available. Not really a true fix but it's working.

The topic ‘Mount drives and access content from inside module function’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort