【问题标题】:How do you mount a VHDX file to a folder path rather than drive letter using PowerShell?如何使用 PowerShell 将 VHDX 文件挂载到文件夹路径而不是驱动器号?
【发布时间】:2021-04-07 04:36:55
【问题描述】:

好的,说实话我已经知道问题的答案了。但是,我在网上搜索了很长一段时间,以了解如何做到这一点,但结果却很短。在拼凑了几个半有用的答案提示后,我找到了几个解决方案,我想分享给可能有同样问题的其他人。

【问题讨论】:

    标签: powershell mount vhd .vhdx


    【解决方案1】:

    解决方案 #1 - PowerShell One-Liner... 假设 VHDX 只有一个卷,并且预先存在所需的装载位置。

    Mount-VHD -Path "C:\Temp\Test.vhdx" -NoDriveLetter -Passthru | Get-Disk | Get-Partition | where { ($_ | Get-Volume) -ne $Null } | Add-PartitionAccessPath -AccessPath "C:\Temp\MountPoint\"
    

    Get-Partition 之后过滤的原因是我发现,虽然我在创建 VHDX 文件时只在磁盘管理器中看到了一个分区,但实际上磁盘管理器中没有显示一个保留分区。过滤器会删除挂载该分区的尝试。

    解决方案 #2 - 假设您的 VHDX 可能(或可能没有)多个分区,并且您只想通过驱动器号和分区号轻松识别每个卷。

    $MountPath = "C:\Temp\VHD\" # This folder must pre-exist
    $VhdFile = "C:\Temp\Test.vhdx"
    $MountedVhd = Mount-DiskImage -ImagePath $VhdFile -NoDriveLetter -PassThru | Get-Disk
    $Partitions = $MountedVhd | Get-Partition
    
    if (($Partitions | Get-Volume) -ne $Null) {
        $DriveNumberPath = $MountPath + "D" + $MountedVhd.Number
        New-Item $DriveNumberPath -ItemType "directory"
        foreach ($Partition in ($Partitions | where {($_ | Get-Volume) -ne $Null})) {
            $PartitionMountPath = $DriveNumberPath + "\P" + $Partition.PartitionNumber
            New-Item $PartitionMountPath -ItemType "directory"
            $Partition | Add-PartitionAccessPath -AccessPath $PartitionMountPath
        }
    }
    

    或者,如果您想使用磁盘管理器或文件资源管理器中显示的分区卷标,您可以替换 $PartitionMountPath 变量代码,如下所示。只需提前确保这些卷实际上有一个名称,并且它们对于驱动器是唯一的。

    $PartitionMountPath = $DriveNumberPath + "\" + ($Partition | Get-Volume).FileSystemLabel
    

    您还可以使用 foreach 循环包装解决方案 #2 代码的一部分,并使用 Get-ChildItem 命令从文件夹位置拉入多个 VHDX 文件。不过,将其作为第三种解决方案提出似乎有点过头了。

    无论如何...这就是我找到的解决方案。如果您有更好/更清洁的方法...请告诉我!

    对于任何想知道为什么我什至首先想要这个的人......这样我就可以将它放入任务计划程序作业中,以便在启动时自动挂载 VHDX 文件。解决方案#1 是我真正需要的,#2 只是一个奖励。 ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2019-04-10
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      相关资源
      最近更新 更多