0

I think this has been asked many times yet I can't find an answer that works for me.

  • I need to construct a command line from variables.
  • I need to run that command line executable.
  • I need to have the results spew out onto the same screen as my script.

I'm trying to run Putty SCP with a custom source and destination.

I've messed around with & and Invoke-Expression but its impossible. Some simple #1 use-case things in PowerShell are so incredibly hard they eat away at all that's good about it.

The only way I can see to do this is to run it within a new Process instance and setup all the output redirection and then pipe it back out on the PS screen after it finished.

And I know this could fail because the output buffer can get full unless you're hooked-into the events and are scooping it out.

The other way is to write my command line to a batch file and run that.

Any help appreciated. There has to be a simpler way.

Edit

For example:

[string]$scpPath = Find-PathToPuttyScpExecutable;
[string]$scpArguments = "-v -r -pw " + $MarkLogicServerPassword + " " + $MarkLogicSourcePath + " " + $MarkLogicServerUserName + "@" + $ServerName + ":" + $MarkLogicDestinationRootPath

I need to execute $scpPath + " " + $scpArguments.

6
  • Third way is to write a CmdLet in C# but although I've done this in the past, I want to avoid loading a module. Commented Mar 20, 2013 at 13:09
  • "I've messed around with & and Invoke-Expression but its impossible" - I'm pretty sure it's not. Post your code & point out what isn't working, and someone can try to help. Otherwise, this starts sounding like a rant. Commented Mar 20, 2013 at 13:14
  • post the exe name, its parameters you need and what variables you are plugging in and also mention where they go Commented Mar 20, 2013 at 13:17
  • Hmm/Grr. I tried to post a screen dump but the .png that Snipping Tool makes isn't understood by SO! Commented Mar 20, 2013 at 13:23
  • Screenshots of code are terrible anyway. Can't copy/paste, can't search. Commented Mar 20, 2013 at 14:05

1 Answer 1

4

Don't get so attached to strings. Just run commands with arguments:

[string]$scpPath = Find-PathToPuttyScpExecutable;
&$scpPath -v -r -pw $MarkLogicServerPassword $MarkLogicSourcePath "$($MarkLogicServerUserName)@$($ServerName):$($MarkLogicDestinationRootPath)"

PowerShell will handle the quoting for you. In most cases it works well and I doubt you'll stumble over the edge cases here.

If you have to create the argument list on the fly, then you should use an array and pass that:

$scpArguments = '-v',
                '-r',
                '-pw',
                $MarkLogicServerPassword,
                $MarkLogicSourcePath,
                "$($MarkLogicServerUserName)@$($ServerName):$($MarkLogicDestinationRootPath)"

&$scpPath @scpArguments
Sign up to request clarification or add additional context in comments.

2 Comments

I did not see that coming. Must adjust mental model. Thanks Joey.
Powershell needs either an array of string args or spaces between them on the line so it can tokenize them. A single string with spaces in it will be interpreted as a single token.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.