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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章