Clean up handles during reg key handling

This commit is contained in:
Manuel Thalmann 2023-06-28 13:40:56 +02:00
parent 16eb9a01b0
commit 50b8511e9e

View file

@ -65,12 +65,15 @@ class Context {
}
[void] ProcessDefaultUserKey([System.Action[Microsoft.Win32.RegistryKey]] $action) {
$root = "HKLM:\DefaultUser";
$regRoot = $root.Replace(":", "");
$rootPath = "HKLM:\DefaultUser";
$regRootPath = $rootPath.Replace(":", "");
$hivePath = "$env:SystemDrive\Users\Default\NTUSER.dat"
$null = & reg load $regRoot $hivePath;
$action.Invoke((Get-Item $root));
reg unload $regRoot;
$null = & reg load $regRootPath $hivePath;
$root = Get-Item $rootPath;
$action.Invoke($root);
$root.Handle.Close();
[System.GC]::Collect();
& reg unload $regRootPath;
}
[void] ProcessLogonKey([System.Action[Microsoft.Win32.RegistryKey]] $action) {
@ -104,7 +107,9 @@ class Context {
}
[void] RegisterReboot([Microsoft.Win32.RegistryKey] $userKey) {
$null = Set-ItemProperty -Path $this.GetRunOnceKey($userKey).PSPath -Name $this.RunOnceName -Value "pwsh `"$($this.EntryPoint)`"" -Type "ExpandString";
$runOnceKey = $this.GetRunOnceKey($userKey);
$null = Set-ItemProperty -Path $runOnceKey.PSPath -Name $this.RunOnceName -Value "pwsh `"$($this.EntryPoint)`"" -Type "ExpandString";
$runOnceKey.Handle.Close();
}
[void] RegisterNewUserReboot() {