2013年3月18日 星期一

利用 Winodws 8 企業版中的 PowerShell 製作 Winodws To Go 開機隨身碟

在前面的文章中已經有介紹過【利用 Windows 8 企業版中的 【建立 Windows To Go 工作區】製作開機隨身碟】,當然,我們也是一樣可以使用指令的模式來手動製作 Windows To Go 開機隨身碟。

接下來筆者就帶著各位一步一步的執行,利用 Windows 8 內建的 PowerShell 指令來達到製作 Windows To Go 的需求。

在製作開機隨身碟的同時,請您確認及準備以下幾項

1、請確認您本身的作業系統是 Windows 8 企業版。
2、準備相容於 Windows 8 的 USB 3.0 / 32G 以上的隨身碟。(需有通過 Windows To Go 認證或能相容於 Windows To Go )
3、準備 Windows 8 企業版原版光碟片或 ISO 映像檔。( ISO 映像檔可使用 Windows 8 內建的掛接功能)。

首先,我們先放入 Windows 8 企業版原版光碟片或先把 ISO 映像檔掛接起來。在範例中掛接後的 ISO 映像檔是在 D 磁碟機。

當 ISO 映像檔掛接好後,我們在呼叫出【PoserShell】,請按組合鍵【WIN】+【Q】,於搜尋列鍵入【powershell】,搜尋結果出來後,我們按方向鍵,選擇【Windows PowerShell】後並按下組合鍵【Ctrl】+【Shift】+【Enter】。


此時會出現【使用者帳戶控制】對話框,請按【是(Y)】。


這時候我們就可以看見 PowerShell 已經以系統管理員身分開啟了。
註:我們必須使用系統管理員身分來開啟 PowerShell,以避免因權限不足而中斷。


請插入 USB 隨身碟,在 PowerShell 命令提示列中鍵入【Get-Disk】並按【Enter】。這時候我們可以看到 USB 隨身碟的裝置代號是【1】。


接下來我們在鍵入【$Disk = Get-Disk 1】,並按【Enter】。此動作是告訴系統,接下來的動作將會針對此裝置執行。


在來我們在鍵入【Clear-Disk -InputObject $Disk -RemoveData】,並按【Enter】,此時系統會出現是否要執行此動作,請按【Y】並按【Enter】。此動作是清除隨身碟內所有的資料,在執行此動作之前,請確定隨身碟是可用的,也沒有任何的資料在裡面。


清除後,我們在鍵入【Initialize-Disk -InputObject $Disk -PartitionStyle MBR】,並按【Enter】。此動作是在初始化隨身碟。


接下來我們在鍵入【$SystemPartition = New-Partition -InputObject $Disk -Size (350MB) –IsActive】,並按【Enter】。此動作是將隨身碟切割成第一個磁碟區,空間大小為 350MB ,並且將它設定為【啟動】的磁區。


接著請在鍵入【Format-Volume -NewFileSystemLabel "UFD-System" -FileSystem FAT32 -Partition $SystemPartition】,並按【Enter】,此時系統會出現是否要執行此動作,請按【Y】並按【Enter】。此動作是將這一個磁碟區格式化為 FAT32 的檔案格式,並且設定磁碟名稱為【UFD-System】。


在來,我們在鍵入【$OSPartition = New-Partition -InputObject $Disk –UseMaximumSize】,並按【Enter】。此動作是將剩下來的空間,全部分割為第二個磁碟區。


接著我們在鍵入【Format-Volume -NewFileSystemLabel "UFD-Windows" -FileSystem NTFS -Partition $OSPartition】,並按【Enter】,此時系統會出現是否要執行此動作,請按【Y】並按【Enter】。此動作是將這一個磁碟區格式化為 NTFS 的檔案格式,並且設定磁碟名稱為【UFD-Windows】。


接下來,我們就在鍵入二段指令,分別將第一個磁碟區 (UFD-System) 的磁碟機代號設定為【S】及第二個磁碟區 (UFD-Windows) 的磁碟機代號設定為【W】。

請鍵入【Set-Partition -InputObject $SystemPartition -NewDriveLetter "S"】,並按【Enter】。再鍵入【Set-Partition -InputObject $OSPartition -NewDriveLetter "W"】,並按【Enter】。


當磁碟機代號設定完畢後,我們接著在鍵入【Set-Partition -InputObject $OSPartition -NoDefaultDriveLetter $TRUE】,並按【Enter】。此動作是將磁碟機代號設定為不顯示,也就是說當製作完畢後插入電腦時,不會顯示它們的磁碟機代號 (S及W) 。


接下來我們就要開始將 Windows 8 的映像檔部署到磁碟機【W】了。請鍵入【dism /apply-image /imagefile:D:\sources\install.wim /index:1 /applydir:W:\】,並按【Enter】,在此同時,請耐心等候,系統將會自動套用映像檔至隨身碟。


在映像檔部署的過程中,我們先開啟【記事本】,將下面文字複製到【記事本】中,並另存為【policy.xml】及【unattend.xml】,範例中【policy.xml】及【unattend.xml】是存在 C:\xml 的目錄中。

注意:在另存檔案時,【存檔類型】記得選【所有檔案 (*.*)】。如果沒選的話,所存下來的檔案會是【policy.xml.txt】及【unattend.xml.txt】,切記,【存檔類型】記得選【所有檔案 (*.*)】。

policy.xml內容如下虛線內文字,此訊息是讓 Windows To Go 開機隨身碟在任何電腦上開機後,會隱藏該電腦的磁碟機。

----------------- 以下文字 -----------------

<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
  <settings pass="offlineServicing">
    <component
        xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        language="neutral"
        name="Microsoft-Windows-PartitionManager"
        processorArchitecture="x86"
        publicKeyToken="31bf3856ad364e35"
        versionScope="nonSxS"
        >
      <SanPolicy>4</SanPolicy>
    </component>
    <component
        xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        language="neutral"
        name="Microsoft-Windows-PartitionManager"
        processorArchitecture="amd64"
        publicKeyToken="31bf3856ad364e35"
        versionScope="nonSxS"
        >
      <SanPolicy>4</SanPolicy>
    </component>
  </settings>
</unattend>

----------------- 以上文字 -----------------


unattend.xml內容如下虛線內文字,此動作是將 Windows RE 停用。

----------------- 以下文字 -----------------

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="oobeSystem">
        <component name="Microsoft-Windows-WinRE-RecoveryAgent"
          processorArchitecture="x86"
          publicKeyToken="31bf3856ad364e35" language="neutral"
          versionScope="nonSxS"
          xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <UninstallWindowsRE>true</UninstallWindowsRE>
        </component>
        <component name="Microsoft-Windows-WinRE-RecoveryAgent"
          processorArchitecture="amd64"
          publicKeyToken="31bf3856ad364e35" language="neutral"
          versionScope="nonSxS"
          xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <UninstallWindowsRE>true</UninstallWindowsRE>
        </component>
    </settings> 
</unattend>

----------------- 以上文字 -----------------


當完成映像檔套用後,請接著鍵入【bcdboot W:\Windows /f ALL /s S:】,此動作是將開機檔寫入到第一個啟動磁碟,也就是【S】。


當開機檔成功建立後,接下來就要將剛剛建立的【policy.xml】寫到隨身碟中,請鍵入【dism /image:W:\ /apply-unattend:C:\xml\policy.xml】,並按【Enter】。


再來就是要將【unattend.xml】寫到隨身碟中,請鍵入【copy C:\xml\unattend.xml W:\Windows\system32\sysprep】,並按【Enter】,最後在按【exit】來結束並關閉 PowerShell 命令提示字元的視窗。


大功告成,接下來我們就可以拿著這一個製作完成的 Windows To Go 開機隨身碟去試試囉!

完整指令流程 (範例是筆者的 LAB 環境,其相關位置請自行修改)

01、Get-Disk
02、$Disk = Get-Disk 1
03、Clear-Disk -InputObject $Disk -RemoveData
04、Y
05、Initialize-Disk -InputObject $Disk -PartitionStyle MBR
06、$SystemPartition = New-Partition -InputObject $Disk -Size (350MB) –IsActive
07、Format-Volume -NewFileSystemLabel "UFD-System" -FileSystem FAT32 -Partition $SystemPartition
08、Y
09、$OSPartition = New-Partition -InputObject $Disk –UseMaximumSize
10、Format-Volume -NewFileSystemLabel "UFD-Windows" -FileSystem NTFS -Partition $OSPartition
11、Y
12、Set-Partition -InputObject $SystemPartition -NewDriveLetter "S"
13、Set-Partition -InputObject $OSPartition -NewDriveLetter "W"
14、Set-Partition -InputObject $OSPartition -NoDefaultDriveLetter $TRUE
15、dism /apply-image /imagefile:D:\sources\install.wim /index:1 /applydir:W:\
16、bcdboot W:\Windows /f ALL /s S:
17、dism /image:W:\ /apply-unattend:C:\xml\policy.xml
18、copy C:\xml\unattend.xml W:\Windows\system32\sysprep
19、exit

完整指令流程畫面 (範例是筆者的 LAB 環境,其相關位置請自行修改)


相關資源:
Windows To Go Step by Step
Windows To Go:功能概觀
Windows To Go:常見問題集
何謂 Windows RE?
利用 Windows 8 企業版中的 【建立 Windows To Go 工作區】製作開機隨身碟

3 則留言:

  1. Dear Andy 老師,
    我用這篇的作法在$OSPartition = New-Partition -InputObject $Disk –UseMaximumSize 這一步的時候就出現錯誤訊息後面就不能再繼續了,請問這種錯誤跟隨身碟本身有關嗎? 也因為這一步不會過所以要format 剩餘空間的時候也不能 format。

    回覆刪除
    回覆
    1. 您好!
      上述有說明了
      2、準備相容於 Windows 8 的 USB 3.0 / 32G 以上的隨身碟。(需有通過 Windows To Go 認證或能相容於 Windows To Go )

      刪除