Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MountImage Test
I have tested on Windows 10 and Hyper-V server 2012 R2
When a drive is mounted from a function inside a moduel the drive is not avalible to the module.
but if the function is part of a script or dot sorced it works.
Run Fails.ps1 first. (Fails)
Run Always-works.ps1 (Works)
the run Fails.ps1 again (Works)
Update - 8/20 3:21 Central
Added a workaround.ps1
Update 8/23
Added second version of module
Updated workarround
By adding $null = get-psdrive right after assigning the driveletter it's made avalible in the Module context.
if (Get-Module -Name test)
{
Remove-Module -Name test
}
if (Test-Path -Path function:test-function)
{
Remove-Item -Path function:test-function
}
function test-function2
{
[CmdletBinding()]
Param
(
[string] $path
)
$disk = New-VHD -Path $path -SizeBytes 1gb -Dynamic
Mount-DiskImage -ImagePath $path
$disknumber = (Get-DiskImage -ImagePath $path | Get-Disk).Number
Initialize-Disk -Number $disknumber -PartitionStyle GPT -PassThru -Verbose |
Get-Partition |
Remove-Partition -Confirm:$false
New-Partition -DiskNumber $disknumber -GptType '{de94bba4-06d1-4d40-a16a-bfd50179d6ac}' -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel 'Windows Recovery' -Confirm:$false
$null = Get-Partition -DiskNumber $disknumber -PartitionNumber 1 |
Add-PartitionAccessPath -AssignDriveLetter
$p = Get-Partition -DiskNumber $disknumber -PartitionNumber 1
$p | Format-Table
$file = $p.DriveLetter + ':\temp.txt'
#$file
'test 123' | Out-File $file
Get-ChildItem $file
Dismount-DiskImage $path
}
if (Test-Path -Path c:\test.vhdx)
{
Remove-Item -Path c:\test.vhdx
}
test-function2 -path c:\test.vhdx
if (Get-Module -Name test)
{
Remove-Module -Name test
}
if (Test-Path -Path function:test-function)
{
Remove-Item -Path function:test-function
}
Import-Module -Name $PSScriptRoot\Test.psm1
if (Test-Path -Path c:\test.vhdx)
{
Remove-Item -Path c:\test.vhdx
}
test-function -path c:\test.vhdx
function test-function
{
[CmdletBinding()]
Param
(
[string] $path
)
$disk = New-VHD -Path $path -SizeBytes 1gb -Dynamic
Mount-DiskImage -ImagePath $path
$disknumber = (Get-DiskImage -ImagePath $path | Get-Disk).Number
Initialize-Disk -Number $disknumber -PartitionStyle GPT -PassThru -Verbose |
Get-Partition |
Remove-Partition -Confirm:$false
New-Partition -DiskNumber $disknumber -GptType '{de94bba4-06d1-4d40-a16a-bfd50179d6ac}' -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel 'Windows Recovery' -Confirm:$false
$null = Get-Partition -DiskNumber $disknumber -PartitionNumber 1 |
Add-PartitionAccessPath -AssignDriveLetter
$p = Get-Partition -DiskNumber $disknumber -PartitionNumber 1
$p | Format-Table
$file = $p.DriveLetter + ':\temp.txt'
#$file
'test 123' | Out-File $file
Get-ChildItem $file
Dismount-DiskImage $path
}
function test-function
{
[CmdletBinding()]
Param
(
[string] $path
)
$disk = New-VHD -Path $path -SizeBytes 1gb -Dynamic
Mount-DiskImage -ImagePath $path
$disknumber = (Get-DiskImage -ImagePath $path | Get-Disk).Number
Initialize-Disk -Number $disknumber -PartitionStyle GPT -PassThru -Verbose |
Get-Partition |
Remove-Partition -Confirm:$false
New-Partition -DiskNumber $disknumber -GptType '{de94bba4-06d1-4d40-a16a-bfd50179d6ac}' -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel 'Windows Recovery' -Confirm:$false
$null = Get-Partition -DiskNumber $disknumber -PartitionNumber 1 |
Add-PartitionAccessPath -AssignDriveLetter
$null = get-psdrive
$p = Get-Partition -DiskNumber $disknumber -PartitionNumber 1
$p | Format-Table
$file = $p.DriveLetter + ':\temp.txt'
#$file
'test 123' | Out-File $file
Get-ChildItem $file
Dismount-DiskImage $path
}
if (Get-Module -Name test)
{
Remove-Module -Name test
}
if (Get-Module -Name test2)
{
Remove-Module -Name test2
}
if (Test-Path -Path function:test-function)
{
Remove-Item -Path function:fix-MountFunctions
}
Import-Module -Name $PSScriptRoot\Test2.psm1
if (Test-Path -Path c:\test.vhdx)
{
Remove-Item -Path c:\test.vhdx
}
test-function -path h:\test.vhdx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment