1

The more forums i look on, the more confused i get, and i've found very similar posts, but not exactly what i'm looking for, i'm a powershell newbie, can someone tell me the best way of being able to write this in powershell.

I can get the program to launch but it's passing the parameters and the syntax i'm struggling with. To make it easier to read i have removed any formatting from the $programArgs command so i'm hoping it's just putting in the correct synatax around it.

This is what i have so far:-

$program = "C:\Program Files (x86)\PuTTY\psftp.exe"

$programArgs = "-pw 1234 -P 10023 -i D:\sftp\Keys\mykey.ppk [email protected] -b d:\sftp\Scripts\GetAll.txt"

Invoke-Command -ScriptBlock { & $program $programArgs }

2 Answers 2

1

Should be able to just use Invoke-Expression "$program $programArgs".

Sign up to request clarification or add additional context in comments.

5 Comments

Ok thank you, i have tried this, hopefully i have the syntax right, but i now get a new error:- powershell The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program.. does anyone have any ideas $program = "c:\Program Files (x86)\PuTTY\psftp.exe" $programArgs = "-pw 1234 -P 10023 -i D:\sftp\Keys\mykey.ppk [email protected] -b d:\sftp\Scripts\GetAll.txt" Invoke-Expression "$program $programArgs"
@BigPunn25: in this case you need to wrap your $program with extra pair of quotes, like this: Invoke-Expression """$program"" $programArgs". If your arguments may contain spaces, make sure you wrap those as well.
thanks Neolisk, that did the trick and fixed the error. would yourself, or someone else be kind enough to help me with getting the formatting correct with this line of code. I have copied and pasted all the lines of code but its the $ProgramArgs line that is not right. $program = "c:\Program Files (x86)\PuTTY\psftp.exe" $programArgs = "-pw 1234 -P 10023 -i D:\sftp\Keys\mykey.ppk [email protected] -b d:\sftp\Scripts\GetAll.txt" Invoke-Expression """$program"" ""$programArgs"""
@BigPunn25: what do you mean by is not right? Putty does not like it? Wrapping arguments like you did with program will not work, because every argument in the list may need to be wrapped, so please do it when setting a variable, and then Invoke-Expression as usual. You need to investigate which arguments require quoting, do not put it everywhere - you will make it harder to maintain (if ever needed).
thank you for you comments guys. In the end i decided to use the following which works very well. Start-Process -FilePath "<path name>" -ArgumentList "<arguments>"
1

To Clarify this is the command i used to launch psftp and pass various parameters:-

Start-Process -FilePath "c:\Program Files (x86)\PuTTY\psftp.exe" -ArgumentList "-pw Password123 -P 10023 -i D:\Keys\pub.ppk [email protected] -b d:\Scripts\GetAll.txt" -Wait -NoNewWindow

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.