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;