You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Mount-DiskImage当前挂载的ISO盘符?多实例并行挂载疑问

解决同一ISO多次挂载后获取当前实例盘符的问题

要获取每次Mount-DiskImage挂载的新盘符,核心思路是挂载前后对比该ISO已挂载的盘符列表,取差值即为本次挂载的盘符。具体方案如下:

方法一:前后对比盘符列表(推荐)

这种方法简单可靠,适配并行运行场景:

  1. 挂载前,先收集该ISO已有的所有挂载盘符:
$isoPath = "C:\ewdk\10.0.20339.0.iso"
$existingDrives = (Get-DiskImage -ImagePath $isoPath | Get-Volume).DriveLetter
  1. 执行挂载操作:
Mount-DiskImage -ImagePath $isoPath
  1. 再次收集所有挂载盘符,通过对比得到本次新增的盘符:
$allDrives = (Get-DiskImage -ImagePath $isoPath | Get-Volume).DriveLetter
$currentMountDrive = Compare-Object -ReferenceObject $existingDrives -DifferenceObject $allDrives | Select-Object -ExpandProperty InputObject

此时$currentMountDrive就是本次脚本实例挂载的新盘符,后续操作直接使用该变量即可。

方法二:通过WMI查询挂载信息

也可以直接查询WMI的Win32_MountedImage类,过滤目标ISO路径后获取最新挂载记录:

$isoPath = "C:\ewdk\10.0.20339.0.iso"
# 挂载ISO
Mount-DiskImage -ImagePath $isoPath
# 查询该ISO的所有挂载记录,按挂载时间排序取最新的
$mountedImage = Get-CimInstance -ClassName Win32_MountedImage | Where-Object { $_.ImagePath -eq $isoPath } | Sort-Object -Property InstallDate -Descending | Select-Object -First 1
$currentMountDrive = $mountedImage.Drive.TrimEnd(':')

并行运行注意事项

  • 每个脚本实例独立执行前后对比逻辑,Windows会自动分配未被占用的盘符,不会互相干扰。
  • 操作完成后,可通过盘符精准卸载本次挂载的实例:
Dismount-DiskImage -Volume (Get-Volume -DriveLetter $currentMountDrive)

内容的提问来源于stack exchange,提问作者eli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 02:35:28