powershellvisual-studio-codensis

I want to execute a Windows command, obtain its output, and display it in a text box using NSIS


nsExec::ExecToStack 'powershell -NoProfile -Command "$(& {$Args[0]})" "where code"'
Pop $0 ;
${NSD_SetText} $LocationTextbox "$0"

When running this I'm getting 0 as the output. Am I missing something here ?


Solution

  • I dont think nsExec::ExecToStack is behaving the way your expecting. Taken from the docs:

    Return value If nsExec is unable to execute the process, it will return "error" on the top of the stack, if the process timed out it will return "timeout", else it will return the return code from the executed process.

    So the value 0 means "Success" - ie the powershell process started and returned a success code of 0.

    Looking at the 3rd example from the ExecToStack docs, it seems stdout is next on the stack:

    nsExec::ExecToStack [/OEM] [/TIMEOUT=x] path
    Pop $ExitCode
    Pop $StdOutText
    

    $StdOutText will include everything outputted by Powershell (you might want to add /nologo to your powershell command to remove some of the garbage) - so that might be an option depending on what your script does.

    Another option would be to write the value to a file from Powershell with Set-Content, then read from that file with FileOpen/FileRead/FileClose in NSIS.