Solved

Executing external program with PowerShell

Posted on 2014-02-10
3
285 Views
Last Modified: 2014-02-11
I need a PowerShell script to (among other things) execute commands (contained in script.txt) on a remote system.  The following works if I put it in a Windows command file (.cmd).  

C:\PuTTY\plink.exe 10.10.10.5 -ssh -P 1234 -l username -pw xyz < %temp%\script.txt

How can I do this inline in PoweShell?  I expect I'll need to use the invoke-expression comdlet, but can't figure out how to handle the parameters and input redirection.
0
Comment
Question by:McThump
3 Comments
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39849846
Try this:

$arg1 = "10.10.10.5"
$arg2 = "-ssh"
$arg3 = "-P"
$arg4 = "1234"
$arg5 = "-l"
$arg6="username"
$arg7="-pw"
$arg8 = "xyz"
$arg9="< %temp%\script.txt"

$args = @($arg1,$arg2,$arg3,$arg4,$arg5,$arg6,$arg7,$arg8,$arg9)

 & "C:\PuTTY\plink.exe"  $args
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39850538
That's a pretty awful way to supply arguments. And it won't work, in particular the input redirection.
You should not need to do anything special here. The only change needed is to replace %temp% with $env:Temp
C:\PuTTY\plink.exe 10.10.10.5 -ssh -P 1234 -l username -pw xyz < $env:Temp\script.txt

Open in new window

or, more Powershell'ish:
Get-Content $env:Temp\script.txt | C:\PuTTY\plink.exe 10.10.10.5 -ssh -P 1234 -l username -pw xyz

Open in new window

0
 

Author Closing Comment

by:McThump
ID: 39851157
Works perfectly.  Thanks!!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
A brief introduction to what I consider to be the best editor for PowerShell.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now