• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Powershell starts but never exits when invoked from Wscript shell

K

kanjasaha

#1
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 environment.

The command I pass to my exec command is as follows:

powershell.exe -noninteractive -OutputFormat Text -command "dir"

I tested the script from a command window, and it works! But when
running from a .js script, it never exits (status of exec method shows
as 0); the task manager does show the PowerShell process running. When
I terminate the powershell process from Task Manager manually, my WSH
returns with status = 1 and exitcode <> 0.

What could prevent the powershell script from running inside the child
shell while it runs successfully from command line?

Here is my whole script:

var objShell = WScript.CreateObject("WScript.Shell");

var strCMD = 'powershell.exe -noninteractive -OutputFormat Text -
command "dir"'

WScript.Echo(strCMD)

var objexec = objShell.Exec(strCMD)

WScript.Echo (objexec.status)

while (objexec.Status == 0)
{
WScript.Sleep(100);
}

WScript.Echo (objexec.status)

Any help on this will be appreciated.

Kanja
 
S

Shay Levi

#2
Hi

As you described, by using Exec(), PowerShell is launched but never exits.
If getting the exit code is your goal then you can use the Run() method insted:

Set oShell = CreateObject("Wscript.Shell")
cmd = "powershell -noprofile -command dir"
exitCode = oShell.run(cmd,1,true)
wscript.echo exitCode


I also found a post at the PowerShell team blog:
http://blogs.msdn.com/powershell/archive/2006/10/14/Windows-PowerShell-Exit-Codes.aspx

But there's a gotcha. The following works as expected (exitCode is set to
31492):

Set oShell = CreateObject("Wscript.Shell")
cmd= "powershell -noprofile -command ""&{Write-output Test; exit 31492}"""
exitCode = oShell.run(cmd,1,true)



It won't work if I put "Write-output Test; exit 31492" in a script file,
exitCode is never set to 31492 it is
set to 0/1 respectively.

cmd= "powershell -noprofile -command ""c:\scripts\exitCode.ps1"""
exitCode = oShell.run(cmd,1,true)


Hope this helps

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

> 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 environment.
>
> The command I pass to my exec command is as follows:
>
> powershell.exe -noninteractive -OutputFormat Text -command "dir"
>
> I tested the script from a command window, and it works! But when
> running from a .js script, it never exits (status of exec method shows
> as 0); the task manager does show the PowerShell process running. When
> I terminate the powershell process from Task Manager manually, my WSH
> returns with status = 1 and exitcode <> 0.
>
> What could prevent the powershell script from running inside the child
> shell while it runs successfully from command line?
>
> Here is my whole script:
>
> var objShell = WScript.CreateObject("WScript.Shell");
>
> var strCMD = 'powershell.exe -noninteractive -OutputFormat Text -
> command "dir"'
>
> WScript.Echo(strCMD)
>
> var objexec = objShell.Exec(strCMD)
>
> WScript.Echo (objexec.status)
>
> while (objexec.Status == 0)
> {
> WScript.Sleep(100);
> }
> WScript.Echo (objexec.status)
>
> Any help on this will be appreciated.
>
> Kanja
>
 
K

kanjasaha

#3
Hi,

My goal is not only to get the exitcode but also the status, stderr
and stdout which we cannot get using Run() method.

Is there a way to acheive that?

Thanks
Kanja
 
S

Shay Levi

#4
It hangs for me too when I use exec().
I wonder, can't you launch PowerShell directly instead of hosting it?


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

> Hi,
>
> My goal is not only to get the exitcode but also the status, stderr
> and stdout which we cannot get using Run() method.
>
> Is there a way to acheive that?
>
> Thanks
> Kanja
 
K

kanjasaha

#5
I will need to host it instead of launching it directly.

The reason is that I have written a bit of powershell code which needs
to be integrated with an existing VBscript.

If I have to rewrite the whole of this VBscript in powershell, it will
be a separate task altogether.
 
K

kanjasaha

#6
I finally opened a ticket with microsoft and here is there solution.

StdIn is still open so PowerShell is waiting for input. (This is an
“implementation consideration” that we’re hoping to fix in V2. The
PowerShell executable gathers all input before processing.) So
objexec.StdIn.Close() needs to be added. Here is a revised script that
works as per the requirement:

var objShell = WScript.CreateObject("WScript.Shell");

var strCMD = 'powershell.exe -noninteractive -OutputFormat Text -
command "dir"'

WScript.Echo(strCMD)
var objexec = objShell.Exec(strCMD)
WScript.Echo ("Status before stdin")
WScript.Echo (objexec.status)
objexec.StdIn.Close()
WScript.Echo ("Status after stdin")
WScript.Echo (objexec.status)


Hope this is useful to some of you.

Kanja
 
K

kanjasaha

#7
I finally opened a ticket with microsoft and here is their solution.

StdIn is still open so PowerShell is waiting for input. (This is an
“implementation consideration” that we’re hoping to fix in V2. The
PowerShell executable gathers all input before processing.) So
objexec.StdIn.Close() needs to be added. Here is a revised script
that
works as per the requirement:

var objShell = WScript.CreateObject("WScript.Shell");
var strCMD = 'powershell.exe -noninteractive -OutputFormat Text -
command "dir"'

WScript.Echo(strCMD)

var objexec = objShell.Exec(strCMD)

WScript.Echo ("Status before stdin")
WScript.Echo (objexec.status)

objexec.StdIn.Close()

WScript.Echo ("Status after stdin")
WScript.Echo (objexec.status)

Hope this is useful to some of you.


Kanja
 
S

Shay Levi

#8
Thanks for posting back the solution!


---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

> I finally opened a ticket with microsoft and here is their solution.
>
> StdIn is still open so PowerShell is waiting for input. (This is an
> “implementation consideration” that we’re hoping to fix in V2. The
> PowerShell executable gathers all input before processing.) So
> objexec.StdIn.Close() needs to be added. Here is a revised script
> that
> works as per the requirement:
> var objShell = WScript.CreateObject("WScript.Shell");
> var strCMD = 'powershell.exe -noninteractive -OutputFormat Text -
> command "dir"'
> WScript.Echo(strCMD)
>
> var objexec = objShell.Exec(strCMD)
>
> WScript.Echo ("Status before stdin")
> WScript.Echo (objexec.status)
> objexec.StdIn.Close()
>
> WScript.Echo ("Status after stdin")
> WScript.Echo (objexec.status)
> Hope this is useful to some of you.
>
> Kanja
>