Skip to content

Commit 7de021c

Browse files
committed
fix: harden SID guard error handling and add comments
1 parent 5b7d62c commit 7de021c

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

src/playbook/Executables/AtlasModules/Scripts/newUsers.ps1

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# Exit early if this SID was already set up — prevents re-running after an accidental profile reset
1+
# Guard against re-running after a profile reset. Windows 24H2/25H2 can recreate a user profile
2+
# from the Default template after sleep/wake, which causes RunOnce to fire this script again.
3+
# We track per-SID completion in HKLM (survives profile resets, unlike HKCU) and exit early if
4+
# this user was already set up. -ErrorAction Stop ensures real errors are not silently swallowed.
25
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
3-
if ((Get-ItemProperty 'HKLM:\SOFTWARE\AtlasOS\UserSetup' -Name $sid -EA SilentlyContinue).$sid -eq 1) { exit }
6+
try {
7+
if ((Get-ItemProperty 'HKLM:\SOFTWARE\AtlasOS\UserSetup' -Name $sid -ErrorAction Stop).$sid -eq 1) { exit }
8+
} catch {
9+
# Key or property does not exist yet - this is the first run, continue normally
10+
}
411

512
$windir = [Environment]::GetFolderPath('Windows')
613
& "$windir\AtlasModules\initPowerShell.ps1"
@@ -77,9 +84,17 @@ if ([string]::IsNullOrWhiteSpace($browser)) {
7784
& "$atlasModules\Scripts\taskbarPins.ps1" $browser
7885
& reg.exe add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Search" /v "SearchboxTaskbarMode" /t REG_DWORD /d 1 /f *> $null
7986

80-
# Mark this SID as set up so the guard above triggers on any future re-run
81-
New-Item 'HKLM:\SOFTWARE\AtlasOS\UserSetup' -Force | Out-Null
82-
Set-ItemProperty 'HKLM:\SOFTWARE\AtlasOS\UserSetup' -Name $sid -Value 1 -Type DWord -Force
87+
# Write the per-SID completion marker so the guard at the top of this script triggers on any
88+
# future re-run. The key is created if it does not exist yet. If writing fails for any reason
89+
# (e.g. permissions), a warning is shown so the failure is visible rather than silent.
90+
try {
91+
if (-not (Test-Path 'HKLM:\SOFTWARE\AtlasOS\UserSetup')) {
92+
New-Item 'HKLM:\SOFTWARE\AtlasOS\UserSetup' -Force | Out-Null
93+
}
94+
Set-ItemProperty 'HKLM:\SOFTWARE\AtlasOS\UserSetup' -Name $sid -Value 1 -Type DWord -Force
95+
} catch {
96+
Write-Warning "Failed to write setup marker for SID '$sid': $($_.Exception.Message)"
97+
}
8398

8499
# Leave
85100
Start-Sleep 5

0 commit comments

Comments
 (0)