Windows Vista Forums

Powershell starts but never exits when invoked from Wscript shell

  1. #1


    kanjasaha Guest

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

      My System SpecsSystem Spec

  2. #2


    Shay Levi Guest

    Re: Powershell starts but never exits when invoked from Wscript shell

    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

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


      My System SpecsSystem Spec

  3. #3


    kanjasaha Guest

    Re: Powershell starts but never exits when invoked from Wscript shell

    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

      My System SpecsSystem Spec

  4. #4


    Shay Levi Guest

    Re: Powershell starts but never exits when invoked from Wscript shell


    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


      My System SpecsSystem Spec

  5. #5


    kanjasaha Guest

    Re: Powershell starts but never exits when invoked from Wscript shell

    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.


      My System SpecsSystem Spec

  6. #6


    kanjasaha Guest

    Re: Powershell starts but never exits when invoked from Wscript shell

    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


      My System SpecsSystem Spec

  7. #7


    kanjasaha Guest

    Re: Powershell starts but never exits when invoked from Wscript shell

    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

      My System SpecsSystem Spec

  8. #8


    Shay Levi Guest

    Re: Powershell starts but never exits when invoked from Wscript shell

    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
    >


      My System SpecsSystem Spec


Powershell starts but never exits when invoked from Wscript shell
Similar Threads
Thread Forum
Wscript.Shell open file in wmplayer VB Script
WScript.Shell -> Terminal Server 2008... VB Script
WScript.Shell echo command? VB Script
PowerShell starts, but never executes nor exits? PowerShell
Invoking PowerShell from WScript via COM PowerShell