From c90d2f7cdd2788c5c8a6fa0a6e6b6622e3ed095c Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 28 Jun 2023 13:40:56 +0200 Subject: [PATCH] Clean up handles during reg key handling --- scripts/Windows/Scripts/Context.ps1 | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/Windows/Scripts/Context.ps1 b/scripts/Windows/Scripts/Context.ps1 index eba59b3b..d2d0648d 100644 --- a/scripts/Windows/Scripts/Context.ps1 +++ b/scripts/Windows/Scripts/Context.ps1 @@ -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() {