將自訂指令碼新增至 Windows 安裝程式Add a Custom Script to Windows Setup

Windows 安裝程式指令碼:Setupcomplete.cmdErrorHandler.cmd期間或在 Windows 安裝程式程序之後執行的自訂指令碼。Windows Setup scripts: Setupcomplete.cmd and ErrorHandler.cmd are custom scripts that run during or after the Windows Setup process. 它們可以用來安裝應用程式,或使用執行其他工作cscript/wscript指令碼。They can be used to install applications or run other tasks by using cscript/wscript scripts.

  • %WINDIR%\安裝程式\指令碼\SetupComplete.cmd:使用者會看到桌面之後,立即執行這個指令碼。%WINDIR%\Setup\Scripts\SetupComplete.cmd: This script runs immediately after the user sees the desktop. 使用 OEM 產品金鑰時,會停用此設定。This setting is disabled when using OEM product keys. 它會以本機系統權限執行。It runs with local system permission.
  • %WINDIR%\安裝程式\指令碼\ErrorHandler.cmd:安裝程式發生嚴重錯誤時,會自動執行此指令碼。%WINDIR%\Setup\Scripts\ErrorHandler.cmd: This script runs automatically when Setup encounters a fatal error. 它會以本機系統權限執行。It runs with local system permission.

Windows 自動安裝指令碼:使用其中一個設定執行 Windows 安裝過程中建立的 Unattend.xml 檔案。Windows Unattend scripts: Create an Unattend.xml file with one of these settings to run during the Windows Setup process. 這可以搭配 OEM 產品金鑰。This can be used with OEM product keys.

若要執行的服務或在相同的時間可以啟動的命令,使用 RunAsynchronousCommands。To run services or commands that can start at the same time, use RunAsynchronousCommands. 若要執行需要其他命令之前完成的命令可以啟動,使用 RunSynchronousCommands。To run commands that need to finish before other commands can start, use RunSynchronousCommands.

附註  As of Windows 10、windows Microsoft Windows Shell-安裝\LogonCommands\AsynchronousCommand現在的運作方式類似 LogonCommands\AsynchronousCommand: 使用這些方法的所有命令的自動都安裝設定現在會啟動一次,並再等待前一個命令來完成。Note  As of Windows 10, Microsoft-Window-Shell-Setup\LogonCommands\AsynchronousCommand now works like LogonCommands\AsynchronousCommand: all commands using these unattend settings are now started at the same time, and no longer wait for the previous command to finish.

其中某些設定使用者內容中執行,其他人在系統內容,根據設定階段中執行。Some of these settings run in the user context, others run in the system context depending on the configuration pass.

在安裝程式完成 (SetupComplete.cmd) 後,執行指令碼Run a script after setup is complete (SetupComplete.cmd)

作業的順序Order of operations

  1. 已安裝 Windows,但登入畫面出現之前,Windows 安裝程式在搜尋後SetupComplete.cmd中的檔案 %WINDIR%\安裝\指令碼\目錄.After Windows is installed but before the logon screen appears, Windows Setup searches for the SetupComplete.cmd file in the %WINDIR%\Setup\Scripts\ directory.

  2. 如果SetupComplete.cmd找到檔案,Windows 安裝程式會執行指令碼。If a SetupComplete.cmd file is found, Windows Setup runs the script. Windows 安裝程式記錄的動作c:\Windows\Panther\UnattendGC\Setupact.log檔案。Windows Setup logs the action in the C:\Windows\Panther\UnattendGC\Setupact.log file.

    安裝程式不會驗證任何結束代碼或指令碼中的錯誤層級之後,它會執行SetupComplete.cmdSetup does not verify any exit codes or error levels in the script after it executes SetupComplete.cmd.

    警告  無法將系統重新開機和執行的繼續SetupComplete.cmdWarning  You cannot reboot the system and resume running SetupComplete.cmd. 您應該藉由將命令加入這類不重新啟動系統關機 rYou should not reboot the system by adding a command such as shutdown -r. 這會讓系統處於不正常的狀態。This will put the system in a bad state.

  3. 如果電腦加入網域,在安裝期間,為網域所定義的群組原則不適用於之前的電腦Setupcomplete.cmd完成。If the computer joins a domain during installation, the Group Policy that is defined in the domain is not applied to the computer until Setupcomplete.cmd is finished. 這是為了確定群組原則設定活動不會使用指令碼。This is to make sure that the Group Policy configuration activity does not interfere with the script.

執行指令碼,如果 Windows 安裝程式發生嚴重錯誤 (ErrorHandler.cmd)Run a script if Windows Setup encounters a fatal error (ErrorHandler.cmd)

許多系統安裝在同一時間時,適合使用此指令碼。This script is useful when you're installing many systems at the same time. 這可協助您偵測在 Windows 安裝期間發生錯誤時。This helps you detect when an error occurs during Windows Setup. 載入時,安裝程式會自動執行指令碼可包含自訂命令或動作來解決錯誤的原因。When it does, Setup automatically runs a script that can contain custom commands or actions to address the cause of the error.

如果 Windows 安裝程式發生嚴重錯誤,而且無法完成安裝,Windows 安裝程式會搜尋下列目錄中的命令指令碼: %WINDIR%\安裝程式\指令碼\ErrorHandler.cmdIf Windows Setup encounters a fatal error and is prevented from completing the installation, Windows Setup searches for a command script in the following directory: %WINDIR%\Setup\Scripts\ErrorHandler.cmd. 兩個動作之一,就會根據是否找到指令碼。One of two actions will occur, depending on whether the script is found.

  • 如果找不到指令碼,對話方塊會顯示錯誤文字。If the script is not found, a dialog box is displayed with the error text. Windows 安裝程式結束之前,使用者必須關閉對話方塊。A user must dismiss the dialog box before Windows Setup exits.
  • 如果找到指令碼,以同步方式執行指令碼。If the script is found, the script executes synchronously. 會不顯示任何對話方塊或錯誤文字。No dialog box or error text is displayed. 在後ErrorHandler.cmd指令碼完成執行時,Windows 安裝程式結束。After the ErrorHandler.cmd script has finished running, Windows Setup exits.

Windows 安裝程式的階段,根據電腦將會回到從中 Windows 安裝程式已執行的環境,例如舊版的作業系統或 Windows 預先安裝環境 (Windows PE),例如。Depending on the phase of Windows Setup, the computer will return to the environment from which Windows Setup was executed, such as an earlier version of the operating system or Windows Preinstallation Environment (Windows PE), for example.

當 Windows 安裝程式發生一個以上的錯誤,並會執行一次以上 ErrorHandler.cmd 指令碼時,則可能會執行個體。There may be instances when Windows Setup encounters more than one error and runs the ErrorHandler.cmd script more than once. 開發的程式碼時ErrorHandler.cmd,請確定您可以執行此指令碼多次。When developing the code for ErrorHandler.cmd, make sure that you can run this script multiple times.

若要使用 ErrorHandler.cmd,您可以執行下列其中一項:To use ErrorHandler.cmd, you can do either of the following:

  • 掛接映像,並將它新增至映像,在 %WINDIR%\安裝程式\指令碼\ErrorHandler.cmdMount the image, and add it to the image, in %WINDIR%\Setup\Scripts\ErrorHandler.cmd. 取消掛接映像。Unmount the image.

    - 或者 --or-

  • 新增ErrorHandler.cmd暫存檔案位置 (例如 c:\Temp\ErrorHandler.cmd),然後執行的 Windows 安裝程式會使用 /m選項。Add ErrorHandler.cmd to a temporary file location (for example, C:\Temp\ErrorHandler.cmd), and then run Windows Setup using the /m option.

    Setup /m:C:\Temp
    

    若要進一步了解,請參閱Windows Setup Command-Line OptionsTo learn more, see Windows Setup Command-Line Options.

相關的主題Related topics

Windows 安裝程式技術參照Windows Setup Technical Reference

從 DVD 開機Boot from a DVD

使用 Windows 安裝程式的組態集Use a Configuration Set with Windows Setup

部署自訂映像Deploy a Custom Image

開機為稽核模式或 OOBE 的 WindowsBoot Windows to Audit Mode or OOBE

將裝置驅動程式新增至 Windows 中,在 Windows 安裝期間Add Device Drivers to Windows During Windows Setup