Skip to content

Instantly share code, notes, and snippets.

@julianhille
Last active April 28, 2022 20:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianhille/e5f3ee50b11a77c4f77c855ae3d9c443 to your computer and use it in GitHub Desktop.
Save julianhille/e5f3ee50b11a77c4f77c855ae3d9c443 to your computer and use it in GitHub Desktop.
NSIS Powershell execution with win64 support. 32bit NSIS starts a 64bit powershell script
;based on https://nsis.sourceforge.io/PowerShell_support but with x64 support
!include x64.nsh
!ifndef PSEXEC_INCLUDED
!define PSEXEC_INCLUDED
!macro PowerShellExecMacro PSCommand
InitPluginsDir
;Save command in a temp file
Push $R1
FileOpen $R1 $PLUGINSDIR\tempfile.ps1 w
FileWrite $R1 "${PSCommand}"
FileClose $R1
Pop $R1
!insertmacro PowerShellExecFileMacro "$PLUGINSDIR\tempfile.ps1"
!macroend
!macro PowerShellExecLogMacro PSCommand
InitPluginsDir
;Save command in a temp file
Push $R1
FileOpen $R1 $PLUGINSDIR\tempfile.ps1 w
FileWrite $R1 "${PSCommand}"
FileClose $R1
Pop $R1
!insertmacro PowerShellExecFileLogMacro "$PLUGINSDIR\tempfile.ps1"
!macroend
!macro PowerShellExecFileMacro PSFile
!define PSExecID ${__LINE__}
Push $R0
${If} ${RunningX64}
nsExec::ExecToStack '$WINDIR\sysnative\windowspowershell\v1.0\powershell.exe -inputformat none -ExecutionPolicy RemoteSigned -File "${PSFile}" '
${Else}
nsExec::ExecToStack 'powershell -inputformat none -ExecutionPolicy RemoteSigned -File "${PSFile}" '
${EndIf}
Pop $R0 ;return value is first on stack
;script output is second on stack, leave on top of it
IntCmp $R0 0 finish_${PSExecID}
SetErrorLevel 2
finish_${PSExecID}:
Exch ;now $R0 on top of stack, followed by script output
Pop $R0
!undef PSExecID
!macroend
!macro PowerShellExecFileLogMacro PSFile
!define PSExecID ${__LINE__}
Push $R0
${If} ${RunningX64}
nsExec::ExecToLog '$WINDIR\sysnative\windowspowershell\v1.0\powershell.exe -inputformat none -ExecutionPolicy RemoteSigned -File "${PSFile}" '
${Else}
nsExec::ExecToLog 'powershell -inputformat none -ExecutionPolicy RemoteSigned -File "${PSFile}" '
${EndIf}
Pop $R0 ;return value is on stack
IntCmp $R0 0 finish_${PSExecID}
SetErrorLevel 2
finish_${PSExecID}:
Pop $R0
!undef PSExecID
!macroend
!define PowerShellExec `!insertmacro PowerShellExecMacro`
!define PowerShellExecLog `!insertmacro PowerShellExecLogMacro`
!define PowerShellExecFile `!insertmacro PowerShellExecFileMacro`
!define PowerShellExecFileLog `!insertmacro PowerShellExecFileLogMacro`
!endif
@sredna
Copy link

sredna commented May 7, 2021

You probably want to check for IsWow64 , not RunningX64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment