加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

如何在NSIS中调用PowerShell

发布时间:2020-12-14 05:27:51 所属栏目:Windows 来源:网络整理
导读:我试图在NSIS中运行Power Shell.当我运行NSIS脚本时: !include "x64.nsh"Name "nsExec Test"OutFile "nsExecTest.exe"ShowInstDetails showSection "Output to variable" nsExec::ExecToStack 'powershell -Command " {Import-Module }" ServerManager' Pop
我试图在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 "$pluginsdirNSISTemp"
File script.ps1
nsExec::ExecToStack 'powershell -inputformat none -ExecutionPolicy RemoteSigned -File "$pluginsdirNSISTempscript.ps1"  '

使用宏I’ve written here,只需${PowerShellExec}“echo’hello powershell’”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读