Waiting till the particular message is displayed on console - vbscript

Here is my VBS code
Set wshshell = wscript.CreateObject("WScript.Shell")
Wshshell.run "C:\Temp\Executable.exe -c -dir C:\Productdir"
'Wait till "This will install the product on your computer. Press OK, Cancel" appears
WScript.Sleep 10000
WshShell.SendKeys "~"
Is it possible "rather than hard-coded sleep of 10 secs" to add something like this for e.g. if consolemessage="This will install the product on your computer. Press OK, Cancel" then WshShell.SendKeys "~"?
Can WScript.StdOut be used to capture the console messages in the above case? I was not able to do it.

You can read StdOut of a process when you execute the program using the Exec method.
Set wshshell = wscript.CreateObject("WScript.Shell")
Set p = Wshshell.Exec("C:\Temp\Executable.exe -c -dir C:\Productdir")
Do While p.Status = 0
output = ""
Do Until p.StdOut.AtEndOfStream
c = p.StdOut.Read(1)
WScript.StdOut.Write c 'write read characters to the command prompt
output = output & c
If InStr(output, "This will install the product") > 0 Then
'do stuff
Exit Do
End If
Loop
WScript.Sleep 100
Loop

Related

VBScript script as a wrapper for a silent Batch file

I have a batch file that gets some parameters from the command line and returns some values into STDOUT.
I want the batch file to be "silent" (such that the console is not shown), and found out probably the only possibility of using vbs script.
I used this thread for implementing the argument forwarding to the batch file in VBS.
Then, I used the following command for calling the batch file I wrapped:
CreateObject("WScript.Shell").Run batchFilePath & " " & Trim(arglist), 0, False
It turns out that my batch file does run, but its STDOUT is discarded somewhere, and does not make its way back to whom called the VBS script. I.e. the batch file's STDOUT is not redirected into the VBS script's STDOUT.
How can I make the batch file's STDOUT being redirected to the VBS script STDOUT, such that if I start the VBS script from some shell, the batch file's output will be printed to the shell too?
Use Exec instead of Run, like this:
set objShell = CreateObject( "WScript.Shell" )
cmd = "echo Hello World!"
' Run the process
set objRes = objShell.Exec( "cmd /c """ & cmd & """" )
' Wait for the child process to finish
Do While objRes.Status = 0
WScript.Sleep 100
Loop
' Show whatever it printed to its standard output
Wscript.Echo "The output was:" & vbNewLine & objRes.StdOut.ReadAll()
Try this...
Intreturn = WshShell.Run("cmd /c " & path& " " & args & ">c:\batchoutput.txt", 0, true)
Set fso = CreateObject("Scripting.FileSystemObject")
Set objfile = fso.OpenTextFile("c:\batchoutput.txt", 1)
text = objfile.ReadAll
Objfile.Close
Or try this...
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objexc = WshShell.Exec("cmd /c " & command and args) 'replace command and args with proper variables
strOutputText = ""
While Not objexc.StdOut.AtEndOfStream
strOutputText = strOutputText & objexc.StdOut.ReadLine()
Loop
Msgbox strOutputText
You may need some debugging on this.

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.

vbs log out with delay

I want to log out my computer with delay in a vbscript. Here is what i've tried so far:
Dim ObjShell
Set ObjShell = CreateObject("WScript.Shell")
ObjShell = msgbox("Wollen Sie den Computer herunterfahren ?", +vbYesNo+vbExclamation, "")
If ObjShell = 6 then
Set ShellObject = GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
For Each sys In ShellObject
Sys.Win32Shutdown 0
Next
End if
I do want to know how i can create a delay.
You can use WScript.Exec "shutdown.exe -L -F -t 30" to let the waiting be done by shutdown or use Wscript.Sleep 30000 to initiate a wait before the next statement is executed.
Both times are 30 seconds in this example.

capture command line output in vbscript

Got a simple script that executes a command to a server - briefly:
//Create shell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
//send commands
WshShell.SendKeys "telnet IP_ADDRESS"
WshShell.Sendkeys "dir"
Server offers feedback which I want to capture. I just need to capture the first line into a variable, and then just print that variable out to confirm.
Can you help? Thanks.
Do not use the Windows telnet client for automation purposes. The telnet client that ships with Windows was made for interactive use only.
I'd use plink (from the PuTTY suite) in batch mode for this:
plink.exe -telnet -batch IP_ADDRESS dir
The tool doesn't require installation, so you can simply deploy it alongside your script.
Run it either in a batch file using head/tail, or in a VBScript using the Exec method, so you can read from StdOut:
addr = "62.39.x.x"
port = 24
timeout = 300 'seconds
timedOut = False
cmdline = "echo ""mute near get"" | plink.exe -telnet -batch " & addr & " -P " & port
Set sh = CreateObject("WScript.Shell")
'change working directory to directory containing script and plink executable
Set fso = CreateObject("Scripting.FileSystemObject")
sh.CurrentDirectory = fso.GetParentFolderName(WScript.ScriptFullName)
'wait until command completes or timeout expires
expiration = DateAdd("s", timeout, Now)
Set cmd = sh.Exec("%COMSPEC% /c " & cmdline)
Do While cmd.Status = 0 And Now < expiration
WScript.Sleep 100
Loop
If cmd.Status = 0 Then
cmd.Terminate
timedOut = True
End If
WScript.Echo cmd.StdOut.ReadAll
If cmd.ExitCode <> 0 Then
WScript.Echo "Command terminated with exit code " & cmd.ExitCode & "."
WScript.Echo cmd.StdErr.ReadAll
WScript.Quit 1
ElseIf timedOut Then
WScript.Echo "Command timed out."
WScript.Echo cmd.StdErr.ReadAll
WScript.Quit 2
End If
It might not be the best method, but worked for me:
Windows telnet command can save the output in client side using -f arguments. Therefore, you could use:
WshShell.SendKeys "telnet -f D:\output\telnet.out IP_ADDRESS"
and at the end of your script, simply process the content of telnet.out

VBs to log (.txt) telnet output

A very simple script to save time for something i (repeatedly) need to check # work;
Set cloner = CreateObject("WScript.Shell")
cloner.SendKeys"telnet 0.0.0.0"
cloner.SendKeys("{Enter}")
WScript.Sleep 1000
cloner.SendKeys("whatever")
And, i'd like this to output to a .txt.
try this one
Set cloner = CreateObject("WScript.Shell")
cloner.Run "cmd.exe"
WScript.Sleep 1000
cloner.SendKeys"telnet 0.0.0.0 -f out.txt"
cloner.SendKeys("{Enter}")
WScript.Sleep 1000
cloner.SendKeys("whatever")
The code just producing a blank txt; no string in it.
Try:
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.run("telnet.exe")
WScript.Sleep 500
WshShell.SendKeys("set logfile filename.txt{Enter}")
WScript.Sleep 500
WshShell.SendKeys("open 0.0.0.0{Enter}")
WScript.Sleep 500
WshShell.SendKeys("whatever")
this will create a file named filename in the working directory, can be replaced with absolute path aswell.

Resources