cschneegans / unattend-generator

.NET Core library to create highly customized autounattend.xml files
https://schneegans.de/windows/unattend-generator/
MIT License
680 stars 62 forks source link

Execution of .bat File #3

Closed hanton94 closed 8 months ago

hanton94 commented 8 months ago

Would it be possible to add the option to execute a .bat or .cmd file as an administrator that is already previously saved in a folder?

The idea is for this .bat file to run only once during the system installation.

Thank you, I await your response ^^

cschneegans commented 8 months ago

Such a mechanism already exists in Windows Setup: If present, the file %WINDIR%\Setup\Scripts\SetupComplete.cmd will run during Windows Setup with SYSTEM permissions.

Also note that setting Schneegans.Unattend.Configuration.RunScriptOnFirstLogon = true will run the file %WINDIR%\Setup\Scripts\UserFirstLogon.cmd whenever a user logs on for the first time.

However, I will consider adding an option to run script code from autounattend.xml in a self-contained way, without the need to add SetupComplete.cmd and UserFirstLogon.cmd.

cschneegans commented 8 months ago

I have now added settings (via https://github.com/cschneegans/unattend-generator/commit/6d3114eb83f304ca1f2bea84d88d3a88c87e1d43) to run custom .cmd or .ps1 scripts during various stages of the Windows Setup process. See the Run custom scripts section in the web interface.

procionecurioso commented 8 months ago

Hi you had given me a script to disable windows hibernation, I wanted to know how to make it work in the interface? To make it run by itself when I log on windows, I need administrator permissions to run the script and when I tried this section it didn't work 'Run these cmd commands when the first user logs on'

cschneegans commented 8 months ago

Since powercfg.exe settings are not per-user, do not use a FirstLogon script, but rather a System script.

Furthermore, unlike SetupComplete.cmd, the new custom scripts can run PowerShell commands directly, eliminating the need to call powershell.exe -NoProfile -Command "...". Therefore, a custom script might look like this:

powercfg.exe /HIBERNATE OFF;
$out = powercfg.exe /DUPLICATESCHEME 'e9a42b02-d5df-448d-aa00-03f14749eb61';
if( $out -match '\s([a-f0-9-]{36})\s' ){
    powercfg.exe /SETACTIVE $Matches[1];
}

This link%7B%0D%0A%09powercfg.exe%20%2FSETACTIVE%20$Matches%5B1%5D;%0D%0A%7D&SystemScriptType=Ps1) will configure the SystemScript and SystemScriptType fields accordingly.

I have tested the resulting autounattend.xml file with a virtual Windows 10 Home installation, and the Ultimate Performance power plan was indeed enabled.

cschneegans commented 8 months ago

Also note that due to a bug custom scripts were not created (and hence not executed) if the C:\Windows\Setup\Scripts folder did not exist.

If you refresh your autounattend.xml file, you will notice an additional command <Path>cmd.exe /c "mkdir C:\Windows\Setup\Scripts"</Path>.

hanton94 commented 8 months ago

Thank you very much, it's just what I was looking for. I'll try to implement it to see how it works.