
我想從 ISO 磁碟映像讀取檔案作為 powershell 腳本的一部分。因為這是一個自動化過程,磁碟只會暫時打開,所以我希望盡可能避免分配不必要的資源,例如磁碟機代號。
答案1
這是一種無需分配任何磁碟機號碼即可讀取 ISO 的簡單方法(* 請參閱下文)。
$DiskImage = Mount-DiskImage -ImagePath $ISOPath -StorageType ISO -NoDriveLetter -PassThru
New-PSDrive -Name ISOFile -PSProvider FileSystem -Root (Get-Volume -DiskImage $DiskImage).UniqueId
Push-Location ISOFile:
# read files with the usual filesystem commands
Pop-Location
Remove-PSDrive ISOFile
Dismount-DiskImage -DevicePath $DiskImage.DevicePath
* 建立的驅動器New-PSDrive
不是真正的 Windows 驅動器,並且在 PowerShell 外部不可見,甚至在New-PSDrive
運行的 Powershell 函數外部不可見。Remove-PSDrive
如果您不在一個範圍內多次使用上述內容,則該呼叫甚至可能沒有必要。