Old 13th April 2009, 21:23   #1
JeronimoColon
Junior Member
 
Join Date: Sep 2004
Location: New York City
Posts: 39
Unhappy 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.
JeronimoColon is offline   Reply With Quote
Old 13th April 2009, 23:09   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
and "c:\testscripts\test.ps1" is also a valid path? try using Process Monitor to figure out where the problem path is

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 14th April 2009, 13:09   #3
JeronimoColon
Junior Member
 
Join Date: Sep 2004
Location: New York City
Posts: 39
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.
JeronimoColon is offline   Reply With Quote
Old 15th April 2009, 10:32   #4
JeronimoColon
Junior Member
 
Join Date: Sep 2004
Location: New York City
Posts: 39
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
JeronimoColon is offline   Reply With Quote
Old 15th April 2009, 11:54   #5
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
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

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 10th July 2009, 21:41   #6
DrStevens
Junior Member
 
Join Date: Jul 2009
Posts: 1
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
DrStevens is offline   Reply With Quote
Old 27th June 2011, 17:04   #7
ripper17
Junior Member
 
Join Date: May 2006
Location: Germany
Posts: 6
Send a message via ICQ to ripper17
Did anyone ever make this work? I would love to use nsExec::ExecToStack in order to check for installed features in WinServer2008 R2
ripper17 is offline   Reply With Quote
Old 26th March 2012, 19:46   #8
FlappingCrane
Junior Member
 
Join Date: Nov 2008
Posts: 2
Send a message via ICQ to FlappingCrane Send a message via AIM to FlappingCrane Send a message via Yahoo to FlappingCrane
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.

code:

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

FlappingCrane is offline   Reply With Quote
Old 26th March 2012, 21:57   #9
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Where is sqlps located? How about giving nsExec a full path instead?

Stu
Afrow UK is offline   Reply With Quote
Old 27th March 2012, 20:33   #10
FlappingCrane
Junior Member
 
Join Date: Nov 2008
Posts: 2
Send a message via ICQ to FlappingCrane Send a message via AIM to FlappingCrane Send a message via Yahoo to FlappingCrane
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.
FlappingCrane is offline   Reply With Quote
Old 9th October 2013, 19:27   #11
beth62
Junior Member
 
Join Date: Apr 2010
Posts: 4
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.
beth62 is offline   Reply With Quote
Old 7th January 2014, 18:18   #12
chbrosso
Junior Member
 
Join Date: Jan 2014
Posts: 2
I have found that this bug can be worked-around by saving the command in a file. As it's a pain to do it for each command, I've put it in macros here http://nsis.sourceforge.net/PowerShell_support.

Hope you'll find it helpful for running PowerShell in NSIS!
chbrosso is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump