Skip to content

Commit 139c342

Browse files
committed
windows: 支持使用非 administrator 账号
1 parent 107c56a commit 139c342

6 files changed

Lines changed: 124 additions & 12 deletions

File tree

.github/workflows/run_reinstall.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
3838
${{ matrix.command }} netboot.xyz
3939
${{ matrix.command }} dd --img=https://download.opensuse.org/tumbleweed/appliances/openSUSE-MicroOS.x86_64-SelfInstall.raw.xz
40-
${{ matrix.command }} windows --image-name='Windows Server blah' --iso https://aka.ms/HCIReleaseImage
40+
${{ matrix.command }} windows --image-name='Windows Server blah' --iso https://aka.ms/HCIReleaseImage --username administrator
4141
4242
${{ matrix.command }} reset
4343

README.en.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,9 @@ bash reinstall.sh netboot.xyz
327327
>
328328
> If the script was run by mistake, you can run `bash reinstall.sh reset` before rebooting to cancel the reinstallation operation.
329329
330-
- Username `administrator`. The script prompts for a password. If left blank, a random one is generated.
331-
- If remote login fails, try using the username `.\administrator`.
330+
- The script prompts for a username. If left blank, will use `administrator`.
331+
- The script prompts for a password. If left blank, will use a random one.
332+
- If remote login fails, try adding `.\` before the username, for example, `.\administrator`.
332333
- The machine with a static IP will automatically configure the IP. It may take a few minutes to take effect on the first boot.
333334
- Supports ISO images in any language.
334335
- Automatically bypassing Windows 11 hardware requirements.
@@ -446,6 +447,7 @@ bash reinstall.sh windows \
446447

447448
#### Optional Parameters
448449

450+
- `--username USERNAME` Set Username (for Windows only)
449451
- `--password PASSWORD` Set Password
450452
- `--allow-ping` Configure Windows Firewall to Allow Ping Responses
451453
- `--rdp-port PORT` Change RDP port

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,9 @@ bash reinstall.sh netboot.xyz
327327
>
328328
> 如果不小心运行了脚本,可以在重启前运行 `bash reinstall.sh reset` 取消重装
329329
330-
- 用户名为 `administrator`,脚本会提示输入密码,不输入则使用随机密码
331-
- 如果远程登录失败,可以尝试使用用户名 `.\administrator`
330+
- 脚本会提示输入用户名,不输入则使用 `administrator`
331+
- 脚本会提示输入密码,不输入则使用随机密码
332+
- 如果远程登录失败,请尝试在用户名前添加 `.\`,例如 `.\administrator`
332333
- 静态机器会自动配置好 IP,可能首次开机几分钟后才生效
333334
- 支持任意语言的 ISO
334335
- 自动绕过 Windows 11 硬件限制
@@ -446,6 +447,7 @@ bash reinstall.sh windows \
446447

447448
#### 可选参数
448449

450+
- `--username USERNAME` 设置用户名(仅限 Windows)
449451
- `--password PASSWORD` 设置密码
450452
- `--allow-ping` 设置 Windows 防火墙允许被 Ping
451453
- `--rdp-port PORT` 更改 RDP 端口

reinstall.sh

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,59 @@ trim() {
23422342
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
23432343
}
23442344

2345+
assert_username_valid() {
2346+
if ! msg=$(is_username_valid); then
2347+
error_and_exit "$msg"
2348+
fi
2349+
}
2350+
2351+
is_username_valid() {
2352+
# https://learn.microsoft.com/windows-hardware/customize/desktop/unattend/microsoft-windows-shell-setup-useraccounts-localaccounts-localaccount-name
2353+
# 不能为 none [ ] / \ : | < > + = ; , ? * % @
2354+
2355+
# 账号为空,则使用 Administrator
2356+
if [ -z "$username" ]; then
2357+
echo "Username: Will use the built-in Administrator account in ISO language."
2358+
return 0
2359+
fi
2360+
2361+
if [ "$(to_lower <<<"$username")" = none ]; then
2362+
echo "Username: Do not use the name \"NONE\", this is a restricted username."
2363+
return 1
2364+
fi
2365+
2366+
if grep -q '[][/\:|<>+=;,?*%@]' <<<"$username"; then
2367+
echo "Username: Do not use any of the following characters: / \ [ ] : | < > + = ; , ? * % @"
2368+
return 1
2369+
fi
2370+
2371+
# 如果输入以下用户名则忽略,并使用系统内置的 Administrator 账号
2372+
# 防止系统有两个不同语言的 Administrator 账号而造成困扰
2373+
for builtin_username in \
2374+
administrator \
2375+
administrador \
2376+
administrateur \
2377+
administratör \
2378+
администратор \
2379+
järjestelmänvalvoja \
2380+
rendszergazda; do
2381+
if [ "$(to_lower <<<"$username")" = "$builtin_username" ]; then
2382+
echo "Username: Will use the built-in Administrator account in ISO language."
2383+
unset username
2384+
return 0
2385+
fi
2386+
done
2387+
}
2388+
2389+
prompt_username() {
2390+
info "prompt username"
2391+
warn false "Leave blank to use Administrator"
2392+
warn false "不填写则使用 Administrator"
2393+
IFS= read -r -p "Username: " username
2394+
username="$(printf "%s" "$username" | trim)"
2395+
assert_username_valid
2396+
}
2397+
23452398
prompt_password() {
23462399
info "prompt password"
23472400
warn false "Leave blank to use a random password."
@@ -3118,7 +3171,7 @@ build_extra_cmdline() {
31183171
# https://salsa.debian.org/installer-team/rootskel/-/blob/master/src/lib/debian-installer-startup.d/S02module-params?ref_type=heads
31193172
for key in confhome hold force_boot_mode force_cn force_old_windows_setup cloud_image main_disk \
31203173
elts deb_mirror \
3121-
ssh_port rdp_port web_port allow_ping; do
3174+
username ssh_port rdp_port web_port allow_ping; do
31223175
value=${!key}
31233176
if [ -n "$value" ]; then
31243177
is_need_quote "$value" &&
@@ -4312,6 +4365,7 @@ for o in ci installer debug minimal allow-ping force-cn help \
43124365
img: \
43134366
cloud-data: \
43144367
lang: \
4368+
user: username: \
43154369
passwd: password: \
43164370
ssh-port: \
43174371
ssh-key: public-key: \
@@ -4446,6 +4500,14 @@ while true; do
44464500
force_boot_mode=$2
44474501
shift 2
44484502
;;
4503+
--user | --username)
4504+
if ! [ "$distro" = windows ]; then
4505+
error_and_exit "$1 is only supported for installing Windows."
4506+
fi
4507+
username="$(printf "%s" "$2" | trim)"
4508+
assert_username_valid
4509+
shift 2
4510+
;;
44494511
--passwd | --password)
44504512
[ -n "$2" ] || error_and_exit "Need value for $1"
44514513
password=$2
@@ -4621,6 +4683,11 @@ done
46214683
# 检查必须的参数
46224684
verify_os_args
46234685

4686+
# 用户名
4687+
if [ "$distro" = windows ] && [ -z "$username" ]; then
4688+
prompt_username
4689+
fi
4690+
46244691
# 密码
46254692
if ! is_netboot_xyz && [ -z "$ssh_keys" ] && [ -z "$password" ]; then
46264693
if is_use_dd; then
@@ -4900,7 +4967,7 @@ info 'info'
49004967
echo "$distro $releasever"
49014968

49024969
case "$distro" in
4903-
windows) username=administrator ;;
4970+
windows) username=${username:-administrator} ;;
49044971
netboot.xyz) username= ;;
49054972
dd | *) username=root ;;
49064973
esac

trans.sh

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ get_password_windows_administrator_base64() {
584584
get_config password-windows-administrator-base64
585585
}
586586

587+
get_password_windows_user_base64() {
588+
get_config password-windows-user-base64
589+
}
590+
587591
get_password_plaintext() {
588592
get_config password-plaintext
589593
}
@@ -7175,20 +7179,44 @@ EOF
71757179
}
71767180
71777181
# 修改应答文件
7182+
apk add xmlstarlet
71787183
download $confhome/windows.xml /tmp/autounattend.xml
71797184
locale=$(get_selected_image_prop 'Default Language')
71807185
use_default_rdp_port=$(is_need_change_rdp_port && echo false || echo true)
7181-
password_base64=$(get_password_windows_administrator_base64)
7186+
71827187
# 7601.24214.180801-1700.win7sp1_ldr_escrow_CLIENT_ULTIMATE_x64FRE_en-us.iso Image Name 为空
71837188
# 将 xml Image Name 的值设为空可以正常安装
71847189
sed -i \
71857190
-e "s|%arch%|$arch|" \
71867191
-e "s|%image_name%|$image_name|" \
71877192
-e "s|%locale%|$locale|" \
7188-
-e "s|%administrator_password%|$password_base64|" \
71897193
-e "s|%use_default_rdp_port%|$use_default_rdp_port|" \
71907194
/tmp/autounattend.xml
71917195
7196+
# 账号密码
7197+
if [ -n "$username" ]; then
7198+
# 普通账号
7199+
password_base64=$(get_password_windows_user_base64)
7200+
xmlstarlet ed -L -N x="urn:schemas-microsoft-com:unattend" \
7201+
-d "//x:AdministratorPassword" \
7202+
/tmp/autounattend.xml
7203+
sed -i \
7204+
-e "s|%enable_administrator%|0|" \
7205+
-e "s|%user_username%|$username|" \
7206+
-e "s|%user_password%|$password_base64|" \
7207+
/tmp/autounattend.xml
7208+
else
7209+
# Administrator
7210+
password_base64=$(get_password_windows_administrator_base64)
7211+
xmlstarlet ed -L -N x="urn:schemas-microsoft-com:unattend" \
7212+
-d "//x:LocalAccounts" \
7213+
/tmp/autounattend.xml
7214+
sed -i \
7215+
-e "s|%enable_administrator%|1|" \
7216+
-e "s|%administrator_password%|$password_base64|" \
7217+
/tmp/autounattend.xml
7218+
fi
7219+
71927220
# 修改应答文件,分区配置
71937221
if is_efi; then
71947222
sed -i "s|%installto_partitionid%|3|" /tmp/autounattend.xml
@@ -7275,12 +7303,12 @@ EOF
72757303
wim_windows_xml=$(get_path_in_correct_case /wim/windows.xml)
72767304
wim_setup_exe=$(get_path_in_correct_case /wim/setup.exe)
72777305
7278-
apk add xmlstarlet
72797306
xmlstarlet ed -d '//comment()' /tmp/autounattend.xml >$wim_autounattend_xml
72807307
unix2dos $wim_autounattend_xml
72817308
info "autounattend.xml"
72827309
# 查看最终文件,并屏蔽密码
72837310
xmlstarlet ed -d '//*[name()="AdministratorPassword" or name()="Password"]' $wim_autounattend_xml | cat -n
7311+
72847312
apk del xmlstarlet
72857313
72867314
# 避免无参数运行 setup.exe 时自动安装

windows.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@
8686
<Order>4</Order>
8787
<Path>powercfg /setactive SCHEME_MIN</Path>
8888
</RunSynchronousCommand>
89-
<!-- 启用 administrator 账户 -->
89+
<!-- 按需启用 administrator 账户 -->
9090
<RunSynchronousCommand wcm:action="add">
9191
<Order>5</Order>
9292
<!-- vista 没有自带 powershell -->
9393
<!-- <Path>powershell "$User = Get-WmiObject Win32_UserAccount | where SID -like *-500; $User.Disabled = $False; $User.Put()"</Path> -->
9494
<!-- win7 此时无法用 wmic useraccount -->
9595
<!-- <Path>wmic useraccount where "sid like '%-500'" set Disabled=false</Path> -->
9696
<!-- https://learn.microsoft.com/archive/technet-wiki/13813.localized-names-for-administrator-account-in-windows -->
97-
<Path>cmd /c "for %a in (Administrator Administrador Administrateur Administratör Администратор Järjestelmänvalvoja Rendszergazda) do (net user %a /active:yes &amp;&amp; exit)"</Path>
97+
<!-- %enable_administrator% 会被 trans.sh 替换成 1 或 0 -->
98+
<Path>cmd /c "if "%enable_administrator%"=="1" for %a in (Administrator Administrador Administrateur Administratör Администратор Järjestelmänvalvoja Rendszergazda) do (net user %a /active:yes &amp;&amp; exit)"</Path>
9899
</RunSynchronousCommand>
99100
<!-- 禁用保留空间 -->
100101
<RunSynchronousCommand wcm:action="add">
@@ -152,6 +153,18 @@
152153
<Value>%administrator_password%</Value>
153154
<PlainText>false</PlainText>
154155
</AdministratorPassword>
156+
<LocalAccounts>
157+
<LocalAccount wcm:action="add">
158+
<Name>%user_username%</Name>
159+
<Password>
160+
<Value>%user_password%</Value>
161+
<PlainText>false</PlainText>
162+
</Password>
163+
<!-- 需要填英文的 Administrators,任何语言都是 -->
164+
<!-- https://learn.microsoft.com/windows-hardware/customize/desktop/unattend/microsoft-windows-shell-setup-useraccounts-localaccounts-localaccount-group -->
165+
<Group>Administrators</Group>
166+
</LocalAccount>
167+
</LocalAccounts>
155168
</UserAccounts>
156169
<OOBE>
157170
<HideEULAPage>true</HideEULAPage>

0 commit comments

Comments
 (0)