Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Running a Command line script from within powershell.

Reply
 
Old 02-26-2008   #1 (permalink)
rush


 
 

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
Old 02-26-2008   #2 (permalink)
Juan Tran


 
 

Re: Running a Command line script from within powershell.

Invoke-Expression


My System SpecsSystem Spec
Old 02-26-2008   #3 (permalink)
Shay Levi


 
 

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

> 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
Old 02-27-2008   #4 (permalink)
rush


 
 

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
Old 02-27-2008   #5 (permalink)
Shay Levi


 
 

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

> 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
Old 02-27-2008   #6 (permalink)
rush


 
 

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
Old 02-27-2008   #7 (permalink)
Shay Levi


 
 

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

> 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
Old 02-27-2008   #8 (permalink)
rush


 
 

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
Reply

Thread Tools


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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46