Add functions for registering RunOnce for new users
This commit is contained in:
parent
a985ac56d3
commit
95981f4d3c
1 changed files with 29 additions and 1 deletions
|
@ -4,6 +4,7 @@ class Context {
|
||||||
[string]$BackupName;
|
[string]$BackupName;
|
||||||
[string]$UserName;
|
[string]$UserName;
|
||||||
[string]$AdminName = "Admin";
|
[string]$AdminName = "Admin";
|
||||||
|
[string]$RunOnceName = "PortValhalla";
|
||||||
[string] BackupRoot() {
|
[string] BackupRoot() {
|
||||||
if (-not $this.RootDir)
|
if (-not $this.RootDir)
|
||||||
{
|
{
|
||||||
|
@ -63,9 +64,36 @@ class Context {
|
||||||
return $tempDir;
|
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() {
|
[void] Reboot() {
|
||||||
Write-Host "Restarting Computer...";
|
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;
|
Restart-Computer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue