Call command program that requires user input - vbscript

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

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)

Related

Run cmd from vbscript silently

I created the following runas.vbs script:
**************
Option explicit
Dim oShell, k
Const PASSWORD = "Pass123~"
set oShell= Wscript.CreateObject("WScript.Shell")
WScript.Sleep 500
oShell.run("RunAs /noprofile /user:%computername%\postgres " & Chr(34) & "cmd /c\" &
Chr(34) & WScript.Arguments(0) & "\" & Chr(34) & Chr(34))
WScript.Sleep 1000
For k=1 To Len(PASSWORD)
WScript.Sleep 200
oShell.SendKeys Mid(PASSWORD,k,1)
Next
Wscript.Quit
**************
I use this vbscript in a Batch file to run initdb.exe (Postgresql).
Used as:
cscript //Nologo //B runasNSPostgres.vbs ""%LG_PATH%\initdb.exe" --locale=C --encoding=UTF-8 -U %DBADMIN% -D "%DBDATA%""
When this command is executed, another command prompt screen opens up which starts the initdb processing. I do not want the new cmd prompt screen to show up. I want the initdb.exe to run in the background.
If you hide the command prompt window, you won't be able to use SendKeys to send it your password keystrokes.
You can use another method, though. Try using the ECHO command and pipe its output to RunAs.
oShell.Run "echo " & PASSWORD & " | runas /noprofile ...", 0
Use a 0 as the 2nd parameter to prevent the window from appearing.

passing arguments(containing white space) in vbs through silk

I'm writing to call a VBScript from Silk bdh and passing arguments from Silk.
My first problem is there are more than 1 argument (total 4 arguments).
My second problem is these arguments contain white space.
Below is the program:
sCmdLine := "cscript.exe";
//sParms := "C:\\QK\\test1_old.vbs \" \"" +string(error_counter)+"\" \"" +error_timer ;
sParms := "C:\\QK\\test1.vbs \" 2\string(error_counter)+
error_timer+error_details+error_time;
hProcessId := ProcessInitialize(sCmdLine, PROCESS_PIPED, sParms,"C:\\WINDOWS\\System32\\");
ProcessSetOutputBuffer(hProcessId, reportedTo, STRING_COMPLETE);
ProcessStart(hProcessId);
StrSearchDelimited(reportedTo,STRING_COMPLETE,reportedTo,"reserved.",1,NULL,1,STR_SEARCH_FIRST);
print("reportedTo*****"+reportedTo);
VBS program is:
dim captcha
errorcounter=Wscript.Arguments(0)
errortimer=Wscript.Arguments(1)
errordetails=Wscript.Arguments(2)
errortime=Wscript.Arguments(3)
text= "Level : " & errorcounter
text= text & vbNewline
text = text & "Page : " & errortimer
text= text & vbNewline
text = text & "Error : " & errordetails
text= text & vbNewline
text = text & "Error Time : " & errortime
reportedto=inputbox( text,"ReportedTo")
You always quote parameters with spaces. This is basic Windows and was introduced 19 years ago. With the exception of chdir and notepad, all other commands and code that parses command lines expect things containing spaces to be quoted.
dir "c:\some folder\some file.txt" /a
In vbs we would write the above string to execute as (chr(34) is a quote char)
"dir " & chr(34) & "c:\some folder\some file.txt" & chr(34) & " /a"

How to redirect output from CMD window to file

I wrote the following VBScript in order to run commands from WIN XP on a Linux machine and redirect the output command to out.txt file (under C:\)
I don’t understand why output from the command window is not written to out.txt file.
What’s wrong with the line
Sh.Run "cmd /k & CMD >> ""C:\out.txt""" , 1, True
My full VB script:
'TARGET_HOST - Linux machine
Const TARGET_HOST = "18.20.183.99"
const PATH = "cat /etc/hosts"
const LOGIN = "root"
const PASS = " dingdong "
Const PLINKPATH="""C:\SPUTNIK\plink.exe"""
Set Sh = CreateObject("WScript.Shell")
CMD = " echo y | " & PLINKPATH & " -ssh -pw " & PASS & LOGIN & "#" & " " & TARGET_HOST & " " & PATH
Sh.Run "cmd /k & CMD >> ""C:\out.txt""" , 1, True
Set Sh = Nothing
VBScript doesn't expand variables inside strings. Change this:
Sh.Run "cmd /k & CMD >> ""C:\out.txt""" , 1, True
into this:
Sh.Run "cmd /k " & CMD & " >> ""C:\out.txt""" , 1, True
On top of that your command is malformed. You need to put a space between password and username, and remove the space between # and hostname:
CMD = " echo y | " & PLINKPATH & " -ssh -pw " & PASS & " " _
& LOGIN & "#" & TARGET_HOST & " " & PATH

VBscript Robocopy Syntax

I am having a problem with what I think is a syntax error in VBscript regarding running robocopy.
The following is a code snippet of what I now using to try to run robocopy:
Dim Command2
sLocalDestinationPath = "C:\Script\files\outzips\"
sFinalDestinationPath = "C:\CopyTestFolder\"
Command2 = "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath
The thing is that the command does not produce any errors, but it also does not copy any files from the local path to the final path. It runs perfectly fine when executed from the command line. Any help would be greatly appreciated because this simple command is keeping me from finishing the rest of this script.
I also have it echoing out the command and the command matches exactly what I put in the command line.
Thank you, if you need anymore explanation just let me know.
You don't say how you are trying to 'run' Robocopy, but I presume it is via WScript.Shell.Run().
I don't happen to have Robocopy handy, but I did work up an example using Windows XCopy. Perhaps you can adapt my simple XCopy example to gain more insight into your problem with Robocopy.
Option Explicit
' XCOPY doesn't Like trailing slashes in folder names
Const sLocalDestinationPath = "C:\Script\files\outzips"
Const sFinalDestinationPath = "C:\CopyTestFolder"
Dim Command2 : Command2 = _
"XCOPY" _
& " " & sLocalDestinationPath _
& " " & sFinalDestinationPath _
& " /E /I /Y" _
& ""
Dim oSh : Set oSh = CreateObject("WScript.Shell")
WScript.Echo "Cmd: [" & Command2 & "]"
On Error Resume Next
Dim nRetVal : nRetval = oSh.Run(Command2, 0, True)
If Err Then
WScript.Echo "An exception occurred:" _
& vbNewLine & "Number: [" & Hex(Err.Number) & "]" _
& vbNewLine & "Description: [" & Err.Description & "]" _
& ""
Else
If nRetVal Then
WScript.Echo "Copy error: [" & nRetVal & "]"
Else
WScript.Echo "Copy succeeded."
End If
End If
Set oSh = Nothing
' XCOPY options:
'
' /E Copies directories and subdirectories, including empty ones.
' Same as /S /E. May be used to modify /T.
'
' /I If destination does not exist and copying more than one file,
' assumes that destination must be a directory.
'
' /Y Suppresses prompting to confirm you want to overwrite an
' existing destination file.
' End

Psexec not outputting to log file in VB script

I have a VB script which needs to run psexec to launch an app called md5 on a remote server. Md5 generates a hash key of a file and takes one parameter - the file path\name. I need to retrieve the has key that is generated to store in a variable. Below is the code I am using:
Set objShell = CreateObject("Wscript.Shell")
strcomputer = "remotecomputer"
tempDest = "C:\somedir"
filename = "somefile"
strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
Set objExecObject = objShell.Exec("%comspec% /c " & strCommand)
Do While objExecObject.Status <> 1 'loop until previous process has finished
WScript.Sleep 100
Loop
The MD5 command is run however nothing is written to the log file. When I copy and paste strCommand (substituting all the variables for the actual data) into a cmd prompt and run it, it successfully writes the output of Md5 to the log file.
At the end of the day I just need the output of Md5, if anyone knows a better way than writing it to a log file please let me know. I have already tried using objExecObject.StdOut.Readall() to try and catch the output which resulted in random failures - sometimes it would catch the output, sometimes it wouldn't, without changing anything in the script.
Just a guess: Are you sure about what the current directory is when the script is running? Try giving an absolute path to the log file and see if it helps.
I found a solution for this. Instead of using the following code:
strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
Set objExecObject = objShell.Exec("%comspec% /c " & strCommand)
Do While objExecObject.Status <> 1 'loop until previous process has finished
WScript.Sleep 100
Loop
I used this instead:
strCommand = "psexec -accepteula \\" & strcomputer & " -c md5.exe " & tempDest & "\" & filename & " > log.txt"
objShell.Run "%comspec% /c " & strCommand, 0, true
The script is now redirecting to log.txt properly.

Resources