【问题标题】:How to cancel NSIS Installer after executed powershell script was canceled?取消执行的powershell脚本后如何取消NSIS安装程序?
【发布时间】:2025-12-31 14:30:06
【问题描述】:

对于我的软件套件,我使用 NSIS 创建了一个安装程序。此安装程序包括一个带有 GUI 的 powershellscript。我想在用户单击 powershell gui 中的取消按钮后取消安装过程。

脚本创建一个带有列表框的 powershell gui。列表框项将写入 txt 文件。有两个按钮:一个用于确定 - 将项目写入文件,另一个按钮是取消按钮。单击确定按钮后,将打开第二个表单。

powershellscript中的一些代码sn-ps:

$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

if('Ok' -eq $form.ShowDialog())
 {
   $msg = "Item copied to txt-file"
   [System.Windows.Forms.MessageBox]::Show($msg,"Confirmation",0)
 }

通过这条指令,我在 NSIS 中调用 PS-Script:

ExecWait "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File nameofscript.ps1 -FFFeatureOff"

【问题讨论】:

  • 您需要展示更多的 PowerShell 脚本,而不仅仅是在线。例如,它会检查$form.ShowDialog() 的返回值吗?

标签: powershell nsis


【解决方案1】:

如果您在取消时从 PowerShell 返回退出代码,您可以从 NSIS 脚本中处理它:

Powershell:

if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::Cancel)
{
    exit 1
}

国家安全局:

ClearErrors
ExecWait "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File nameofscript.ps1 -FFFeatureOff"
IfErrors 0 noError
; Handle error here
noError:

另请参阅:how to get the exit code of other application in nsis

【讨论】: