【问题标题】:How to call PowerShell in NSIS如何在 NSIS 中调用 PowerShell
【发布时间】:2012-11-15 03:56:46
【问题描述】:

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

!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 悬而未决。

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

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

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

!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

【问题讨论】:

    标签: powershell nsis windows-server-2008-r2


    【解决方案1】:

    据我所知,AaronLS 的回答对我不起作用,我发现了两个解决这个问题的方法,与 PowerShell v2 reported here 中的一个错误有关(但从未修复):

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

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

    使用宏I've written here,只需${PowerShellExec} "echo 'hello powershell'"

    【讨论】:

    • 我放弃了PowerShell,最后改用servercmd(已经被PowerShell代替了,但是在Win2K8下还是可以用的)。
    【解决方案2】:

    自从我使用 NSIS 以来已经有一段时间了,所以我只是根据我在其他地方看到的语法猜测:

    nsExec::ExecToStack 'powershell.exe "& "Import-Module ServerManager"'
    

    取出第二个命令,然后用第一个测试并首先让它工作,然后你可以确定你的第一个命令是正确的。

    还可以尝试将< NUL 添加到您和/或我的命令行的末尾:

    nsExec::ExecToStack 'powershell -Command "& {Import-Module }" ServerManager < NUL'
    nsExec::ExecToStack 'powershell.exe "& "Import-Module ServerManager" < NUL'
    

    我不确定它是否需要在双引号内。如果它正在等待您完成输入,就像您以交互方式运行它一样,它可能会挂起:

    http://epeleg.blogspot.com/2010/06/solution-to-powershell-never-exists.html

    【讨论】:

    • 感谢您的回复,AaronLS。
    • 我像这样运行脚本:nsExec::ExecToStack 'powershell.exe "&amp; "Import-Module ServerManager"'。它打印错误“访问路径被拒绝”。我运行nsExec::ExecToStack 'powershell.exe "&amp; "Import-Module ServerManager" &lt; NUL',同样的错误。你能给我一些建议吗?谢谢。
    • 您是否也尝试过此命令(您的原始命令添加了 nsExec::ExecToStack 'powershell -Command "& {Import-Module }" ServerManager < NUL'
    • 是的,我也用 Access to the path is denied. + CategoryInfo : NotSpecified: (:) [], UnauthorizedAccessException + FullyQualifiedErrorId : ConsoleHost.ReportException
    • 我能建议的唯一另一件事是首先尝试让 powershell 命令从批处理文件中工作。这可能只是用 connect.microsoft.com/PowerShell/feedback/details/572313/…
    猜你喜欢
    • 2018-06-21
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多