Cách thêm đoạn script tùy chỉnh để khởi chạy trong tiến trình cài đặt Windows

Khi thiết lập và cài đặt, Windows được cấu hình chạy các đoạn script tùy chỉnh trước khi hoàn thành quá trình cài đặt vào đến desktop. Tuy nhiên đoạn script này phải được cấu hình và thêm vào trước khi cài đặt. Có 2 kịch bản script sẽ khởi chạy đó là trước khi vào đến màn hình thiết lập tài khoản bằng tệp SetupComplete.cmd và sau khi đã tạo tài khoản bằng tệp Unattend.xml.

add-a-custom-script-to-windows-setup
add-a-custom-script-to-windows-setup

Tệp SetupComplete.cmd

Đoạn script khởi chạy trước khi tạo tài khoản được tạo bằng tệp SetupComplete.cmd và được đặt trong thư mục Scripts (tạo mới) theo đường dẫn C:WindowsSetup hay %WINDIR%Setup

Ví dụ về nội dung trong tệp SetupComplete.cmd

Copy
powercfg -h on

reg add “HKLMSOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem” /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f

reg add “HKLMSOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem” /v PromptOnSecureDesktop /t REG_DWORD /d 0 /f

Dism /Online /Remove-ProvisionedAppxPackage /PackageName:Microsoft.WindowsFeedbackHub_2018.822.2.0_neutral_~_8wekyb3d8bbwe

Dism /Online /Remove-ProvisionedAppxPackage /PackageName:Microsoft.Wallet_2.2.18179.0_neutral_~_8wekyb3d8bbwe

Dism /Online /Remove-ProvisionedAppxPackage /PackageName:Microsoft.MicrosoftSolitaireCollection_4.2.8172.0_neutral_~_8wekyb3d8bbwe

del /q /f %windir%system32sysprepSysprep_succeeded.tag

del /q /f %windir%system32sysprepunattend.xml

del /q /f %windir%pantherunattend.xml

del /q /f %windir%pantherunattendgcsetupact.log

rd /s /q %windir%SetupScripts


Lưu ý: Lưu tệp với định dạng All files và Encoding: ANSI

Sau khi Windows được cài đặt nhưng trước khi màn hình đăng nhập xuất hiện, Windows sẽ tìm kiếm tệp SetupComplete.cmd trong thư mục Scripts theo đường dẫn %WINDIR%Setup. Nếu tìm thấy tệp SetupComplete.cmd , Windows sẽ chạy tập lệnh. Hệ thống sẽ ghi lại hành động trong tệp Setupact.log theo đường dẫn %WINDIR%PantherUnattendGC

Cảnh báo: Bạn không thể khởi động lại hệ thống và tiếp tục chạy SetupComplete.cmd, bạn không nên khởi động lại hệ thống bằng cách thêm một lệnh như shutdown -r trong tệp, điều này sẽ đặt hệ thống ở trạng thái xấu.

Tệp Unattend.xml

Đoạn script khởi chạy sau khi tạo tài khoản được tạo bằng tệp Unattend.xml được đặt trong thư mục Panther theo đường dẫn C:Windows hoặc trong thư mục Sysprep theo đường dẫn C:WindowsSystem32 hoặc được đặt cùng phân vùng gốc của USB, đĩa cài đặt ngay cạnh tệp setup.exe

Các lệnh command trong nội dung của tệp Unattend.xml được cấu hình tham chiếu theo đường dẫn

Copy
oobeSystemMicrosoft-Windows-Shell-SetupFirstLogonCommandsSynchronousCommand

Cụ thể nội dung tệp như sau:

Copy
<?xml version="1.0" encoding="UTF-8"?>

<unattend xmlns=”urn:schemas-microsoft-com:unattend”>

<settings pass=”oobeSystem”>

<!–Win 32 bit–>

<component xmlns:wcm=”http://schemas.microsoft.com/WMIConfig/2002/State” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” name=”Microsoft-Windows-Shell-Setup” processorArchitecture=”x86″ publicKeyToken=”31bf3856ad364e35″ language=”neutral” versionScope=”nonSxS”>

<FirstLogonCommands>

<SynchronousCommand wcm:action=”add”>

<CommandLine>Lệnh 1</CommandLine>

<Order>1</Order>

<RequiresUserInput>false</RequiresUserInput>

</SynchronousCommand>

<SynchronousCommand wcm:action=”add”>

<CommandLine>Lệnh 2</CommandLine>

<Order>2</Order>

<RequiresUserInput>false</RequiresUserInput>

</SynchronousCommand>

<SynchronousCommand wcm:action=”add”>

<CommandLine>Lệnh 3</CommandLine>

<Order>3</Order>

<RequiresUserInput>false</RequiresUserInput>

</SynchronousCommand>

<SynchronousCommand wcm:action=”add”>

<CommandLine>Lệnh n</CommandLine>

<Order>n</Order>

<RequiresUserInput>false</RequiresUserInput>

</SynchronousCommand>

</FirstLogonCommands>

</component>

<!–Win 64 bit–>

<component xmlns:wcm=”http://schemas.microsoft.com/WMIConfig/2002/State” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” name=”Microsoft-Windows-Shell-Setup” processorArchitecture=”amd64″ publicKeyToken=”31bf3856ad364e35″ language=”neutral” versionScope=”nonSxS”>

<FirstLogonCommands>

<SynchronousCommand wcm:action=”add”>

<CommandLine>Lệnh 1</CommandLine>

<Order>1</Order>

<RequiresUserInput>false</RequiresUserInput>

</SynchronousCommand>

<SynchronousCommand wcm:action=”add”>

<CommandLine>Lệnh 2</CommandLine>

<Order>2</Order>

<RequiresUserInput>false</RequiresUserInput>

</SynchronousCommand>

<SynchronousCommand wcm:action=”add”>

<CommandLine>Lệnh 3</CommandLine>

<Order>3</Order>

<RequiresUserInput>false</RequiresUserInput>

</SynchronousCommand>

<SynchronousCommand wcm:action=”add”>

<CommandLine>Lệnh n</CommandLine>

<Order>n</Order>

<RequiresUserInput>false</RequiresUserInput>

</SynchronousCommand>

</FirstLogonCommands>

</component>

</settings>

</unattend>


Lưu ý: Lưu tệp với định dạng All files và Encoding: UTF-8

Sau khi Windows được cài đặt sau khi đã đăng nhập tài khoản, Windows sẽ thực thi lệnh trong tệp Unattend.xml, hệ thống sẽ ghi lại hành động trong tệp Setupact.log theo đường dẫn %WINDIR%PantherUnattendGC

Khi nào sử dụng tệp SetupComplete.cmd và khi nào sử dụng tệp Unattend.xml

So với tệp Unattend.xml thì tệp SetupComplete.cmd sẽ viết tập hợp các lệnh đơn giản hơn nhưng các lệnh thực thi là lệnh hệ thống không liên quan đến tài khoản người dùng đăng nhập. Lấy ví dụ minh họa khi thêm lệnh REG ADD

Copy
reg add "HKLMSOFTWAREPoliciesMicrosoftWindowsWindowsUpdateAU" /v "NoAutoUpdate" /t REG_DWORD /d 0 /f

Sử dụng trong SetupComplete.cmd và Unattend.xml đều được nhưng lệnh sau đây:

Copy
reg add "HKCUSoftwareMicrosoftWindowsCurrentVersionContentDeliveryManager" /v "SilentInstalledAppsEnabled" /t REG_DWORD /d 0 /f

Chỉ có thể thực thi được trong tệp Unattend.xml

Tóm lại các lệnh nào phải sử dụng đến tài khoản thì sử dụng tệp Unattend.xml còn không cứ dùng tệp SetupComplete.cmd

Cách thêm tệp SetupComplete.cmd và tệp Unattend.xml

Trường hợp rebuild tệp install.wim khi khi đã mount tệp để chỉnh sửa

+ Tệp SetupComplete.cmd

Tạo mới thư mục Scripts lưu tệp SetupComplete.cmd, sao chép thư mục này vào thư mục Setup của thư mục Windows trong thư mục đã mout tệp install.wim, ví dụ

Copy
C:offlinemountWindowsSetupScriptsSetupComplete.cmd

+ Tệp Unattend.xml

Tạo mới thư mục Panther lưu tệp Unattend.xml, sao chép thư mục này vào thư mục Windows trong thư mục đã mout tệp install.wim, ví dụ

Copy
C:offlinemountWindowsPantherUnattend.xml

Trường hợp đã cài đặt và đang trong chế độ Audit

+ Tệp SetupComplete.cmd

Tạo mới thư mục Scripts lưu tệp SetupComplete.cmd, sao chép thư mục này vào thư mục Setup của thư mục Windows

Copy
C:WindowsSetupScriptsSetupComplete.cmd

+ Tệp Unattend.xml

Sao chép tệp Unattend.xml vào thư mục Panther của thư mục Windows, hoặc vào thư mục Sysprep trong thư mục System32 của thư mục Windows

Copy
C:WindowsPantherUnattend.xml

C:WindowsSystem32SysprepUnattend.xml


Trường hợp cài đặt từ thiết bị gắn ngoài

Tham khảo làm theo bài viết: Hướng dẫn cấu hình sao chép và thực thi tệp tự động trong suốt quá trình cài đặt Windows

View Comments

Leave a Reply

Your email address will not be published. Required fields are marked *