1.    26 Feb 2008 #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.    26 Feb 2008 #2
    Juan Tran Guest

    Re: Running a Command line script from within powershell.


    Invoke-Expression


      My System SpecsSystem Spec

  3.    26 Feb 2008 #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

  4.    27 Feb 2008 #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

  5.    27 Feb 2008 #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

  6.    27 Feb 2008 #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

  7.    27 Feb 2008 #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

  8.    27 Feb 2008 #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


Similar Threads
Thread Forum
Command line execution in Powershell
I've written a script to build parameters to pass to a command that will be executed via the command line. The trouble I'm having is finding a...
PowerShell
How to pass the name of a PowerShell script as a command line para
How to pass the name of a PowerShell script as a command line parameter to be run by anther PowerShell script -- or -- How can I call another...
PowerShell
noob: PS script fails, but runs from command line
I want to call RoboCopy from a PS script. When I run this line from the PS command line, it works great: robocopy C:\my\directory...
PowerShell
Running a command from inside a script, command line is corrupted
Hello, I am trying to give Powershell a chance, since it seems like it may have some promise. I have this powershell script that needs to run...
PowerShell
Script from command-line
Ok... I feel as if this worked for me before. From Cmd.exe: powershell "c:\test test\test.ps1" doesn't work. It tells me it doesn't know...
PowerShell
Powershell.exe command-line syntax
Howdy, Hoping to try and accomplish something like this: powershell.exe -command "& {c:\script.ps1; if (!$?) {read-host - prompt "An error...
PowerShell
Running PS1 file from command line
Hi, I am trying to run a powershell script from the command line, and I would like this script to NOT show the box that powershell seem to show when...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • PC Help Forum
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 15:38.
    .