如何在NSIS中调用PowerShell

前端之家收集整理的这篇文章主要介绍了如何在NSIS中调用PowerShell前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在NSIS中运行Power Shell.当我运行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却被挂了.

我已经google了这个问题,并找到了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

解决方法

据我所知,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’”.

猜你在找的Windows相关文章