07-apply-ui-ime-default-profile:除了 Default profile 也套到所有已載入的真人 user hive
之前只寫 Default profile NTUSER.DAT,新使用者首次登入會繼承—— 但若腳本是在 User 已登入後才被重跑(例如 deploy 後想改設定), Default 改動不會反映到既存的 User profile,肉眼看不到變化。 把套設定的邏輯抽成 Apply-UiImeSettings function,先寫 Default、 再用 Win32_UserProfile 列出所有非系統 profile,凡是 hive 已載入 (HKEY_USERS\<SID> 存在)的就一併寫進去。production 流程下 User 還沒建立、所以只有 Admin 拿到(無關緊要 Admin 會被隱藏); re-run 時 User hive 已載入則直接生效。
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
. "$PSScriptRoot\_common.ps1"
|
. "$PSScriptRoot\_common.ps1"
|
||||||
|
|
||||||
# 把 UI / IME 預設套到 Default profile,新使用者首次登入會繼承
|
# Apply UI/IME settings to (a) Default profile NTUSER.DAT (so brand-new users
|
||||||
Section 'apply-ui-ime-default-profile' {
|
# inherit) and (b) every currently-loaded real-user hive (so re-runs against
|
||||||
$hivePath = 'C:\Users\Default\NTUSER.DAT'
|
# an already-logged-in box still take effect immediately on those users).
|
||||||
$mountKey = 'HKU\SetupDefault'
|
function Apply-UiImeSettings {
|
||||||
reg.exe load $mountKey $hivePath | Out-Null
|
param([string]$base) # e.g. "Registry::HKU\SetupDefault" or "Registry::HKEY_USERS\<SID>"
|
||||||
try {
|
|
||||||
$base = "Registry::$mountKey"
|
|
||||||
|
|
||||||
$adv = "$base\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
|
$adv = "$base\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
|
||||||
Set-RegValue $adv 'HideFileExt' 0
|
Set-RegValue $adv 'HideFileExt' 0
|
||||||
@@ -23,9 +21,9 @@ Section 'apply-ui-ime-default-profile' {
|
|||||||
$search = "$base\Software\Microsoft\Windows\CurrentVersion\Search"
|
$search = "$base\Software\Microsoft\Windows\CurrentVersion\Search"
|
||||||
Set-RegValue $search 'SearchboxTaskbarMode' 0
|
Set-RegValue $search 'SearchboxTaskbarMode' 0
|
||||||
|
|
||||||
# SearchSettings 這個 key 在某些 build 上 reg.exe import/add 會被「存取被拒」
|
# SearchSettings 在某些 build 上 reg.exe 會「存取被拒」(Win11Debloat 的
|
||||||
# (Win11Debloat 的 Disable_Search_History.reg 跟 Disable_Search_Highlights.reg 都會撞),
|
# Disable_Search_History.reg 跟 Disable_Search_Highlights.reg 都會撞),
|
||||||
# 但 PowerShell 的 .NET API 寫得進去——所以這裡一次處理掉,Win11Debloat config 已把這兩項移除
|
# 但 PowerShell .NET API 寫得進去——這裡一次處理掉,Win11Debloat config 已把這兩項移除
|
||||||
$searchSettings = "$base\Software\Microsoft\Windows\CurrentVersion\SearchSettings"
|
$searchSettings = "$base\Software\Microsoft\Windows\CurrentVersion\SearchSettings"
|
||||||
Set-RegValue $searchSettings 'IsDeviceSearchHistoryEnabled' 0
|
Set-RegValue $searchSettings 'IsDeviceSearchHistoryEnabled' 0
|
||||||
Set-RegValue $searchSettings 'IsDynamicSearchBoxEnabled' 0
|
Set-RegValue $searchSettings 'IsDynamicSearchBoxEnabled' 0
|
||||||
@@ -51,9 +49,33 @@ Section 'apply-ui-ime-default-profile' {
|
|||||||
Set-RegValue $tg 'Hotkey' '3' 'String'
|
Set-RegValue $tg 'Hotkey' '3' 'String'
|
||||||
Set-RegValue $tg 'Language Hotkey' '3' 'String'
|
Set-RegValue $tg 'Language Hotkey' '3' 'String'
|
||||||
Set-RegValue $tg 'Layout Hotkey' '3' 'String'
|
Set-RegValue $tg 'Layout Hotkey' '3' 'String'
|
||||||
|
}
|
||||||
|
|
||||||
|
Section 'apply-ui-ime-default-profile' {
|
||||||
|
# (a) Default profile(新使用者首次登入會繼承)
|
||||||
|
$hivePath = 'C:\Users\Default\NTUSER.DAT'
|
||||||
|
$mountKey = 'HKU\SetupDefault'
|
||||||
|
reg.exe load $mountKey $hivePath | Out-Null
|
||||||
|
try {
|
||||||
|
Apply-UiImeSettings "Registry::$mountKey"
|
||||||
|
Log "Applied to Default profile"
|
||||||
} finally {
|
} finally {
|
||||||
[GC]::Collect()
|
[GC]::Collect()
|
||||||
Start-Sleep 1
|
Start-Sleep 1
|
||||||
reg.exe unload $mountKey | Out-Null
|
reg.exe unload $mountKey | Out-Null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# (b) 已載入的真人 user hive(HKU\<SID>,跳過 .DEFAULT / 系統 SID)
|
||||||
|
# 取自 Win32_UserProfile,過濾 Special;只在 hive 已載入時寫
|
||||||
|
$profiles = Get-CimInstance Win32_UserProfile -ErrorAction SilentlyContinue |
|
||||||
|
Where-Object { -not $_.Special }
|
||||||
|
foreach ($p in $profiles) {
|
||||||
|
$sid = $p.SID
|
||||||
|
if (Test-Path "Registry::HKEY_USERS\$sid") {
|
||||||
|
Apply-UiImeSettings "Registry::HKEY_USERS\$sid"
|
||||||
|
Log ("Applied to live user hive {0} ({1})" -f $sid, (Split-Path $p.LocalPath -Leaf))
|
||||||
|
} else {
|
||||||
|
Log ("Skip {0} ({1}) - hive not loaded" -f $sid, (Split-Path $p.LocalPath -Leaf))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user