1

Using VBScript (run through cscript), how can I call another command program (pscp in this case), which also requires user input, etc.?

The purpose is that the initial VBScript will gather the parameters (password, user, etc.), then the pscp command

pscp -r -pw password copyFromPath user@host:copyToPath

can be issued and the user will be able to see the output from the pscp command, as well as being able to input (if, for example, they gave the wrong password and are required to input it again).

I currently have:

' create the command, that calls pscp from cmd
Dim comSpec : comSpec = objShell.ExpandEnvironmentStrings("%comspec%")
Dim command : command = comspec & " /C " & """" & "pscp -r -pw " & "^""" & (Replace(pscpPassword,"""","\^""")) &  "^"" " _
  & "^""" & (windowsPath) & "^"" " _
  & pscpUser & "@" & pscpHostName & ":" & Replace(linuxPath," ","\ ") & """"
Dim objExec : Set objExec = objShell.Exec(command)

An alternative I came up with for generating command was:

Dim command : command = "pscp -r -pw " & Chr(34) & Replace(pscpPassword,"""","\""") & Chr(34) & " " _
  & Chr(34) & windowsPath & Chr(34) & " " _
  & pscpUser & "@" & pscpHostName & ":" & Replace(linuxPath," ","\ ")

But neither of these allow me to interact with pscp once it's called.

Edit

The fact that I'm calling pscp is almost irrelevant. I could be calling any program which asks for user input and displays things to stdout and stderr

simonalexander2005
  • 4,338
  • 4
  • 48
  • 92
  • `pscp` is a program to copy files from or to an SSH server. Once started it shouldn't require further interaction. If a wrong password was specified the program should terminate with an error and you should repeat the steps from asking for a password to running the command in your VBScript. – Ansgar Wiechers Sep 07 '15 at 14:21
  • no, `pscp` will prompt again for the password if the wrong one is provided - so I want to catch that and allow for it. I also want to be able to see the progress that `pscp` outputs when copying files, which I can't with the given code at the moment. – simonalexander2005 Sep 07 '15 at 14:26
  • Not if you use the parameter `-batch` (which you should). – Ansgar Wiechers Sep 07 '15 at 14:35
  • why 'should' I? I understand that it's useful in some circumstances, but that's not my requirement here for this question. And anyway, even using `-batch`, `pscp` outputs progress which I want to display - e.g.: `"filename.log | 0 kB | 0.0 kB/s | ETA: 00:00:00 | 100%"` – simonalexander2005 Sep 07 '15 at 14:48
  • If your requirement is to shoot yourself in the foot you'll have to look for assistance somewhere else. VBScript is bad at the kind of interactivity you're asking for. Excessive output on STDOUT or STDERR will clog the buffer and cause the program and script to hang. Also, not all programs will write to STDOUT/STDERR or read from STDIN. If you need to work interactively with commandline programs: use CMD or PowerShell, not VBScript. – Ansgar Wiechers Sep 07 '15 at 14:55
  • OK, then perhaps another way to phrase my question would be, how can I spawn a new command window which is running `pscp` with the parameters gathered by the script? – simonalexander2005 Sep 07 '15 at 14:57
  • That could be achieved by using the [`Run`](https://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.84%29.aspx) method (`objShell.Run command, 1, True`). That method won't return the command output, though. – Ansgar Wiechers Sep 07 '15 at 15:07
  • hmm ok, so that's a possibility. If anyone knows how to make it part of the script instead though, that would be useful (assuming run through `cscript`) – simonalexander2005 Sep 07 '15 at 15:15
  • to show the command output too you could probably run it with "cmd /c " won't be part of the script but a separate window – Syberdoor Sep 08 '15 at 05:40
  • @Syberdoor but then you lose the output as soon as it's complete, for good or for bad - if there's an error, you don't see it because the window closes. And if you use /K you end up with a lot of windows left open if this is called in a loop (which it is) – simonalexander2005 Sep 09 '15 at 08:36
  • got it, I put all the commands together into a script (separated with &), then run them all after the loop. I append "& pause" to keep the window open. Thanks all for getting me there. That solves my problem, but doesn't answer the original question – simonalexander2005 Sep 09 '15 at 09:24
  • This is all true, but you are trying to use a restricted toolset for a very very special purpose so there will always be compromises, I am just trying to give you some ideas for workarounds – Syberdoor Sep 09 '15 at 09:26
  • Fair enough. I didn't realise it would be such a specialist thing... If you want to write up some of this into an answer I'll accept it :) – simonalexander2005 Sep 09 '15 at 09:44

1 Answers1

0

I put all the commands together into a script (separated with &), then run them all after the loop. I append "& pause" to keep the window open (see comments under question for full details)

simonalexander2005
  • 4,338
  • 4
  • 48
  • 92