Solved

Executing external program with PowerShell

Posted on 2014-02-10
3
283 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

17 Experts available now in Live!

Get 1:1 Help Now