Archive: nsExec and PowerShell error


nsExec and PowerShell error
Hello all,

I'm having trouble doing all the following in unison:
1. Execute a PowerShell script.
2. Have that script execute hidden (i.e., don't show the shell)
3. Redirect the output to the NSIS log window.
4. Have it wait until the script is finished before continuing.

When I execute the following:
nsExec::ExecToLog '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "c:\testscripts\test.ps1"'

I get "Access to the path is denied"

I've tried all sorts of combinations with no success.

Just as an FYI, running:
nsExec::ExecToLog '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"' alone with no parameters will output powershell's banner to the log window and freeze - which is expected because it's waiting for user input which it will never get. This at least shows that it can execute powershell hidden, redirect output and wait for it to complete before continuing. It just doesn't seem to like parameters.

Any help or alternate ideas will be greatly appreciated.


and "c:\testscripts\test.ps1" is also a valid path? try using Process Monitor to figure out where the problem path is


Yes, the path is valid. Also, if I cut and paste the entire line (minus the single quotes) into the Run prompt (off the start menu) it works perfectly.

Just as an asside, I've used nsExec before for a different project.

Thanks.


As a workaround I tried to use the system plugin to call ShellExecuteEx with SEE_MASK_NOASYNC as the mask. This should block and effectively behave like ExecWait with the added bonus being able to control visibility (i.e., hide PowerShell).

Everything seems to work correctly except that the call returns right away and doesn't "wait" for the application to end.

Does the System plugin not honor blocking calls or am I doing something wrong?

Thanks.

jc3


yes, you are doing something wrong. To wait for a _process_ (and only a process, not word documents etc.) you need to add the SEE_MASK_NOCLOSEPROCESSflag and after ShellExecuteEx returns, use WaitForSingleObject(sei.hProcess,INFINITE) and then CloseHandle

BUT, ExecWait should work, it would be better if you used Process Monitor and tried to figure out the actual problem


I've run into the same problem. I have been unable to call powershell with any argument other than /? using nsExec:Exec or nsExec:ExecToLog. All result in "Access to the path is denied" error.

See http://sourceforge.net/tracker/?func...49&atid=373085


Did anyone ever make this work? I would love to use nsExec::ExecToStack in order to check for installed features in WinServer2008 R2


Same with SQLPS
Has anyone come up the reason why nsExec does not execute PowerShell scripts? I am attempting to run a PowerShell script using SQLPS.exe, with the need to hide the command window that pops up. All I receive is the error message, "Access to the path is denied."
Running the same command using ExecWait yields no errors and the script finishes successfully, so I know the paths are correct.


Function myFunction
/* Execute 'sqlps.exe' with the PowerShell script as target.
Wait for sqlps.exe process to end before continuing. */
# Write to log file to indicate what's running:
LogText "${SCRIPTNAME}: Running myScript.ps1"
nsExec::ExecToStack `sqlps ".'$OUTDIR\myScript.ps1' xxxxxxxx"`

# Read return value of top of stack and respond accordingly.
Pop $2 ;Read the value off the top of the stack.
Pop $3 ;Read the error message itself.
# Value of "0" is successful exit for SQLPS. If "0", skip to MoreStuff.
StrCmp $2 "0" MoreStuff 0
LogText "${SCRIPTNAME} FAIL: An error occurred executing the SQL script."
LogText " Return value: $2"
LogText " Return msg : $3"
Quit

MoreStuff:
...
FunctionEnd

Where is sqlps located? How about giving nsExec a full path instead?

Stu


sqlps.exe is located in the Tools directory under the SQL Server install. The directory is included in the PATH on the computer; but I added it to test, just for grins. It failed again, same "Access to the path is denied" error message.

I found a workaround that does what I need, though. Hopefully it will help others.
I wrapped the command in a batch file and called the batch file, instead. I created a batch file using "START /MIN /WAIT" to run the command so that the window would run minimized. Not hidden, but minimized works for my purposes. Contents of batch file:

START /MIN /WAIT sqlps ".'C:\path_to_batch_file\myScript.ps1' xxxxxxxx"
I added the new batch file to the NSIS project and changed my nsExec command to:
nsExec::ExecToStack "$OUTDIR\myScript.bat"
This works without errors. I'm still interested to know why it doesn't work calling the PowerShell script directly from the NSIS script using nsExec. To what path is access denied? Curious.

I solved the "Access to this path is denied" error when using nsExec::ExecToStack to launch a Powershell script. The solution is to add "-inputformat none" to the command line like the following:

nsExec::ExecToStack 'Powershell.exe -inputformat none -noprofile "Get-Process xyz| Stop-Process -Force"'

I got the idea for this fix from the bug report #572313 on Microsoft Connect:


PowerShell.exe can hang if STDIN is redirected
http://connect.microsoft.com/PowerSh...-is-redirected

Also, this bug does not occur with Powershell V3.