keyongtech


  keyongtech > windows.* > windows.powershell

 #1  
03-26-08, 01:01 AM
Paul J
Hello,
I am trying to run a powershell script from a web page, via a beanshell
script (using the beanshell exec() command).
The command I pass to my exec command is simple:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powersh ell -command "&
'C:\test.ps1'"

My test.ps1 is a one-liner script:
Write-Output "Hello!"

I tested the script from a command window, and it works! But when running
from the web page, I see the powershell process running in the task manager,
but it never exits (when I kill it, my beanshell returns).
I also tried to pass -Noninteractive -NoLogo -NoProfile, with no better
results.

I tried calling a batch script (C:\test.bat), and this works.

What could prevent the powershell script from running? Security issues? Wait
on standard input / output? Other?

Thanks for your help.
 #2  
03-26-08, 12:01 PM
Marco Shaw [MVP]
Paul J wrote:
[..]
> but it never exits (when I kill it, my beanshell returns).
> I also tried to pass -Noninteractive -NoLogo -NoProfile, with no better
> results.
>
> I tried calling a batch script (C:\test.bat), and this works.
>
> What could prevent the powershell script from running? Security issues? Wait
> on standard input / output? Other?
>
> Thanks for your help.


Hard to tell exactly here because of the mix involved. If the web
server is IIS, then PowerShell is getting executed as the web site's
configured user.

It is also quite possible that the output from the PowerShell command is
being returned to the beanshell script, and that's causing problems.

Interesting nonetheless... What OS? What web server? What version of
"beanshell" (which I've never heard of until just now)?
 #3  
03-26-08, 01:12 PM
Marco Shaw [MVP]
> C:\WINDOWS\system32\WindowsPowerShell\v1.0\powersh ell -command "&
> 'C:\test.ps1'"
>
> My test.ps1 is a one-liner script:
> Write-Output "Hello!"
>
> I tested the script from a command window, and it works! But when running
> from the web page, I see the powershell process running in the task manager,
> but it never exits (when I kill it, my beanshell returns).
> I also tried to pass -Noninteractive -NoLogo -NoProfile, with no better
> results.


How exactly did you run this from a command window (you mean from
PowerShell and/or DOS or the Beanshell CLI)?

I'm not sure what the Java escape character is/are?

Marco
 #4  
03-26-08, 02:15 PM
Paul J
Hi Marco,
Tx for your reply.
Beanshell is a scripting language (http://www.beanshell.org/ ). The reason I
use it is that it comes with the web application that we are using:
Confluence (a wiki web app), which runs on Tomcat.

So my stack is: Windows Server 2003 SP2, Java JDK 1.4.2, Tomcat 5.5,
Confluence 2.4, BeanShell (don't know the exact version...).

I ran my PS1 script successfully from the Start/Run box and from a DOS
command window. And I ran a BAT script successfully from my beanshell
embedded script.
But if in my BAT script, I include a call to the powershell script, it runs
fine from the DOS command window, but the Powershell process hangs when the
BAT script is started from the beanshell embedded script!
In both cases, I can see in the Task Manager that the Powershell process is
started under the same user (Administrator).

The output (stdout) of the beanshell script is redirected to the Web App to
create the web page.
The escape sequence looks fine to me (backslash is the esc char).

Could it be that the powershell script is blocked on some resource at
startup because of the context in which the calling process is running?

Thanks for your help.

"Marco Shaw [MVP]" wrote:
[..]
 #5  
03-26-08, 02:28 PM
Marco Shaw [MVP]
> Could it be that the powershell script is blocked on some resource at
> startup because of the context in which the calling process is running?


Really hard to tell what might be blocking. If you really want to try
and figure it out, I might try if VBScript might work this way. If
VBScript does work, maybe you need a little trick like this to make it work:

http://blog.sapien.com/index.php/200...led-powershell

Marco
 #6  
03-26-08, 02:33 PM
Oisin (x0n) Grehan [MVP]
On Mar 26, 10:15 am, Paul J <Pa> wrote:
[..]
>
> "Marco Shaw [MVP]" wrote:
>>
>>

>
> - Show quoted text -


Hi Paul,

What is in your script file? Have you tested it using an extremely
minimal script, perhaps just using a inline command like so:

powershell.exe -noninteractive -noprofile -command "write-host hello,
world"

Additionally, you can verify the command line arguments to powershell
by using something like process explorer:

http://technet.microsoft.com/en-us/s.../bb896653.aspx

Right click the hung powershell process and view properties - you will
see the full command line that beanshell handed over to the
shellexecute api. Powershell is notoriously picky with arguments,
especially with quotes.

Hope this helps,

- Oisin
 #7  
03-26-08, 09:31 PM
Paul J
Thanks for the link.

Here is what I tested: my Web page calls a BAT file which calls my PS1 file.
My PS1 file has one line:
Write-Output "Welcome!"

My BAT file content is:
@echo off
@echo Calling test.ps1
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powersh ell -NoLogo -command
"& 'C:\Test\test.ps1'"
@echo Call returned

When I inspect the process via ProcessExplorer, the command line is the one
the BAT script passed, but the PS1 process doesn't return.

I copied / pasted your inline command below, and PS1 was still hanging. Here
is the copy/paste from the command line from ProcessExplorer:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powersh ell -NoLogo -command
"write-host hello, world"
(Again, this command works fine from the Start/Run box)

I can see in ProcessExplorer that 'java' calls 'cmd' which in turn calls
'powershell', but I have no idea why powershell is hanging in this context.

"Oisin (x0n) Grehan [MVP]" wrote:
[..]
 #8  
03-26-08, 09:42 PM
Paul J
> Really hard to tell what might be blocking.
Yes, I am running out of idea...

> If you really want to try
> and figure it out, I might try if VBScript might work this way. If
> VBScript does work, maybe you need a little trick like this to make it work:
>
> [..]
>

I am not sure adding VBScript to the mix will help solve the issue. Thank
you though.
 #9  
03-27-08, 01:08 AM
Oisin (x0n) Grehan [MVP]
On Mar 26, 5:31 pm, Paul J <Pa> wrote:
[..]
>>
>>
>>

> - Show quoted text -


Hi Paul,

I'm curious about why you have a batch file wrapping the powershell
command. Have you tried just launching the powershell command I gave
you directly from beanshell?

- Oisin
 #10  
03-27-08, 02:48 PM
Paul J
> I'm curious about why you have a batch file wrapping the powershell
> command. Have you tried just launching the powershell command I gave
> you directly from beanshell?


Hi Oisin,
I wrapped the powershell command in a batch file in an attempt to isolate
the problem. It doesn't have to be that way, but it shows that the beanshell
command can launch an external batch file, and that that batch file returns
if it doesn't call the powershell command.
Yes, I tried launching powershell directly from beanshell using this command
('\' is the escape character) :
exec("C:\\WINDOWS\\system32\\WindowsPowerShell\\v1 .0\\powershell.exe
-noninteractive -noprofile -command \"write-host hello, world\"");
But I get the same behavior: a powershell process is created but doesn't
return. The command line (copy/pasted from Process Explorer) is:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powersh ell.exe
-noninteractive -noprofile -command "write-host hello, world"
The parent process is java.exe.
One other difference that I see with a standard powershell window is that
this Powershell process doesn't have a window. Could that be an issue?
Thanks,
Paul
Similar Threads
Thread Thread Starter
Powershell starts but never exits when invoked from Wscript shell

Hi, I am trying to invoke a very basic powershell command (version 1.0) from a .js script, via a Wscript shell (using the exec() command of WshShell Object ) in windows XP...

kanjasaha
CreateProcessAsUser - Process starts then exits

I have a service that calls CreateProcessAsUser which is succeeds, except that no program appears to be opened. I ran process monitor and filtered for process create, start,...

coder0xff
Help! KDE exits when i starts

Afternoon all, I'm hoping someone can point me in the right direction: I turned on my main box (slack 9.1, with the supplied kernel, KDE version, etc) as usual, logged in...

standardblue
Explorer.exe repeatedly starts and exits

I have a problem. Explorer.exe repeatedly starts and exits, when I try to launch windows explorer or internet explorer, they are blocked and shut down as well. the CPU usage...

parazyte
chkdsk executes everytime user's computer starts

everytime my user starts her computer (which has been properly shut down)chkdsk executes. She receives the following message: There are inconsistencies on your...

lisa

Privacy Policy | All times are GMT. The time now is 06:24 PM.

Merging Information Logo
[Deutschland] [Espaņa] [France] [Italia] [Nederland] [Polska] [United Kingdom]