Add functions for registering RunOnce for new users

This commit is contained in:
Manuel Thalmann 2023-06-23 20:08:31 +02:00
parent 4060eb61d8
commit 65042d4d4c

View file

@ -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;
}
}