Solved

Executing external program with PowerShell

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

749 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