how do I execute several programs consecutively in vbs - vbscript

I have a couple of applications that I would like to execute one following the other.
how do I do this?
I tried this but the second task never executed.
on error resume next
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """C:\Program Files\my folder\do task1.exe.vbs"""
WshShell.Run """C:\Program Files\my folder\do task2.exe.vbs"""
msgbox "Finished tasks"
update:
notes found on WshShell.Run click here

What you are missing (per ISDi's answer) is the third parameter of Run, which tells it to not wait for the program to quit (false), before continuing code exection.
Try (If you want to put your code in a subroutine, which if good coding practice for repeat activities):
'Place all of the following in a .vbs file
Sub RunApplication(ByVal sFile)
Dim WShell : Set WShell = CreateObject("WScript.Shell")
WShell.Run Chr(34) & sFile & Chr(34), 8, false
End Sub
'Executing apps.
RunApplication "C:\Program Files\my folder\task1.exe"
RunApplication "C:\Program Files\my folder\task2.exe"

The Run method of WScript.shell has an optional parameter that can halt execution of the script until the Run method returns.
Try:
WshShell.Run("""C:\YourPathTo\task1.exe""", 1, true)
The third parameter, true in the line above tells the interpreter to wait until this task exits before continuing to the next line of the script.
-isdi-

Related

VBS code not working when called from WMIC

I'm attempting to call some code remotely using Window 10 WMIC.
rem command prompt, machine 1 (main computer)
WMIC /node:"MACHINE_2" process call create "cmd.exe /k "%USERPROFILE%\test.vbs" "
This call seems to go through correctly. It produces partial output from test.vbs (shown below), which means the file is called and executing.
' test.vbs, machine 2 (MACHINE_2, in above code)
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile="%USERPROFILE%\out.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test1" & vbCrLf
objFile.Close
MsgBox("hi")
' CreateObject("WScript.Shell").Run "cmd /c ""nircmd.exe sendkey 0xB3 press"" ", 0
' Set WshShell = Nothing
outFile="%USERPROFILE%\out.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test2" & vbCrLf
objFile.Close
Expected behaviour: The script should write "test1" to out.txt. Then it should open a MsgBox. And after the MsgBox is closed on Machine_2, it should override the contents of out.txt with "test2."
The two lines of commented code below MsgBox can be substituted for the MsgBox code, and also have the same behaviour.
When executing the vbs file locally, the expected behaviour happens. However, when using the WMIC call, "test1" is printed, and then execution seems to stop. The MsgBox is never shown, and "test2" never overrides the content of out.txt.
I'm quite lost as to why this happens, and the steps I might take to make it work. At this point I've exhausted my google-fu.
You CANNOT display a user interface on remote scripts.
You may not mess with the logged on user.

How to display ECHO from VBScript in Command Prompt window with command line

In batch file I have the command below:
WScript ABC.vbs
In ABC.vbs:
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "Hello"
When I run the batch file there is a pop-up message with the text "Hello". But what I want is showing the message "Hello" in the Command Prompt window like I do the right click on ABC.vbs and then select "Open with command prompt".
In addition to the two comments to use cscript.exe ABC.vbs as your command line, here is a function you can put in your .vbs script to ensure it always runs with the cscript engine, no matter how it's called.
Sub checkengine
pcengine = LCase(Mid(WScript.FullName, InstrRev(WScript.FullName,"\")+1))
' BEGIN CALLOUT A
If Not pcengine="cscript.exe" Then
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "CSCRIPT.EXE """ & WScript.ScriptFullName & """"
WScript.Quit
End If
' END CALLOUT A
End Sub
From this site: Forcing VBScript Files to Run in CScript Mode
Put Call checkengine at the beginning of your vbscript. If it detects that cscript.exe is not in the command line, it relaunches the script with that engine.

Trying to script a silent uninstall with VBScript

I am attempting to write a script to an uninstall of some applications. I have the script working, but every program throws up a prompt when run.
I know that the applications support the “/S” parameter so a silent uninstall can be done. I can run the uninstall /s command from a command prompt and it works fine. No prompts, it just uninstalls.
My problem is invoking the /S parameter in a script. No matter how I try, I keep getting syntax errors. I know this is just me and my non-understanding of quotes and parenthesis, but I’m sort of tired of trying to get it to work. The problem is compounded by the fact that all the paths have spaces in them, which necessitates more of those dang quotes.
Hopefully someone can show me what I am doing wrong.
Also, I really don’t know what I’m doing with VBS stuff, so I’d appreciate it if you could all overlook how ugly the script is. :-)
I also have a question about the “true” parameter. My understanding is that this indicates that the current operation should be completed before moving on to the next operation. But the uninstalls seem to run all at the same time. Am I understanding the “true” parameter correctly?
The command for a silent uninstall is:
C:\Program Files\Juniper Networks\Network Connect 7.1.9\uninstall.exe /S
Here is my script without the "/S' parameter.
'Uninstall Juniper Networks Network Connect 7.1.9
Wscript.Echo "Uninstalling 'Juniper Networks Network Connect 7.1.9'"
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("""C:\Program Files\Juniper Networks\Network Connect 7.1.9\uninstall.exe"""), 1, True
Set objShell = nothing
Here's my self-explainig solution:
' VB Script Document
'http://stackoverflow.com/questions/23569022/trying-to-script-a-silent-uninstall-with-vbscript
option explicit
Dim strResult
strResult = Wscript.ScriptName _
& vbTab & "Uninstalling 'Juniper Networks Network Connect 7.1.9'"
Dim strCommand, intWindowStyle, bWaitOnReturn, intRetCode
' strCommand
' String value indicating the command line you want to run.
' You must include any parameters you want to pass to the executable file.
strCommand = """C:\Program Files\Juniper Networks\Network Connect 7.1.9\uninstall.exe"" /S"
'for test only strCommand = """C:\Program Files\Mozilla Firefox\firefox.exe"" -safe-mode"
' intWindowStyle
' Optional. Integer value indicating the appearance of the program's window.
' Note that not all programs make use of this information.
intWindowStyle = 1
' bWaitOnReturn
' Optional. Boolean value indicating whether the script should wait
' for the program to finish executing before continuing
' to the next statement in your script.
' If set to true, script execution halts until the program finishes,
' and Run returns any error code returned by the program.
' If set to false (the default), the Run method returns 0 immediately
' after starting the program (not to be interpreted as an error code).
bWaitOnReturn = True
strResult = strResult & vbNewLine & strCommand _
& vbNewLine & CStr( intWindowStyle) _
& vbNewLine & CStr( bWaitOnReturn)
Wscript.Echo strResult
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
' intRetCode
' The Run method returns an integer
intRetCode = objShell.Run( strCommand, intWindowStyle, bWaitOnReturn)
Set objShell = nothing
Wscript.Echo strResult & vbNewLine & "result=" & CStr( intRetCode)
Wscript.Quit

Run command line for each file in folder - quotation mark issue (I think)

I'm trying to run a run the following command line for each specific file type (as example for each .txt file) in the current directory:
"C:\Program Files (x86)\some program\someprogram.exe" "file.txt" "file.txt.mod" -someparameter
When I run this exact command from an open Windows command prompt (including all the quotation marks), it works.
But when I run it through this VB, it does nothing/closes right away.
What am I doing wrong? I have a feeling it has to do with the quotes, but my head can't sort it out.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "txt" Then
RunCommand()
End If
Next
Sub RunCommand
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C ""C:\Program Files (x86)\some program\someprogram.exe"" """ & objFile.Path & """ """ & objFile.Path & ".mod"" -someparameter"
Set oShell = Nothing
End Sub
You should
Reduce risk of failures
by
using "Option Explicit"
avoiding clever "roll your own" hacks by using standard methods (.GetParentFolderName) instead
using type prefixes correctly (objStartFolder)
avoiding variables just used once (objFolder, colFiles)
not using globals to pass parameters into Subs/Functions (objFile)
avoiding (unnecessary) stress (.Run without wait, new WScript.Shell for each file, "cmd" instead of "%comspec%")
using cscript in a 'dos box' instead of double click/wscript
and
check your assumptions
by
diagnostic output (.Echo objFile.Name immediately before calling RunCommand, use a variable to store and .Echo the command send to .Run)
Check return values of functions that provide diagnostics (.Run)
sanity checks like:
(just to tame the formatter)
>> WScript.Echo goFS.GetExtensionName("A.TXT")
>>
TXT

how to call more than 1 batch files in a vbscript?

I am using the following code to call a batch file:
dim shell
set shell=createobject("wscript.shell")
shell.run "a.bat D:\a"
set shell=nothing
How do I call more than 1 batch file, so that when the 1st file's execution is over the 2nd file is executed.
as always, I really appreciate any help offered.
Below
shell.run "a.bat D:\a"
add another line with another
shell.run "b.bat ...."
Or create a batch file that calls all the other batch files, and call that batch file from your script.
Option explicit
Dim oShell
set oShell = Wscript.CreateObject("WScript.Shell")
oShell.Run "RunAs /noprofile /user:Admininistrator ""%comspec% /c 1stcommand && 2ndcommand && 3rdcommand""", 1, false
WScript.Sleep 1000
oShell.Sendkeys "AdmininistratorPassword~"
Wscript.Quit

Resources