【转】如何在 Windows 10、Windows 8 启动时自动装载 VHD

如何在 Windows 10、Windows 8.1 和 Windows 8 启动时自动装载 VHD (PowerShell)

简介

此 PowerShell 脚本示例展示如何在 Windows 10、Windows 8.1 和 Windows 8 启动时自动装载 VHD。

应用场景

用户重启计算机时,需要重新装载所有已装载的 VHD 文件。以下脚本用于解决此问题。

脚本

步骤 1:右键单击 PowerShell,然后选择“以管理员身份运行”。

步骤 2:然后,将此脚本拖动到 PowerShell 控制台并输入如下所示的命令。

注意

如果你不希望在启动时装载 VHD 文件,则可以运行 schtasks,找到计划任务 MountVHD,然后将其删除。
如果 VHD 文件尚未执行“初始化磁盘”或“新建简单卷”操作,则无法通过 Windows 磁盘驱动器查看该 VHD。若要了解如何执行“初始化磁盘”和“新建简单卷”操作,请查看以下文档作为参考:
初始化磁盘
新建简单卷

param
(
    [Parameter(Mandatory=$true)]
    [String]$Path
)

if(test-path -Path $path)
{
    
    #Create diskpart configuration file
    $content = "select vdisk file= `"$path`"`nattach vdisk" 
    #Output the file and store the file into user profile folder
    out-file -InputObject $content -FilePath "$env:USERPROFILE\MountVHD.txt" -Encoding ascii -Force 
    #Add schedule task
    schtasks /create /tn "MountVHD" /tr "diskpart.exe /s '$env:USERPROFILE\MountVHD.txt'" /sc ONLOGON /ru SYSTEM

    write-host "Operation executed successfully. The specified VHD file will be mounted next logon."
}
Else
{
    Write-Warning "The path is invalid."
}

https://gallery.technet.microsoft.com/scriptcenter/How-to-automatically-mount-d623ce34

posted @ 2020-09-02 10:39  HANFAN  阅读(649)  评论(0编辑  收藏  举报