Skip to content

Instantly share code, notes, and snippets.

@altrive
Last active December 14, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altrive/5035914 to your computer and use it in GitHub Desktop.
Save altrive/5035914 to your computer and use it in GitHub Desktop.
特定条件下でPowerShellでマウントしたISOイメージにアクセスできなくなる問題。
#Can't Access mounted ISO Image PSDrive
<#
概要:
Dismount-DiskImage コマンドはマウントされていないISOイメージに対して呼出しても特にエラーは発生しない。
ただ、特定の条件が重なった場合に、マウントしたPSドライブにアクセスできない問題が発生することを確認。
問題が発生した場合はPowerShellのRunSpaceの再起動が必要となる?
再現条件:
1. Dismount-DiskImage を 2回以上呼出
2. Test-Path をアンマウントされた存在しないディレクトリ対して呼出
3. Mount-DiskImage で再マウント後、PSDrive経由でディスクにアクセスできない。OS/.NET経由は問題なし
結論:
finallyの中でマウント状態を確認せずにDismount-DiskImageを呼び出すのはやめておいたほうがよさそう。
#>
$ErrorActionPreference= "Stop"
$IsoPath = "D:\ISO\Test.iso"
try{
#Mount ISO
Mount-DiskImage -ImagePath $IsoPath
$driveLetter =(Get-DiskImage -ImagePath $IsoPath | Get-Volume).DriveLetter
$driveRootPath = "{0}:\" -f $driveLetter
#Access Drive
Test-Path $driveRootPath #true
Get-PSDrive $driveLetter #PSDrive Info
#Dismount Drive twice
Dismount-DiskImage -ImagePath $IsoPath
Dismount-DiskImage -ImagePath $IsoPath
#Access Dismounted Path using Test-Path
Test-Path $driveRootPath -ErrorAction SilentlyContinue
#ReMount ISO
Mount-DiskImage -ImagePath $IsoPath
$driveLetter =(Get-DiskImage -ImagePath $IsoPath | Get-Volume).DriveLetter
$driveRootPath = "{0}:\" -f $driveLetter
#Access Drive
Test-Path $driveRootPath # false
Get-PSDrive $driveLetter # DriveNotFoundException
#Dismount Drive
Dismount-DiskImage -ImagePath $IsoPath
}
finally{
#Cleanup
if((Get-DiskImage -ImagePath $IsoPath).Attached)
{
Write-Host "Finally Block Called"
Dismount-DiskImage -ImagePath $IsoPath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment