From 350f1d38393fe75fd80c1bd396da1c61578ba0fd Mon Sep 17 00:00:00 2001 From: arookieofc <2128194521@qq.com> Date: Fri, 5 Jun 2026 03:37:34 +0800 Subject: [PATCH 1/2] Do not fail uninstall when PowerShell profile cleanup fails --- coreutils.iss | 59 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/coreutils.iss b/coreutils.iss index 6f873cc..e6ceff6 100644 --- a/coreutils.iss +++ b/coreutils.iss @@ -239,32 +239,52 @@ begin g_HasUsablePowerShell := g_HasUsablePowerShell and (g_PowerShellExecutionPolicy <> ''); end; -procedure RunPwshScript(const ExtraParams: String); +function RunPwshScript(const ExtraParams: String; IgnoreFailure: Boolean): Boolean; var Output: TExecOutput; Params, Detail: String; ResultCode: Integer; begin + Result := False; Params := '-NoProfile -NonInteractive -ExecutionPolicy Bypass -File ' + AddQuotes(g_AppDirPath + 'pwsh-install.ps1') + ' ' + ExtraParams; if not ExecAndCaptureOutput('pwsh.exe', Params, '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode, Output) then - Exit; + begin + if IgnoreFailure then + begin + Log('Skipping PowerShell profile update because pwsh.exe could not be executed'); + Exit; + end; + RaiseException('Failed to execute pwsh.exe'); + end; - if ResultCode <> 0 then + if ResultCode = 0 then begin - Detail := ''; - if GetArrayLength(Output.StdErr) > 0 then - Detail := Output.StdErr[0] - else if GetArrayLength(Output.StdOut) > 0 then - Detail := Output.StdOut[0]; + Result := True; + Exit; + end; + + Detail := ''; + if GetArrayLength(Output.StdErr) > 0 then + Detail := Output.StdErr[0] + else if GetArrayLength(Output.StdOut) > 0 then + Detail := Output.StdOut[0]; + if IgnoreFailure then + begin if Detail <> '' then - RaiseException('Failed to update PowerShell profiles: ' + Detail) + Log('Failed to update PowerShell profiles: ' + Detail) else - RaiseException('Failed to update PowerShell profiles'); + Log('Failed to update PowerShell profiles'); + Exit; end; + + if Detail <> '' then + RaiseException('Failed to update PowerShell profiles: ' + Detail) + else + RaiseException('Failed to update PowerShell profiles'); end; -procedure InstallPowerShellProfiles(Scope: Integer); +procedure InstallPowerShellProfiles(Scope: Integer; IgnoreFailure: Boolean); var Params: String; begin @@ -279,7 +299,7 @@ begin Params := Params + ' -CmdDir ' + AddQuotes(RemoveBackslashUnlessRoot(g_AppCmdDirPath)); end; - RunPwshScript(Params); + RunPwshScript(Params, IgnoreFailure); end; // #### Event Handlers #### @@ -363,11 +383,10 @@ begin ModifyPath(WizardIsTaskSelected('addtopath')); if g_HasUsablePowerShell then - Scope := g_PowerShellPage.SelectedValueIndex - else - Scope := PWSH_SCOPE_NONE; - - InstallPowerShellProfiles(Scope); + begin + Scope := g_PowerShellPage.SelectedValueIndex; + InstallPowerShellProfiles(Scope, False); + end; end; end; @@ -376,7 +395,11 @@ begin if CurUninstallStep = usUninstall then begin InitializeGlobals; - InstallPowerShellProfiles(PWSH_SCOPE_NONE); + DetectPowerShell; + if g_HasUsablePowerShell then + InstallPowerShellProfiles(PWSH_SCOPE_NONE, True) + else + Log('Skipping PowerShell profile cleanup because pwsh.exe is unavailable or unsupported'); ModifyPath(False); DelTree(g_AppDirPath, True, True, True); end; From cf54efff3e9de0701bff92c72029c7f5e2f7fdd8 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Wed, 22 Jul 2026 15:30:55 +0200 Subject: [PATCH 2/2] Just use try/except --- coreutils.iss | 63 ++++++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/coreutils.iss b/coreutils.iss index eb7418d..8211ce3 100644 --- a/coreutils.iss +++ b/coreutils.iss @@ -238,52 +238,32 @@ begin g_HasUsablePowerShell := g_HasUsablePowerShell and (g_PowerShellExecutionPolicy <> ''); end; -function RunPwshScript(const ExtraParams: String; IgnoreFailure: Boolean): Boolean; +procedure RunPwshScript(const ExtraParams: String); var Output: TExecOutput; Params, Detail: String; ResultCode: Integer; begin - Result := False; Params := '-NoProfile -NonInteractive -ExecutionPolicy Bypass -File ' + AddQuotes(g_AppDirPath + 'pwsh-install.ps1') + ' ' + ExtraParams; if not ExecAndCaptureOutput('pwsh.exe', Params, '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode, Output) then - begin - if IgnoreFailure then - begin - Log('Skipping PowerShell profile update because pwsh.exe could not be executed'); - Exit; - end; - RaiseException('Failed to execute pwsh.exe'); - end; - - if ResultCode = 0 then - begin - Result := True; Exit; - end; - - Detail := ''; - if GetArrayLength(Output.StdErr) > 0 then - Detail := Output.StdErr[0] - else if GetArrayLength(Output.StdOut) > 0 then - Detail := Output.StdOut[0]; - if IgnoreFailure then + if ResultCode <> 0 then begin + Detail := ''; + if GetArrayLength(Output.StdErr) > 0 then + Detail := Output.StdErr[0] + else if GetArrayLength(Output.StdOut) > 0 then + Detail := Output.StdOut[0]; + if Detail <> '' then - Log('Failed to update PowerShell profiles: ' + Detail) + RaiseException('Failed to update PowerShell profiles: ' + Detail) else - Log('Failed to update PowerShell profiles'); - Exit; + RaiseException('Failed to update PowerShell profiles'); end; - - if Detail <> '' then - RaiseException('Failed to update PowerShell profiles: ' + Detail) - else - RaiseException('Failed to update PowerShell profiles'); end; -procedure InstallPowerShellProfiles(Scope: Integer; IgnoreFailure: Boolean); +procedure InstallPowerShellProfiles(Scope: Integer); var Params: String; begin @@ -298,7 +278,7 @@ begin Params := Params + ' -CmdDir ' + AddQuotes(RemoveBackslashUnlessRoot(g_AppCmdDirPath)); end; - RunPwshScript(Params, IgnoreFailure); + RunPwshScript(Params); end; // #### Event Handlers #### @@ -382,10 +362,11 @@ begin ModifyPath(WizardIsTaskSelected('addtopath')); if g_HasUsablePowerShell then - begin - Scope := g_PowerShellPage.SelectedValueIndex; - InstallPowerShellProfiles(Scope, False); - end; + Scope := g_PowerShellPage.SelectedValueIndex + else + Scope := PWSH_SCOPE_NONE; + + InstallPowerShellProfiles(Scope); end; end; @@ -394,11 +375,11 @@ begin if CurUninstallStep = usUninstall then begin InitializeGlobals; - DetectPowerShell; - if g_HasUsablePowerShell then - InstallPowerShellProfiles(PWSH_SCOPE_NONE, True) - else - Log('Skipping PowerShell profile cleanup because pwsh.exe is unavailable or unsupported'); + try + InstallPowerShellProfiles(PWSH_SCOPE_NONE); + except + MsgBox('An error occurred: ' + GetExceptionMessage, mbError, MB_OK); + end; ModifyPath(False); DelTree(g_AppDirPath, True, True, True); end;