Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Executing external program with PowerShell

Posted on 2014-02-10
3
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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