Cra*_*aig 5 powershell mount vhd .vhdx
好吧,说实话我已经知道问题的答案了。但是,我在网上搜索了相当长一段时间来了解如何做到这一点,但结果却不尽如人意。在拼凑出一些半可用答案的提示后,我找到了一些解决方案,我想与可能有同样疑问的其他人分享。
解决方案#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\"
Run Code Online (Sandbox Code Playgroud)
在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
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您想要使用磁盘管理器或文件资源管理器中所示的分区卷标,则可以按如下所示替换 $PartitionMountPath 变量代码。只需提前确保卷实际上有一个名称,并且它们对于驱动器来说是唯一的。
$PartitionMountPath = $DriveNumberPath + "\" + ($Partition | Get-Volume).FileSystemLabel
Run Code Online (Sandbox Code Playgroud)
您还可以使用 foreach 循环包装解决方案 #2 代码的一部分,并使用 Get-ChildItem 命令从文件夹位置提取多个 VHDX 文件。不过,将其作为第三种解决方案似乎有点过分了。
无论如何...这就是我找到的解决方案。如果您有更好/更干净的方法...请告诉我!
对于任何想知道为什么我一开始就想要这个的人来说……这样我就可以将其放入任务计划程序作业中以在启动时自动挂载 VHDX 文件。解决方案#1 是我真正需要的,#2 只是一个额外的好处。;)
| 归档时间: |
|
| 查看次数: |
3423 次 |
| 最近记录: |