From 98c6332a5b8e53b4ac0760b647850a77a7503fbc Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Thu, 12 Sep 2024 19:55:01 -0700 Subject: [PATCH] Gracefully abort if browser is not installed --- ArdisUtilities/ArdisUtilities.psm1 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ArdisUtilities/ArdisUtilities.psm1 b/ArdisUtilities/ArdisUtilities.psm1 index 3d08e8b..e9b8a73 100644 --- a/ArdisUtilities/ArdisUtilities.psm1 +++ b/ArdisUtilities/ArdisUtilities.psm1 @@ -1008,7 +1008,7 @@ function Open-TemporaryBrowser { # Wrapping in try/catch is required because -ErrorAction is ignored for terminating errors foreach ($key in @('Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\Brave\shell\open\command', 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\brave.exe')) { try { - $bravePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' + $bravePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' -ErrorAction SilentlyContinue } catch {} } @@ -1016,7 +1016,7 @@ function Open-TemporaryBrowser { foreach ($key in @('Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\Google Chrome\shell\open\command', 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe')) { try { - $chromePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' + $chromePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' -ErrorAction SilentlyContinue } catch {} } @@ -1024,7 +1024,7 @@ function Open-TemporaryBrowser { foreach ($key in @('Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\Microsoft Edge\shell\open\command', 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe')) { try { - $edgePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' + $edgePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' -ErrorAction SilentlyContinue } catch {} } @@ -1037,6 +1037,11 @@ function Open-TemporaryBrowser { 'Chrome' { $execPath = $chromePath } 'Edge' { $execPath = $edgePath } } + $execPath = $execPath.Replace("`"", '') # Registry values have leading and trailing quote literals in the string + + if (-not(Test-Path -LiteralPath $execPath -PathType Leaf)) { + throw "Could not find browser `"$Browser`" at path `"$execPath`", aborting." + } $temp = Join-Path $Env:Temp $(New-Guid) -- 2.47.3