如果其他人遇到这个问题,这里有一个解决方案,可以让您“代替”[RUN] 部分中的任何语句运行 pascal 脚本过程。
我需要将 powershell 脚本的执行移至 [CODE] 部分,因为 [RUN] 部分不提供对外部 cmd 程序的退出代码作出反应的方法。
由于我有一堆子安装程序,我需要在我的设置中按特定顺序执行,因此可以选择在[RUN] 部分中控制 pascal 脚本的位置,而不仅仅是在输入时[RUN] 部分设置步骤。
它的工作原理是使用“虚拟”程序(如ping)作为[RUN] 语句的文件名,然后使用内置BeforeInstall 参数调用包含实际执行逻辑的pascal 脚本过程。
[运行]部分
Filename: "ping"; BeforeInstall: RunSQLSetupScript; Components: "thirds\db"; StatusMsg: "Installing SQL Server ..." ; Flags: runhidden
包含实际执行逻辑的 Pascal 脚本过程
(请注意,{tmp}\{#SQLServerInstallScript} 将是此示例中脚本的实际路径,因为通常建议避免使用硬编码路径并改用常量+临时目录。)
// runs the SQL setup script and terminates the setup process, if the script returns an exit code that indicates an error
procedure RunSQLSetupScript();
var
runSuccess: Boolean;
retVar: Integer;
msgText: String;
begin
runSuccess := ShellExec('', 'powershell.exe', '-NoProfile -File ' + ExpandConstant('{tmp}\{#SQLServerInstallScript}'), '', SW_SHOW, ewWaitUntilTerminated, retVar);
// the external script will return an exit code > 0 if an error occurred
if (runSuccess = False) or (retVar > 0) then
begin
msgText := 'SQL Server setup script returned error code ' + IntToStr(retVar) + ', indicating an unsuccessful installation of SQL Server. Setup will now terminate.';
MsgBox(msgText, mbCriticalError, MB_OK);
// => further handle error case here, like cancelling the running setup or log the issue etc.
end;
end;