如何在 powershell 中以编程方式从 ISO 或磁盘映像读取文件?

如何在 powershell 中以编程方式从 ISO 或磁盘映像读取文件?

我想在 powershell 脚本中从 ISO 磁盘映像读取文件。因为这是一个自动化过程,磁盘只会暂时打开,所以如果可能的话,我想避免分配不必要的资源,比如驱动器号。

答案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如果您没有在一个范围内多次使用上述内容,则可能不需要调用该调用。

相关内容