keyongtech


  keyongtech > windows.* > windows.powershell > 05/2008

 #1  
05-02-08, 03:52 PM
kanjasaha
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
 #2  
05-02-08, 10:24 PM
Shay Levi
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/arc...xit-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
[..]
 #3  
05-05-08, 02:30 PM
kanjasaha
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
 #4  
05-05-08, 03:49 PM
Shay Levi
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
[..]
 #5  
05-06-08, 03:02 PM
kanjasaha
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.
 #6  
05-12-08, 04:52 PM
kanjasaha
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
 #7  
05-12-08, 05:05 PM
kanjasaha
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
 #8  
05-12-08, 06:14 PM
Shay Levi
Thanks for posting back the solution!

Similar Threads
PowerShell starts, but never executes nor exits?

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...

How is WScript.ScriptName in PowerShell?

How can I convert function "WScript.ScriptName" from "WSH" to "PowerShell"? In other words: Have "PowerShell" function similar like "WScript.ScriptName" in "WSH"?

Invoking PowerShell from WScript via COM

Hi all, i'm m trying to do something that doesn't seem possible using default powershell and i'm hitting a bump ?with setting my interface as the default one? / ignorance...

using WScript.CreateObject("Wscript.Shell

Sub Test() Dim objShell As Object Set objShell = WScript.CreateObject("Wscript.Shell") Wait = True objShell.Run "c:\FSOTest\FTP02.BAT", 1, Wait End sub FTP02.BAT points to a...

wscript.shell error after windows starts up

I installed software, and after I had installed it, I get a wscript.shell windows host object error. How can I get rid of this error. Thx JAY~


All times are GMT. The time now is 11:15 AM. | Privacy Policy