Supressing Dialog Box Using VBScript - vbscript

I have an automated testing program, which uses SOAPUI, that is executed using VBScript. However, whenever I execute the testing program a SOAPUI dialog box opens up asking something like "Do you want to help us improve SOAPUI by sending usage statistics." I manually click "No," and the testing program keeps running along.
Is there a way to suppress this dialog box so that it never appears when I run the automated testing program. The VBScript code is below.
Option Explicit
Dim RootFolder, ExecFolder, Command, FSO, Shell
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Shell = CreateObject("WScript.Shell")
RootFolder = FSO.GetParentFolderName(FSO.GetParentFolderName(WScript.ScriptFullName))
ExecFolder = FSO.GetParentFolderName(WScript.ScriptFullName)
On Error Resume Next
'Clean previous log files
Call FSO.DeleteFile(ExecFolder & "\*.txt", True)
Call FSO.DeleteFile(RootFolder & "\Test\*.txt", True)
Call FSO.DeleteFile(ExecFolder & "\*.log", True)
'Execute
Command = Chr(34) & RootFolder & "\SoapUI\bin\testrunner.bat" & Chr(34) & " " & _
Chr(34) & RootFolder & "\Test\Expando.xml" & Chr(34)
Shell.CurrentDirectory = ExecFolder
Call Shell.Run(Command, 1, True)

I have resolved this problem by using SendKeys.
OPTION EXPLICIT
Dim RootFolder, ExecFolder, Command, FSO, Shell
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Shell = CreateObject("WScript.Shell")
RootFolder = FSO.GetParentFolderName(FSO.GetParentFolderName(Wscript.ScriptFullName))
ExecFolder = FSO.GetParentFolderName(Wscript.ScriptFullName)
On Error Resume Next
'Clean previous log files
Call FSO.DeleteFile(ExecFolder & "\*.txt", True)
Call FSO.DeleteFile(RootFolder & "\Test\*.txt", True)
Call FSO.DeleteFile(ExecFolder & "\*.log", True)
'Execute
Command = Chr(34) & RootFolder & "\SoapUI\bin\testrunner.bat" & Chr(34) & " " & Chr(34) & RootFolder & "\Test\Expando.xml" & Chr(34)
Shell.CurrentDirectory = ExecFolder
Call Shell.Run(Command, 1, False)
WScript.Sleep(20000)
Shell.SendKeys("~")

Related

WScript doesn't work with HPALM workflow. But works fine when executed in command Line

I'm setting up a new HP ALM workflow script to invoke firefox and open two tabs in the same.
The script given works like a charm in command line. However, the same script doesn't work well flow HP-ALM Workflow. I'm certain the script is being invoked though, but the firefox browser is not opening. Placed alerts to debug the script and the alerts from the script are displayed.
Dim wshshell
Set wshshell = WScript.CreateObject("Wscript.Shell")
wshshell.run """firefox.exe"" www.google.com",1,False
wshshell.run """firefox.exe"" www.yahoo.com",2,False
Set wshshell = Nothing
wscript.quit
On a Button click in HP ALM client, firefox should be opened with two tabs. One with google & other with yahoo homepage. However, No page is opening.
Finally, the following solution helped;
ActionCanExecute()
Function ActionCanExecute()
On Error Resume Next
ActionCanExecute = DefaultRes
browserName = "Mozilla Firefox"
browserExeName = "Firefox.EXE"
cmdLineArgPlatform = " -new-window "
cmdLineArgIdc = " -new-tab "
sURLPlatform = "www.google.com"
sURLIdc = "www.yahoo.com"
Set WSHShell = CreateObject("WScript.Shell")
exePath = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\" & _
"CurrentVersion\App Paths\" & browserExeName & "\")
'Open the URL
sShellCmdPlatform = """" & exePath & """" & "" & cmdLineArgIdc & """" & sURLPlatform & """"
MsgBox sShellCmdPlatform
sShellCmdIdc = """" & exePath & """" & "" & cmdLineArgIdc & """" & sURLIdc & """"
MsgBox sShellCmdIdc
'MsgBox sFFExe
WSHShell.Run sShellCmdPlatform, vbHide
WSHShell.Run sShellCmdIdc
On Error Resume Next
Set WSHShell = Nothing
ActionCanExecute = DefaultRes
On Error GoTo 0
End Function

Writing a VBS via another VBS

I'm trying to create a VBS via another VBS, but cant handle the quotation marks, and would love to know if it's even possible.
That's the 1 line I need in my new VBS:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Main VBS code that doesn't work:
Dim oFSO, vbFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set vbFile = oFSO.CreateTextFile("try.vbs", True)
vbFile.WriteLine ""CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False""
vbFile.Close
Thanks to Sorceri, I managed to make it work by writing the following code:
Dim oFSO, vbFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set vbFile = oFSO.CreateTextFile("try.vbs", True)
vbFile.WriteLine "CreateObject(" & Chr(34) & "Wscript.Shell" & Chr(34) & ").Run """""""" & WScript.Arguments(0) & """""""", 0, False"
vbFile.Close
This is what you need to do.
Copy and paste this code
do
msgbox("haha you cant close this")
CreateObject ("WScript.Shell").Run(".\Duplicate.vbs")
loop

Get a VBS file to scan computer for a file

This is my first post, but I have been programming for a long time now
I just want to ask a quick question and the title explains it all. I want my VBS to run a file, but I dont want it to search just for a specific directory, I want it to just find the file if you know what I mean, because if I gave the script to anyone else, this file could be ANYWHERE on their computer.
This is the current couple of important lines that I am using for running files:
set wshshell = wscript.CreateObject("wscript.shell")
and
wshshell.run <program directory here>
You need a recursive function like this one searching for shortcuts.
Sub GenerateHotkeyInFolder(Fldr)
on error resume next
set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set FldrItems=Fldr.Items
For Each oFile in FldrItems
With oFile
If .IsFileSystem = true And .IsLink = true And .Type <> "Shortcut to MS-DOS Program" then
set lnk = WshShell.CreateShortcut(oFile.Path)
If lnk.hotkey <> "" then
Set fsop = fso.GetFile(.Path)
LnkName = "<b>" & fso.GetBaseName(fso.GetFile(.Path)) & "</b><br>" & fsop.ParentFolder.path & "\" & fso.GetBaseName(fso.GetFile(.Path)) & "." & fso.GetExtensionName(fso.GetFile(.Path))
TableVar = TableVar & "<tr><td><b>" & lnk.hotkey & "</b></td><td><a class=TblURL onmouseover='MakeRed()' onmouseout='MakeBlack()' onclick='FindShortcut(" & Chr(34) & lnk.fullname & Chr(34) & ")'>" & lnkname & "</a>" & "</td><td><a class=TblURL onmouseover='MakeRed()' onmouseout='MakeBlack()' onclick='FindShortcut(" & Chr(34) & lnk.targetpath & Chr(34) & ")'>" & lnk.targetpath & "</a></td></tr>" & vbcrlf
End If
ElseIf .IsFileSystem = true And .IsFolder = true then
GenerateHotkeyInFolder(.GetFolder)
End If
End With
Next
End Sub

Starting a process in VBS: path not found

I need to make a simple vbs script to run some process' automatically. I found the following script on microsoft's website. It works fine to run notepad.exe the way the original example shows, but I'm trying to modify it to run myprog.exe. The full path to this program is: C:\myprogdir\myprog.exe
Const SW_NORMAL = 1
strComputer = "."
strCommand = "myprog.exe"
strPath = "C:\myprogdir\"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
' Configure the Notepad process to show a window
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = SW_NORMAL
' Create Notepad process
Set objProcess = objWMIService.Get("Win32_Process")
intReturn = objProcess.Create _
(strCommand, strPath, objConfig, intProcessID)
If intReturn <> 0 Then
Wscript.Echo "Process could not be created." & _
vbNewLine & "Command line: " & strCommand & _
vbNewLine & "Return value: " & intReturn
Else
Wscript.Echo "Process created." & _
vbNewLine & "Command line: " & strCommand & _
vbNewLine & "Process ID: " & intProcessID
End If
I keep getting Return value: 9, which indicates "Path Not Found". However the path is correct. Is there something I'm not getting?
You don't need all that to start a process, you just need the Shell object. Also, be sure to wrap the path of your executable in quotes (in case the path has spaces). Like this:
Option Explicit
Dim shl
Set shl = CreateObject("Wscript.Shell")
Call shl.Run("""C:\myprogdir\myprog.exe""")
Set shl = Nothing
WScript.Quit
Unless the path to your program is included in the system's %PATH% environment variable you need to specify the commandline with the full path to the executable. Specifying the path just as the working directory will not work.
strProgram = "myprog.exe"
strPath = "C:\myprogdir"
Set fso = CreateObject("Scripting.FileSystemObject")
strCommand = fso.BuildPath(strPath, strProgram)
...
intReturn = objProcess.Create(strCommand, strPath, objConfig, intProcessID)
Using the BuildPath method will save you the headaches caused by having to keep track of leading/trailing backslashes.
Note that you need to put double quotes around a path that contains spaces, e.g. like this:
strCommand = Chr(34) & fso.BuildPath(strPath, strProgram) & Chr(34)
As others have already pointed out, there are simpler ways to start a process on the local computer, like Run:
Set sh = CreateObject("WScript.Shell")
sh.Run strCommand, 1, True
or ShellExecute:
Set app = CreateObject("Shell.Application")
app.ShellExecute strCommand, , strPath, , 1
There are some notable differences between Run and ShellExecute, though. The former can be run either synchronously or asynchronously (which means the command either does or doesn't wait for the external program to terminate). The latter OTOH always runs asynchronously (i.e. the method returns immediately without waiting for the external program to terminate), but has the advantage that it can be used to launch programs with elevated privileges when UAC is enabled by specifying the verb "runas" as the 4th argument.
However, these methods only allow for launching processes on the local computer. If you want to be able to launch processes on remote computers you will have to use WMI:
strComputer = "otherhost"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
See here for more information about WMI connections to remote hosts.

Executing VBScript file from Excel VBA macros

I need some excel vba examples, where with in the VBA code(Excel Macro) i could call a VBScript and will get some values like filename and directory information from the vbscript and assign it to the variables in VBA code.
Thank you in advance
Some thing like this
VBA macro:
Sub Foo2Script
Dim x As Long
x=2
'Call VBscript here
MsgBox scriptresult
End Sub
VBScript:
Dim x, y ,Z
x = x_from_macro
y = x + 2
Z = X+Y
scriptresult = y,Z
It can be done but I would have to agree with Tomalak and others that it's not the best way to go. However, saying that, VBScript can work wonders occasionally if you use it as a kind of fire and forget mechanism. It can be used quite effectively to simulate multi-threading in VBA whereby you breakdown the payload and farm it out to individual VBScripts to run independently. Eg you could arrange a "swarm" of individual VBScripts to mass download from websites in the background whilst VBA continues with other code.
Below is some VBA code I've simplified to show what can be done and writes a simple VBScript on the fly. Normally I prefer to run it using 'wshShell.Run """" & SFilename & """" which means I can forget about it but I've included in this example this method Set proc = wshShell.exec(strexec) which allows a test of the object for completion
Put this in MODULE1
Option Explicit
Public path As String
Sub writeVBScript()
Dim s As String, SFilename As String
Dim intFileNum As Integer, wshShell As Object, proc As Object
Dim test1 As String
Dim test2 As String
test1 = "VBScriptMsg - Test1 is this variable"
test2 = "VBScriptMsg - Test2 is that variable"
'write VBScript (Writes to Excel Sheet1!A1 & Calls Function Module1.ReturnVBScript)
s = s & "Set objExcel = GetObject( , ""Excel.Application"") " & vbCrLf
s = s & "Set objWorkbook = objExcel.Workbooks(""" & ThisWorkbook.Name & """)" & vbCrLf
s = s & "Set oShell = CreateObject(""WScript.Shell"")" & vbCrLf
s = s & "Msgbox (""" & test1 & """)" & vbCrLf
s = s & "Msgbox (""" & test2 & """)" & vbCrLf
s = s & "Set oFSO = CreateObject(""Scripting.FileSystemObject"")" & vbCrLf
s = s & "oShell.CurrentDirectory = oFSO.GetParentFolderName(Wscript.ScriptFullName)" & vbCrLf
s = s & "objWorkbook.sheets(""Sheet1"").Range(""" & "A1" & """) = oShell.CurrentDirectory" & vbCrLf
s = s & "Set objWMI = objWorkbook.Application.Run(""Module1.ReturnVBScript"", """" & oShell.CurrentDirectory & """") " & vbCrLf
s = s & "msgbox(""VBScriptMsg - "" & oShell.CurrentDirectory)" & vbCrLf
Debug.Print s
' Write VBScript file to disk
SFilename = ActiveWorkbook.path & "\TestVBScript.vbs"
intFileNum = FreeFile
Open SFilename For Output As intFileNum
Print #intFileNum, s
Close intFileNum
DoEvents
' Run VBScript file
Set wshShell = CreateObject("Wscript.Shell")
Set proc = wshShell.exec("cscript " & SFilename & "") ' run VBScript
'could also send some variable
'Set proc = wsh.Exec("cscript VBScript.vbs var1 var2") 'run VBScript passing variables
'Wait for script to end
Do While proc.Status = 0
DoEvents
Loop
MsgBox ("This is in Excel: " & Sheet1.Range("A1"))
MsgBox ("This passed from VBScript: " & path)
'wshShell.Run """" & SFilename & """"
Kill ActiveWorkbook.path & "\TestVBScript.vbs"
End Sub
Public Function ReturnVBScript(strText As String)
path = strText
End Function
This demonstrated several ways that variables can be passed around.

Resources