我想执行 Windows 命令,获取其输出,并使用 NSIS 将其显示在文本框中

问题描述 投票:0回答:1
nsExec::ExecToStack 'powershell -NoProfile -Command "$(& {$Args[0]})" "where code"'
Pop $0 ;
${NSD_SetText} $LocationTextbox "$0"

运行此程序时,我得到 0 作为输出。我在这里错过了什么吗?

powershell visual-studio-code nsis
1个回答
1
投票

我认为

nsExec::ExecToStack
的行为并不符合您的预期。摘自文档

返回值 如果 nsExec 无法执行该进程,它将在堆栈顶部返回“error”,如果进程超时,它将返回“timeout”,否则它将返回已执行进程的返回码。

因此值 0 表示“成功” - 即 powershell 进程启动并返回成功代码 0

查看 ExecToStack 文档中的第三个示例,似乎 stdout 是堆栈上的下一个:

nsExec::ExecToStack [/OEM] [/TIMEOUT=x] path
Pop $ExitCode
Pop $StdOutText

$StdOutText 将包含 Powershell 输出的所有内容(您可能需要将

/nologo
添加到 powershell 命令中以删除一些垃圾) - 因此这可能是一个选项,具体取决于您的脚本的功能。

另一种选择是使用

Set-Content
从 Powershell 将值写入文件,然后 在 NSIS 中使用
FileOpen/FileRead/FileClose
从该文件读取。

© www.soinside.com 2019 - 2024. All rights reserved.