Allow registering reboot for the default user

This commit is contained in:
Manuel Thalmann 2024-08-09 14:44:57 +02:00
parent ec7fc8dd6e
commit 29e2703162

View file

@ -1,10 +1,12 @@
using namespace Microsoft.Win32; using namespace Microsoft.Win32;
$null = New-Module { $null = New-Module {
. "$PSScriptRoot/../Scripts/Registry.ps1";
. "$PSScriptRoot/../../Common/Scripts/Config.ps1"; . "$PSScriptRoot/../../Common/Scripts/Config.ps1";
. "$PSScriptRoot/../../Common/Scripts/Scripting.ps1"; . "$PSScriptRoot/../../Common/Scripts/Scripting.ps1";
[RegistryKey] $key = $null; [RegistryKey] $key = $null;
$runOncePath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"; $runOncePath = "Software\Microsoft\Windows\CurrentVersion\RunOnce";
$systemRunOncePath = "HKLM:\$runOncePath";
$logonPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" $logonPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
$runOnceName = "PortValhalla"; $runOnceName = "PortValhalla";
$autologinOption = "AutoAdminLogon"; $autologinOption = "AutoAdminLogon";
@ -17,7 +19,23 @@ $null = New-Module {
Gets the reghistry key containing the `RunOnce` commands. Gets the reghistry key containing the `RunOnce` commands.
#> #>
function Get-RunOnceKey { function Get-RunOnceKey {
Get-Item $runOncePath; param(
[RegistryKey] $UserKey
)
[string] $path = $null;
if ($UserKey) {
$path = "$($UserKey.PSPath)\$runOncePath";
} else {
$path = $systemRunOncePath;
}
if (-not (Test-Path $path)) {
New-Item $path;
} else {
Get-Item $path;
}
} }
<# <#
@ -29,10 +47,23 @@ $null = New-Module {
#> #>
function Register-Setup { function Register-Setup {
param( param(
[Parameter(ParameterSetName="System")]
[switch] $System,
[Parameter(ParameterSetName="User",Mandatory)]
[switch] $User,
[Parameter(Mandatory)]
[RegistryKey] $UserKey [RegistryKey] $UserKey
) )
if ($User.IsPresent) {
if (-not $UserKey) {
$UserKey = Get-Item "HKCU:\";
}
$key = Get-RunOnceKey $UserKey; $key = Get-RunOnceKey $UserKey;
} else {
$key = Get-RunOnceKey;
}
Set-ItemProperty -Path $key.PSPath -Name $runOnceName -Type "ExpandString" -Value ( Set-ItemProperty -Path $key.PSPath -Name $runOnceName -Type "ExpandString" -Value (
"pwsh -Command " + "pwsh -Command " +
@ -106,7 +137,24 @@ $null = New-Module {
Reboots the machine intermediately and restarts the setup after the next login. Reboots the machine intermediately and restarts the setup after the next login.
#> #>
function Restart-Intermediate { function Restart-Intermediate {
Register-Setup; param(
[switch] $DefaultUser
)
$register = { param($Key) Register-Setup -UserKey $Key; };
if ($DefaultUser) {
Edit-DefaultUserKey {
param(
[RegistryKey] $Key
)
& $register -Key $Key;
}
}
& $register;
Read-Host "Press enter to reboot";
Restart-Computer -Force; Restart-Computer -Force;
} }
} }