From 95981f4d3c538f540fb0d61b86ac1c26361ab823 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 23 Jun 2023 20:08:31 +0200 Subject: [PATCH] Add functions for registering RunOnce for new users --- scripts/Windows/Scripts/Context.ps1 | 30 ++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) 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; } }