【转】如何在 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值