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

Running a Command line script from within powershell.

R
#1
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 Computer

S

Shay Levi

#3
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 Computer

R
#4
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 Computer

S

Shay Levi

#5
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 Computer

R
#6
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 Computer

S

Shay Levi

#7
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/Blog/cns!5A8D2641E0963A97!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 Computer

R
#8
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 Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)