Windows Vista Forums

Running a Command line script from within powershell.

  1. #1


    rush Guest

    Running a Command line script from within powershell.

    I have a 3rd party command line tool I need to run from within a
    powershell script.
    What is the current method for doing this? I need the calling
    powershell script to wait for the 3rd party tool to finish before
    moving on...

    the tool has a number of input I need to provide...

    I'm trying to do something like this !

    $Tool="D:\tool.exe"
    $Password="passw0rd"
    $InputFile="D:\tmp\input.txt"

    $CmdLine = $Tool + " -u=User -p=" + $Password + " -i=" + $InputFile +"
    -m=mode"

    NOW HOW WOULD I RUN THE $CmdLine !?

    Thanks in advance.
    Russ.


      My System SpecsSystem Spec

  2.   


  3. #2


    Juan Tran Guest

    Re: Running a Command line script from within powershell.

    Invoke-Expression



      My System SpecsSystem Spec

  4. #3


    Shay Levi Guest

    Re: Running a Command line script from within powershell.



    Pipe it to out-null to make the current command wait until it finishes before
    moving on.


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

    > I have a 3rd party command line tool I need to run from within a
    > powershell script.
    > What is the current method for doing this? I need the calling
    > powershell script to wait for the 3rd party tool to finish before
    > moving on...
    > the tool has a number of input I need to provide...
    >
    > I'm trying to do something like this !
    >
    > $Tool="D:\tool.exe"
    > $Password="passw0rd"
    > $InputFile="D:\tmp\input.txt"
    > $CmdLine = $Tool + " -u=User -p=" + $Password + " -i=" + $InputFile +"
    > -m=mode"
    >
    > NOW HOW WOULD I RUN THE $CmdLine !?
    >
    > Thanks in advance.
    > Russ.


      My System SpecsSystem Spec

  5. #4


    rush Guest

    Re: Running a Command line script from within powershell.

    invoke-expression doesn't appear to do anything !?
    I've tried the command line in a dos prompt and it works fine.

    Any other ideas out there please?

    I've tried :

    invoke-expression -command $CmdLine
    and
    invoke-expression -command $CmdLine | out-null

    Neither appear to even attempt to run the $CmdLine.

    Thanks Again.
    Russ

      My System SpecsSystem Spec

  6. #5


    Shay Levi Guest

    Re: Running a Command line script from within powershell.


    Runing native commands (executables) in PowerShell is much like running them
    in DOS, type:

    ipconfig /all

    and get the output just as if you typed it in CMD.


    As for your 3rd party tool, I can't test its exact parameters but the above
    can be a candidate to explain how to invoke commands:

    $tool="ipconfig.exe"
    $cmdLine = "/all"

    invoke-expression "$tool $cmdLine"


    Then pipe to out-null

    invoke-expression "$tool $cmdLine" | out-null


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

    > invoke-expression doesn't appear to do anything !? I've tried the
    > command line in a dos prompt and it works fine.
    >
    > Any other ideas out there please?
    >
    > I've tried :
    >
    > invoke-expression -command $CmdLine
    > and
    > invoke-expression -command $CmdLine | out-null
    > Neither appear to even attempt to run the $CmdLine.
    >
    > Thanks Again.
    > Russ


      My System SpecsSystem Spec

  7. #6


    rush Guest

    Re: Running a Command line script from within powershell.

    Hi Shay,

    Even if I simplify the commandline down to the absolute minimum,
    powershell doesn't even attempt to run it? is there a security issue
    here maybe?

    I've tested invoke-expression with a dir listing, works fine.
    I've tried putting the EXE line in a BAT file and executing that, It
    runs the BAT and does DOS commands within but will not run the EXE.

    $Test = "dir > D:\Teamcenter\import\tmp\test.out"
    invoke-expression -command "$Test"
    # WORKS

    Invoke-Expression -Command "D:\ren\ips
    \v2007\ips_data_upload_2007_6_7_1_nti.exe"
    # FAILS - Doesn't even attempt to run the EXE.

    Just to reiterate the EXE does work normally via DOS.

    All ideas/suggestion greatly appreciated!

    Cheers
    Russ


      My System SpecsSystem Spec

  8. #7


    Shay Levi Guest

    Re: Running a Command line script from within powershell.


    It should run. Take the tasklist.exe for example:

    invoke-expression "C:\windows\system32\tasklist.exe"


    Another way is to invoke commands is with the Call operator (ampersand sign):

    & "C:\windows\system32\tasklist.exe" /SVC


    There is also a great blog post by Keith Hill (I recommend the whole series):

    Effective PowerShell Item 10: Understanding PowerShell Parsing Modes
    http://keithhill.spaces.live.com/Blo...A97!6058.entry



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

    > Hi Shay,
    >
    > Even if I simplify the commandline down to the absolute minimum,
    > powershell doesn't even attempt to run it? is there a security issue
    > here maybe?
    >
    > I've tested invoke-expression with a dir listing, works fine.
    > I've tried putting the EXE line in a BAT file and executing that, It
    > runs the BAT and does DOS commands within but will not run the EXE.
    > $Test = "dir > D:\Teamcenter\import\tmp\test.out"
    > invoke-expression -command "$Test"
    > # WORKS
    > Invoke-Expression -Command "D:\ren\ips
    > \v2007\ips_data_upload_2007_6_7_1_nti.exe"
    > # FAILS - Doesn't even attempt to run the EXE.
    > Just to reiterate the EXE does work normally via DOS.
    >
    > All ideas/suggestion greatly appreciated!
    >
    > Cheers
    > Russ


      My System SpecsSystem Spec

  9. #8


    rush Guest

    Re: Running a Command line script from within powershell.

    Hi Shay.

    Thanks for the responses, I've found the issue!..

    I need to use invoke-item not invoke-expression.

    Thanks for your help.
    Cheers
    Russ

      My System SpecsSystem Spec


Running a Command line script from within powershell.
Similar Threads
Thread Forum
How to pass the name of a PowerShell script as a command line para PowerShell
noob: PS script fails, but runs from command line PowerShell
Running a command from inside a script, command line is corrupted PowerShell
Script from command-line PowerShell
Running PS1 file from command line PowerShell