diff --git a/scripts/Windows/Scripts/Context.ps1 b/scripts/Windows/Scripts/Context.ps1 index 73f80c18..dbf90dc0 100644 --- a/scripts/Windows/Scripts/Context.ps1 +++ b/scripts/Windows/Scripts/Context.ps1 @@ -4,6 +4,7 @@ class Context { [string]$BackupName; [string]$UserName; [string]$AdminName = "Admin"; + [string]$RunOnceName = "PortValhalla"; [string] BackupRoot() { if (-not $this.RootDir) { @@ -63,9 +64,36 @@ class Context { return $tempDir; } + [void] ProcessDefaultUserKey([System.Action[Microsoft.Win32.RegistryKey]] $action) { + $root = "HKLM:\DefaultUser"; + $hivePath = "$env:SystemRoot\Profiles\Default User\NTUSER.dat" + $null = & reg load $root $hivePath; + $action.Invoke((Get-Item $root)); + } + + [Microsoft.Win32.RegistryKey] GetRunOnceKey([Microsoft.Win32.RegistryKey] $userKey) { + if (-not $userKey) { + $userKey = Get-Item "HKCU:\"; + } + + return Get-Item $userKey "$userKey\Software\Microsoft\Windows\CurrentVersion\RunOnce"; + } + + [void] RegisterReboot([Microsoft.Win32.RegistryKey] $userKey) { + $null = New-ItemProperty -Path $this.GetRunOnceKey($userKey) -Name $this.RunOnceName -Value "pwsh `"$($this.EntryPoint)`"" -PropertyType ExpandString; + } + + [void] RegisterNewUserReboot() { + $this.ProcessDefaultUserKey({ param ($root) $this.RegisterReboot($root); }); + } + + [void] DeregisterNewUserReboot() { + $this.ProcessDefaultUserKey({ param ($root) Remove-ItemProperty $this.GetRunOnceKey($root) -Name $this.RunOnceName }); + } + [void] Reboot() { Write-Host "Restarting Computer..."; - $null = New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name "PortValhalla" -Value "pwsh `"$($this.EntryPoint)`"" -PropertyType ExpandString; + $this.RegisterReboot(); Restart-Computer; } }