如何在 NSIS 中调用 PowerShell [英] How to call PowerShell in NSIS

查看:61
本文介绍了如何在 NSIS 中调用 PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 NSIS 中运行 PowerShell.当我运行 NSIS 脚本时:

I am trying to run PowerShell in NSIS. when I run the NSIS script:

!include "x64.nsh"

Name "nsExec Test"

OutFile "nsExecTest.exe"

ShowInstDetails show

Section "Output to variable"

    nsExec::ExecToStack 'powershell -Command "& {Import-Module }" ServerManager'
    Pop $0 # return value/error/timeout
    Pop $1 # printed text, up to ${NSIS_MAX_STRLEN}
    DetailPrint '"ImportModules" printed: $1'
    DetailPrint "       Return value: $0"

    nsExec::ExecToStack 'powershell -Command "& {Get-WindowsFeature}" Desktop-Experience'
    Pop $0 # return value/error/timeout
    Pop $1 # printed text, up to ${NSIS_MAX_STRLEN}
    DetailPrint '"GetWindowsFeature" printed: $1'
    DetailPrint "       Return value: $0"
SectionEnd

当它执行到Import-Module ServerManager"时,PowerShell被启动(可以在TaskManager进程中看到).但是 nsExecTest.exe 挂了.

When it executed to "Import-Module ServerManager", The PowerShell was started up(It can be seen in TaskManager processes). But nsExecTest.exe was hanging over.

我在谷歌上搜索了这个问题,并找到了 Java 的解决方法.https://blogs.oracle.com/vaibhav/entry/not_as_easy_as_we

I have googled this problem, and found a workaround for Java. https://blogs.oracle.com/vaibhav/entry/not_as_easy_as_we

有人对 NSIS 中的这个问题有想法吗?

Anyone has ideas for this problem in NSIS?

更新:我简化了我的测试脚本.

Updated: I simplify my test script.

!include "x64.nsh"

Name "nsExec Test"
OutFile "nsExecTest.exe"
ShowInstDetails show

Section "Output to variable"
${If} ${RunningX64}
    ${DisableX64FSRedirection}

    nsExec::ExecToStack 'powershell.exe "& "Import-Module ServerManager"'
    Pop $0 # return value/error/timeout
    Pop $1 # printed text, up to ${NSIS_MAX_STRLEN}
    DetailPrint '"ImportModules" printed: $1'
    DetailPrint " Return value: $0"
    DetailPrint ""

    ${EnableX64FSRedirection}
${Else}
${EndIf}
SectionEnd

推荐答案

据我所知,AaronLS 的回答对我不起作用,我找到了两个解决此问题的方法,与 PowerShell v2 中的一个错误有关 此处报告(但从未修复):

As far as I found out, AaronLS's answer didn't work for me, I found two workarounds for this problem, related to a bug in PowerShell v2 reported here (but never fixed):

  • 升级到 PowerShell v3
  • 从 NSIS 中的文件运行脚本,并指定 inputformat none.出于一个非常奇怪的原因,您必须在 nsExec::ExecToStack 的最后一个引号前留 两个 空格:

  • Upgrade to PowerShell v3
  • Run the script from a file in NSIS, and specify inputformat none. For a very strange reason you have to leave two spaces before the last quote of nsExec::ExecToStack:

SetOutPath "$pluginsdir\NSISTemp"
File script.ps1
nsExec::ExecToStack 'powershell -inputformat none -ExecutionPolicy RemoteSigned -File "$pluginsdir\NSISTemp\script.ps1"  '

使用宏我写在这里,这只是${PowerShellExec} "echo 'hello powershell'".

这篇关于如何在 NSIS 中调用 PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆