Windows 部署範例指令碼

更新日期: 2014年5月

適用於: Windows 8, Windows 8.1, Windows Server 2012, Windows Server 2012 R2

下列指令碼用於套用 Windows 映像接單後生產:使用線上服務對個別電腦進行最終更新以及部署 Windows 映像:啟用和保護電腦,透過使用 Windows 映像檔設定電腦,並設定一鍵重設功能。您可以從 OEM 合作夥伴中心下載這些指令碼。

這些程式碼範例大部分都是純文字,可以使用 [記事本] 儲存。特別註明的少數範例必須儲存為 UTF-8 檔案格式。

提供的範例同時適用於 UEFI 型與 BIOS 型電腦。

這些指令碼用於套用 Windows 映像接單後生產:使用線上服務對個別電腦進行最終更新以及部署 Windows 映像:啟用和保護電腦,透過使用 Windows 映像檔設定電腦。

搭配 DiskPart 使用這些指令碼,格式化和設定 Windows 的硬碟磁碟分割,包括修復工具。依需要調整磁碟分割以填滿磁碟機。

  • UEFI

    建立 Windows RE 工具磁碟分割、系統、MSR、Windows 以及復原映像磁碟分割。

    這個指令碼會暫時指派這些磁碟機代號:WinRE 工具 = T、系統=S、Windows=C 和復原=R。如果您部署到包含未格式化硬碟的電腦,請將該 Windows 磁碟機代號修改為靠近最後字母的磁碟機代號 (例如 W) 來避免磁碟機代號衝突。不要使用代號 X,因為這個磁碟機代號已保留給 Windows PE。電腦重新開機之後,Windows 磁碟分割會被指派磁碟機代號 C,其他磁碟分割則不會收到磁碟機代號。

    下圖顯示產生的磁碟分割設定:

    含復原的 EFI 磁碟分割圖表
    rem == CreatePartitions-UEFI.txt ==
    rem == These commands are used with DiskPart to
    rem    set up the drive and ecreate five partitions
    rem    for a UEFI/GPT-based PC.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    select disk 0
    clean
    convert gpt
    rem == 1. Windows RE tools partition ===============
    create partition primary size=300
    format quick fs=ntfs label="Windows RE tools"
    assign letter="T"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    rem == 2. System partition =========================
    create partition efi size=100
    rem ** NOTE: For Advanced Format 4Kn drives,
    rem          change this value to size = 260 **
    format quick fs=fat32 label="System"
    assign letter="S"
    rem == 3. Microsoft Reserved (MSR) partition =======
    create partition msr size=128
    rem == 4. Windows partition ========================
    rem ==    a. Create the Windows partition ==========
    create partition primary 
    rem ==    b. Create space for the recovery image ===
    shrink minimum=15000
    rem ==    c. Prepare the Windows partition ========= 
    format quick fs=ntfs label="Windows"
    assign letter="C"
    rem === 5. Recovery image partition ================
    create partition primary
    format quick fs=ntfs label="Recovery image"
    assign letter="R"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    list volume
    exit
    
  • BIOS

    建立系統、Windows 以及復原映像磁碟分割。

    這個指令碼會暫時指派這些磁碟機代號:系統=S、Windows=C 和復原=R。如果您部署到包含未格式化硬碟的電腦,請將該 Windows 磁碟機代號修改為靠近最後字母的磁碟機代號 (例如 W) 來避免磁碟機代號衝突。不要使用代號 X,因為這個磁碟機代號已保留給 Windows PE。電腦重新開機之後,Windows 磁碟分割會被指派磁碟機代號 C,其他磁碟分割則不會收到磁碟機代號。

    下圖顯示產生的磁碟分割設定:

    磁碟分割的圖表:復原、系統、Windows
    rem == CreatePartitions-BIOS.txt ==
    rem == These commands are used with DiskPart to
    rem    set up the drive and create three partitions
    rem    for a BIOS/MBR-based PC.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    select disk 0
    clean
    rem == 1. System partition ======================
    create partition primary size=350
    format quick fs=ntfs label="System"
    assign letter="S"
    active
    rem == 2. Windows partition =====================
    rem ==    a. Create the Windows partition =======
    create partition primary
    rem ==    b. Create space for the recovery image  
    shrink minimum=15000
    rem ==    c. Prepare the Windows partition ====== 
    format quick fs=ntfs label="Windows"
    assign letter="C"
    rem == 3. Recovery image partition ==============
    create partition primary
    format quick fs=ntfs label="Recovery image"
    assign letter="R" 
    set id=27
    list volume
    exit
    

建立硬碟磁碟分割後,使用此指令碼將 Windows 映像套用到電腦,並設定每個磁碟分割。可使用 Windows 映像的檔案,設定每個新建立的磁碟分割。

  • UEFI

    rem == ApplyImage-UEFI.bat ==
    
    rem == These commands deploy a specified Windows
    rem    image file to the Windows partition, and configure
    rem    the system partition.
    
    rem    Usage:   ApplyImage WimFileName 
    rem    Example: ApplyImage E:\Images\ThinImage.wim ==
    
    rem == Copy the image to the recovery partition ==
    md R:\RecoveryImage
    copy %1 R:\RecoveryImage\Install.wim
    
    rem == Apply the image to the Windows partition ==
    dism /Apply-Image /ImageFile:"R:\RecoveryImage\Install.wim" /Index:1 /ApplyDir:C:\
    
    rem == Copy boot files to the System partition ==
    C:\Windows\System32\bcdboot C:\Windows /s S:
    
    :rem == Copy the Windows RE image to the
    :rem    Windows RE Tools partition ==
    md T:\Recovery\WindowsRE
    xcopy /h C:\Windows\System32\Recovery\Winre.wim T:\Recovery\WindowsRE\
    
    :rem == Register the location of the recovery tools ==
    C:\Windows\System32\Reagentc /Setreimage /Path T:\Recovery\WindowsRE /Target C:\Windows
    
    :rem == Register the location of the
    :rem    push-button reset recovery image. ==
    C:\Windows\System32\Reagentc /Setosimage /Path R:\RecoveryImage /Target C:\Windows /Index 1
    
    :rem == Restrict permissions of recovery folder to local Admin group
    C:\Windows\System32\icacls R:\RecoveryImage /inheritance:r /T
    C:\Windows\System32\icacls R:\RecoveryImage /grant:r SYSTEM:(F) /T
    C:\Windows\System32\icacls R:\RecoveryImage /grant:r *S-1-5-32-544:(F) /T
    
    :rem == Verify the configuration status of the images. ==
    C:\Windows\System32\Reagentc /Info /Target C:\Windows
    
  • BIOS

    rem == ApplyImage-BIOS.bat ==
    
    rem == These commands deploy a specified Windows
    rem    image file to the Windows partition, and configure
    rem    the system partition.
    
    rem    Usage:   ApplyImage WimFileName 
    rem    Example: ApplyImage E:\Images\ThinImage.wim ==
    
    rem == Copy the image to the recovery partition ==
    md R:\RecoveryImage
    copy %1 R:\RecoveryImage\Install.wim
    
    rem == Apply the image to the Windows partition ==
    dism /Apply-Image /ImageFile:"R:\RecoveryImage\Install.wim" /Index:1 /ApplyDir:C:\
    
    rem == Copy boot files to the System partition ==
    C:\Windows\System32\bcdboot C:\Windows
    
    :rem == Copy the Windows RE image to the
    :rem    System partition ==
    md S:\Recovery\WindowsRE
    xcopy /h C:\Windows\System32\Recovery\Winre.wim S:\Recovery\WindowsRE\
    
    :rem == Register the location of the recovery tools ==
    C:\Windows\System32\Reagentc /Setreimage /Path S:\Recovery\WindowsRE /Target C:\Windows
    
    :rem == Register the location of the
    :rem    push-button reset recovery image. ==
    C:\Windows\System32\Reagentc /Setosimage /Path R:\RecoveryImage /Target C:\Windows /Index 1
    
    :rem == Restrict permissions of recovery folder to local Admin group
    C:\Windows\System32\icacls R:\RecoveryImage /inheritance:r /T
    C:\Windows\System32\icacls R:\RecoveryImage /grant:r SYSTEM:(F) /T
    C:\Windows\System32\icacls R:\RecoveryImage /grant:r *S-1-5-32-544:(F) /T
    
    :rem == Verify the configuration status of the images. ==
    C:\Windows\System32\Reagentc /Info /Target C:\Windows
    

搭配 DiskPart 使用這些指令碼,在設定修復磁碟分割之後隱藏它們。

  • UEFI

    rem === HideRecoveryPartitions-UEFI.txt ===
    select disk 0
    select partition 1
    remove
    set id=de94bba4-06d1-4d40-a16a-bfd50179d6ac
    gpt attributes=0x8000000000000001
    select partition 5
    remove
    set id=ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
    gpt attributes=0x0000000000000001
    list volume
    exit
    
    
  • BIOS

    rem === HideRecoveryPartitions-BIOS.txt ===
    select disk 0
    select partition 3
    set id=27
    remove
    list volume
    exit
    

這些設定檔用於套用 Windows 映像部署 Windows 映像:啟用和保護電腦,協助支援裸機復原。

將原件儲存在 E:\Deployment\RecoveryImage-BIOSE:\Deployment\RecoveryImage-UEFI 資料夾內,其中 E 是外接式 USB 硬碟的磁碟機代號。這些檔案會複製到復原映像磁碟分割資料夾:R:\RecoveryImage\

本節中每一個程式碼範例必須以 UTF-8 檔案格式儲存。

ResetConfig.xml 是 一鍵重設 功能所需的設定檔。此檔案會載入其他設定檔,以及在設定完成裸機重設後,定義磁碟分割設定。

此 XML 檔案必須以 UTF-8 檔案格式儲存。例如,在 [記事本] 中按一下 [檔案],然後按一下 [另存新檔]。在 [編碼] 方塊中按一下 [UTF-8],然後儲存這個檔案。請不要使用 ANSI 編碼。

每個 XML 檔案必須都命名為 ResetConfig.xml。如果您同時部署 UEFI 與 BIOS 電腦,請建立分開的資料夾來儲存這些檔案。

  • UEFI

    <?xml version="1.0" encoding="utf-8"?>
    <!-- ResetConfig.xml - UEFI version - UTF-8 File format-->
       <Reset>
          <Run Phase="BasicReset_BeforeImageApply">
             <Path>SaveLogFiles.cmd</Path>
             <Duration>4</Duration>
          </Run>      
          <Run Phase="BasicReset_AfterImageApply">
             <Path>RetrieveLogFiles.cmd</Path>
             <Duration>2</Duration>
          </Run>
          <Run Phase="FactoryReset_AfterDiskFormat">
             <Path>CheckPartitions.exe</Path>
             <Duration>2</Duration>
          </Run>
          <Run Phase="FactoryReset_AfterImageApply">
             <Path>InstallApps.cmd</Path>
             <Param>/allApps</Param>
             <Duration>2</Duration>
          </Run>
        <SystemDisk>
            <DiskpartScriptPath>ResetPartitions-UEFI.txt</DiskpartScriptPath>
            <MinSize>75000</MinSize>
            <WindowsREPartition>1</WindowsREPartition>
            <WindowsREPath>Recovery\WindowsRE</WindowsREPath>
            <OSPartition>4</OSPartition>
            <RecoveryImagePartition>5</RecoveryImagePartition>
            <RecoveryImagePath>RecoveryImage</RecoveryImagePath>
            <RestoreFromIndex>1</RestoreFromIndex>
            <RecoveryImageIndex>1</RecoveryImageIndex>
        </SystemDisk>
       </Reset>
    
  • BIOS

    <?xml version="1.0" encoding="utf-8"?>
    <!-- ResetConfig.xml - BIOS version - UTF-8 File format-->
       <Reset>
          <Run Phase="BasicReset_BeforeImageApply">
             <Path>SaveLogFiles.cmd</Path>
             <Duration>4</Duration>
          </Run>      
          <Run Phase="BasicReset_AfterImageApply">
             <Path>RetrieveLogFiles.cmd</Path>
             <Duration>2</Duration>
          </Run>
          <Run Phase="FactoryReset_AfterDiskFormat">
             <Path>RunDxDiag.cmd</Path>
             <Duration>2</Duration>
          </Run>
          <Run Phase="FactoryReset_AfterImageApply">
             <Path>RunDxDiag.cmd</Path>
             <Duration>2</Duration>
          </Run>
        <SystemDisk>
            <DiskpartScriptPath>ResetPartitions-BIOS.txt</DiskpartScriptPath>
            <MinSize>75000</MinSize>
            <WindowsREPartition>1</WindowsREPartition>
            <WindowsREPath>Recovery\WindowsRE</WindowsREPath>
            <OSPartition>2</OSPartition>
            <RecoveryImagePartition>3</RecoveryImagePartition>
            <RecoveryImagePath>RecoveryImage</RecoveryImagePath>
            <RestoreFromIndex>1</RestoreFromIndex>
            <RecoveryImageIndex>1</RecoveryImageIndex>
        </SystemDisk>
       </Reset>
    

此指令碼說明如何將動作新增至 一鍵重設 功能。當使用者重新整理電腦時,會執行此指令碼,並在 ResetConfig.xml 中參照。

這個指令碼必須以 UTF-8 檔案格式儲存。例如,在 [記事本] 中按一下 [檔案],然後按一下 [另存新檔]。在 [編碼] 方塊中按一下 [UTF-8],然後儲存這個檔案。請不要使用 ANSI 編碼。

:rem === SaveLogFiles.cmd - UTF-8 File format ===

:rem == This sample script preserves files that would 
:rem    otherwise be removed by placing them in a 
:rem    temporary location in memory, to be retrieved by
:rem    RetrieveLogFiles.cmd.

:rem == 1. Use the registry to identify the location of
:rem       the new operating system and the primary hard
:rem       drive. For example, 
:rem       %TARGETOS% may be defined as C:\Windows
:rem       %TARGETOSDRIVE% may be defined as C:
for /F "tokens=1,2,3 delims= " %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\RecoveryEnvironment" /v TargetOS') DO SET TARGETOS=%%C

for /F "tokens=1 delims=\" %%A in ('Echo %TARGETOS%') DO SET TARGETOSDRIVE=%%A

:rem == 2. Copy old logs to a temporary folder in memory
mkdir X:\Temp
xcopy %TARGETOS%\Logs\*.* X:\temp /cherkyi

EXIT 0

此指令碼說明如何將動作新增至 一鍵重設 功能。當使用者重新整理電腦時,會執行此指令碼,並在 ResetConfig.xml 中參照。

這個指令碼必須以 UTF-8 檔案格式儲存。例如,在 [記事本] 中按一下 [檔案],然後按一下 [另存新檔]。在 [編碼] 方塊中按一下 [UTF-8],然後儲存這個檔案。請不要使用 ANSI 編碼。

:rem === RetrieveLogFiles.cmd - UTF-8 File format ===

:rem == This sample script retrieves the files that 
:rem    were saved in memory by 
:rem    SaveLogFiles.cmd,
:rem    and adds them back to the system.
:rem

:rem == 1. Use the registry to identify the location of
:rem       the new operating system and the primary hard
:rem       drive. For example, 
:rem       %TARGETOS% may be defined as C:\Windows
:rem       %TARGETOSDRIVE% may be defined as C:
for /F "tokens=1,2,3 delims= " %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\RecoveryEnvironment" /v TargetOS') DO SET TARGETOS=%%C

for /F "tokens=1 delims=\" %%A in ('Echo %TARGETOS%') DO SET TARGETOSDRIVE=%%A

:rem == 2. Copy the old logs to the new OS 
:rem       at C:\Windows\OldLogs
mkdir %TARGETOS%\OldLogs
xcopy X:\Temp\*.* %TARGETOS%\OldLogs /cherkyi

EXIT 0

此指令碼說明如何將動作新增至 一鍵重設 功能。當使用者重設電腦時,會執行此指令碼,並在 ResetConfig.xml 中參照。

此指令碼必須以 UTF-8 檔案格式儲存。例如,在 [記事本] 中按一下 [檔案],然後按一下 [另存新檔]。在 [編碼] 方塊中按一下 [UTF-8],然後儲存這個檔案。請不要使用 ANSI 編碼。

:rem === RunDxDiag.cmd - UTF-8 File format ===

:rem == This sample script runs a system diagnostic,
:rem    and sends the output to the C:\Fabrikam folder.


:rem == 1. Use the registry to identify the location of
:rem       the new operating system and the primary hard
:rem       drive. For example, 
:rem       %TARGETOS% may be defined as C:\Windows
:rem       %TARGETOSDRIVE% may be defined as C:
for /F "tokens=1,2,3 delims= " %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\RecoveryEnvironment" /v TargetOS') DO SET TARGETOS=%%C

:rem == 2. Run system diagnostics using the
:rem       DirectX Diagnostic tool, and save the 
:rem       results to the C:\Fabrikam folder. ==

mkdir %TARGETOSDRIVE%\Fabrikam
%TARGETOS%\system32\dxdiag.exe /whql:off /t %TARGETOSDRIVE%\Fabrikam\DxDiag-TestLogFiles.txt

EXIT 0

一鍵重設裸機修復工具會使用這些指令碼,在硬碟重設後重新分割硬碟。

  1. UEFI

    rem === ResetPartitions-UEFI.txt ===
    rem == These commands are used with DiskPart to
    rem    reset the drive and recreate five partitions
    rem    for a UEFI/GPT-based PC.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    rem == The differences between this file and
    rem    CreatePartitions-UEFI.txt
    rem    are noted in parenthesis.
    rem       (NOT USED: select disk 0)
    rem       (NOT USED: clean)
    convert gpt
    rem == 1. Windows RE tools partition ===============
    create partition primary size=300
    format quick fs=ntfs label="Windows RE tools"
    assign letter="T"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    rem == 2. System partition =========================
    create partition efi size=100
    rem ** NOTE: For Advanced Format 4Kn drives,
    rem          change this value to size = 260 **
    format quick fs=fat32 label="System"
    assign letter="S"
    rem == 3. Microsoft Reserved (MSR) partition =======
    create partition msr size=128
    rem == 4. Windows partition ========================
    rem ==    a. Create the Windows partition ==========
    create partition primary 
    rem ==    b. Create space for the recovery image ===
    shrink minimum=15000
    rem ==    c. Prepare the Windows partition ========= 
    format quick fs=ntfs label="Windows"
    assign letter="C"
    rem === 5. Recovery image partition ================
    create partition primary
    format quick fs=ntfs label="Recovery image"
    assign letter="R"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    gpt attributes=0x8000000000000001
    list volume
    exit
    
  2. BIOS:

    rem === ResetPartitions-BIOS.txt ===
    rem == These commands are used with DiskPart to
    rem    reset the drive and create three partitions
    rem    for a BIOS/MBR-based PC.
    rem    Adjust the partition sizes to fill the drive
    rem    as necessary. ==
    rem == The differences between this file and
    rem    CreatePartitions-BIOS.txt
    rem    are noted in parenthesis.
    rem       (NOT USED: select disk 0 )
    rem       (NOT USED: clean )
    rem == 1. System partition ======================
    create partition primary size=350
    format quick fs=ntfs label="System"
    assign letter="S"
    active
    rem == 2. Windows partition =====================
    rem ==    a. Create the Windows partition =======
    create partition primary
    rem ==    b. Create space for the recovery image  
    shrink minimum=15000
    rem ==    c. Prepare the Windows partition ====== 
    format quick fs=ntfs label="Windows"
    assign letter="C"
    rem == 3. Recovery image partition ==============
    create partition primary
    format quick fs=ntfs label="Recovery image"
    assign letter="R" 
    set id=27
    list volume
    exit
    

這個指令碼會使用 DirectX 診斷工具建立系統報告,然後將它傳送到名為 C:\Fabrikam 的新資料夾。

md C:\Fabrikam
C:\Windows\System32\dxdiag /t C:\Fabrikam\DxDiag-TestLogFiles.txt

另請參閱

顯示:
© 2015 Microsoft