From ae7e50b370db4646b068425b25d50537b4c7a8e1 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 25 May 2021 00:26:11 -0700 Subject: [PATCH] Test registry path before invoking Get-ChildItem (#95) --- installers/win-setup-template.ps1 | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/installers/win-setup-template.ps1 b/installers/win-setup-template.ps1 index 9b6be66..f2ac2cd 100644 --- a/installers/win-setup-template.ps1 +++ b/installers/win-setup-template.ps1 @@ -28,16 +28,20 @@ function Remove-RegistryEntries { $versionFilter = Get-RegistryVersionFilter -Architecture $Architecture -MajorVersion $MajorVersion -MinorVersion $MinorVersion $regPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products" - $regKeys = Get-ChildItem -Path Registry::$regPath -Recurse | Where-Object Property -Ccontains DisplayName - foreach ($key in $regKeys) { - if ($key.getValue("DisplayName") -match $versionFilter) { - Remove-Item -Path $key.PSParentPath -Recurse -Force -Verbose + if (Test-Path -Path Registry::$regPath) { + $regKeys = Get-ChildItem -Path Registry::$regPath -Recurse | Where-Object Property -Ccontains DisplayName + foreach ($key in $regKeys) { + if ($key.getValue("DisplayName") -match $versionFilter) { + Remove-Item -Path $key.PSParentPath -Recurse -Force -Verbose + } } } $regPath = "HKEY_CLASSES_ROOT\Installer\Products" - Get-ChildItem -Path Registry::$regPath | Where-Object { $_.GetValue("ProductName") -match $versionFilter } | ForEach-Object { - Remove-Item Registry::$_ -Recurse -Force -Verbose + if (Test-Path -Path Registry::$regPath) { + Get-ChildItem -Path Registry::$regPath | Where-Object { $_.GetValue("ProductName") -match $versionFilter } | ForEach-Object { + Remove-Item Registry::$_ -Recurse -Force -Verbose + } } $uninstallRegistrySections = @(