VBS running a file on a .bat - batch-file

I am trying to run a .bat file as part of my VBScript with the parameter of another file.
I have tried:
param1 = CurrentFolder & "\file.extension"
command = "C:\folder name\compiler.bat"
Set WScript = CreateObject("WScript.Shell")
WScript.run "cmd " & command & " " & param1
But nothing seems to work.
Im trying to achieve the same as if I dragged "file.extension" and dropped it onto "compiler.bat"

Choose a different name for your Shell object. WScript is a built-in, global object in WSH. For example:
Set objShell = CreateObject("WScript.Shell")
Try this for your Run statement:
objShell.Run "cmd /c " & Chr(34) & Chr(34) & command & Chr(34) & " " & Chr(34) & param1 & Chr(34) & Chr(34)
The /c will close the prompt when the command completes. The Chr(34)'s are used to put quotes around your command and your parameter, in case either contains spaces. Note that you also need quotes around the entire statement. For example:
cmd /c ""c:\folder name\compiler.bat" "a param with spaces""

Related

How to invoke administrator command prompt from batch script without having to enter password

I am trying to automate a windows service installation task using an installUtil command embedded in a .bat file. The OS I have is Windows 2012 Server standard edition.
Whenever I do it manually, I need to invoke the administrator command prompt to run the InstallUtil command. Even though my login account has administrative privileges, when I invoke a command prompt, change path to "C:\Windows\Microsoft.NET\Framework64\v4.0.30319", and then run the command:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil "Path of my executables"
I am met with an error message saying "The installation failed, and the rollback has been performed"
To get around the above error, I right-click on "Command Prompt" under "Windows System", and choose "Run as Administrator", this invokes the administrator command prompt, i.e., the one captioned "Administrator: Command Prompt". The windows service installs just fine when I run the above InstallUtil command on this prompt. I do not need to enter the administrator password in order to invoke the administrator command prompt in this manner and run commands on it.
Now I am trying to automate the same process as above using a .bat file.
In the .bat file, I try entering the following command:
InstallUtil /runas /user:MyMachine\Administrator
But when I run the batch file (both by double clicking and using the 'Run as Administrator' option), I am prompted for the administrator password. I try supplying various options to the InstallUtil command, such as /nouac, /noprofile, /env, etc., but it prompts me for the administrator password each time, which I don't have.
Any ideas how to get around this?
This is impossible you cant grant a file admin privileges automatically. If this was possible then it would defeat the object of UAC. And this would allow all kinds of vulnerability's to open up.
This is vbscript using the shell elevation verb RUNAS to elevate itself.
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "wscript.exe", chr(34) & WScript.ScriptFullName & Chr(34) & " " & sParms, , "runas", 1
Msgbox "error is " & err.number & " " & err.description
This is a script that allows you to list verbs for a file or execute verbs for a file.
HelpMsg = vbcrlf & " ShVerb" & vbcrlf & vbcrlf & " David Candy 2014" & vbcrlf & vbcrlf & " Lists or runs an explorer verb (right click menu) on a file or folder" & vbcrlf & vbcrlf & " ShVerb <filename> [verb]" & vbcrlf & vbcrlf & " Used without a verb it lists the verbs available for the file or folder" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & " The program lists most verbs but only ones above the first separator" & vbcrlf & " of the menu work when used this way" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & " The Properties verb can be used. However the program has to keep running" & vbcrlf & " to hold the properties dialog open. It keeps running by displaying" & vbcrlf & " a message box."
Set objShell = CreateObject("Shell.Application")
Set Ag = WScript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If Ag.count = 0 then
wscript.echo " ShVerb - No file specified"
wscript.echo HelpMsg
wscript.quit
Else If Ag.count = 1 then
If LCase(Replace(Ag(0),"-", "/")) = "/h" or Replace(Ag(0),"-", "/") = "/?" then
wscript.echo HelpMsg
wscript.quit
End If
ElseIf Ag.count > 2 then
wscript.echo vbcrlf & " ShVerb - To many parameters" & vbcrlf & " Use quotes around filenames and verbs containing spaces" & vbcrlf
wscript.echo HelpMsg
wscript.quit
End If
If fso.DriveExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetFileName(Ag(0)))
' Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
Set objFolderItem = objFolder.self
msgbox ag(0)
ElseIf fso.FolderExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
ElseIf fso.fileExists(Ag(0)) = True then
Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
Else
wscript.echo " ShVerb - " & Ag(0) & " not found"
wscript.echo HelpMsg
wscript.quit
End If
Set objVerbs = objFolderItem.Verbs
'If only one argument list verbs for that item
If Ag.count = 1 then
For Each cmd in objFolderItem.Verbs
If len(cmd) <> 0 then CmdList = CmdList & vbcrlf & replace(cmd.name, "&", "")
Next
wscript.echo mid(CmdList, 2)
'If two arguments do verbs for that item
ElseIf Ag.count = 2 then
For Each cmd in objFolderItem.Verbs
If lcase(replace(cmd, "&", "")) = LCase(Ag(1)) then
wscript.echo Cmd.doit
Exit For
End If
Next
'Properties is special cased. Script has to stay running for Properties dialog to show.
If Lcase(Ag(1)) = "properties" then
WSHShell.AppActivate(ObjFolderItem.Name & " Properties")
msgbox "This message box has to stay open to keep the " & ObjFolderItem.Name & " Properties dialog open."
End If
End If
End If

Sending input to a cmd window using vbscript

I am trying to automate my process. I need to ask the user for the computer name ex: SH010123 store that as a variable. start cmd prompt. type psexec\\"computername" cmd then I need to type net use t: \\network\path password /user:domain\username and finally type \\network\path\"my file.bat"
I cant figure out how to use vbscript to send those inputs to a cmd window.
Although the solution may be implemented bypassing sending input to a cmd window, here is an example how to send the command to cmd and get back some output back:
Dim oShell, oExec
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.exec("%comspec%")
oExec.StdIn.Write "dir" & vbCrLf
oExec.StdIn.Write "cd c:\" & vbCrLf
oExec.StdIn.Write "dir" & vbCrLf
oExec.StdIn.Write "exit" & vbCrLf
ShowInNotepad oExec.StdOut.readall
Sub ShowInNotepad(strTest)
Dim TempPath
With CreateObject("Scripting.FileSystemObject")
TempPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%") & "\" & .GetTempName
With .CreateTextFile(TempPath, True, True)
.WriteLine(strTest)
.Close
End With
CreateObject("WScript.Shell").Run "notepad.exe " & TempPath, 1, True
.DeleteFile(TempPath)
End With
End Sub

Passing parameters from batch to vba and get output value back in batch

I'm making a utility using QuickTest Professional which calls a batch file with some parameters.
This batch file further calls a vbscript and passes some parameters to this vbs file.
This vbs file perform operations and generate a number.
I want this number to flow back from vbs to batch and then to QTP.
This is what I've figured out so far:
QTP(sending parameters to bat) >> Batch(sending parameters to vbs) >> VBS (generates a number)
now I want this vbs to return the output number back
VBS >> Batch(same bat which called vbs file) >> QTP(same qtp process which called this batch)
Here is my code:
QTP: (calling batch)
Dim BatchRun
Set BatchRun = CreateObject ("WSCript.shell")
invokefile= Chr(34) + "C:/invokebugz.bat" + Chr(34)
BatchRun.Run invokebugzfile & lob & " " & mailto & " " & mailcc & " " & title & " " & subject
Bat: (calling vbs)
cd C:\
cscript abc.vbs "%~1" "%~2" "%~3" "%~4" "%~5"
vbs:
Set args = Wscript.Arguments ' to accept command line arguments
xprod = args(0)
mailto = args(1)
mailcc = args(2)
xtitle = args(3)
xcomment = args(4)
You can get the value back if you just print it to standard output. So just write the result from VBS like this:
Wscript.Echo result
The for command can be used to get the output of a command that you invoke:
for /f %%a in ('cscript abc.vbs "%~1" "%~2" "%~3" "%~4" "%~5"') do (
echo The output is %%a
)
If the output is just a number, you shouldn't need to add any extra options to the for loop. Try running for /? for more help.

How to pass parameters to batch file using QTP?

I've a batch script which calls a vbs file to do some operations on a web page. From this batch file, I pass 4 parameters(string) to vbs file.
Now I'm looking to integrate this with my QTP framework.
So I need to call this batch file from framework and I'll pass these parameters from QTP to Batch file which batch file will further pass to vbs code.
Here is what I'm trying:
QTP Code:
Dim BatchRun
Set BatchRun = CreateObject ("WSCript.shell")
lob=DataTable("LOB",IntSheetNo-1)
mailto=DataTable("EmailTo",IntSheetNo-1)
mailcc=DataTable("EmailCC",IntSheetNo-1)
BatchRun.Run "C:\invoke.bat " & lob & " " & mailto & " " & mailcc
Set BatchRun = Nothing
Batch Code:
C:
cscript kamal.vbs %1 %2 %3
vbs code :
Set args = Wscript.Arguments ' to accept command line arguments
xprod = args(0)
mailto=args(1)
mailcc=args(2)
And I used these for some operations through vbs.
I did search on google and stackoverflow to find spome examples but none worked for me so far.
Not sure what QTP is, and assuming DataTable(....) returns a string, your problem probably is the spaces in data. When calling a batch file, parameter separation is determined by spaces. If there are any space in lob, mailto or mailcc, arguments are not correctly parsed. You need to wrap each of the arguments in quotes (and ensure inner quotes in arguments are escaped to no interfere)
So, QTP
lob= Chr(34) + Replace(DataTable("LOB",IntSheetNo-1), Chr(34), Chr(34)+Chr(34)) + Chr(34)
mailto=Chr(34) + Replace(DataTable("EmailTo",IntSheetNo-1), Chr(34), Chr(34)+Chr(34)) + Chr(34)
mailcc=Chr(34) + Replace(DataTable("EmailCC",IntSheetNo-1), Chr(34), Chr(34)+Chr(34)) + Chr(34)
Dim BatchRun
Set BatchRun = CreateObject ("WSCript.shell")
BatchRun.Run "C:\invoke.bat " & lob & " " & mailto & " " & mailcc
Set BatchRun = Nothing
Batch Code
C:
cscript kamal.vbs "%~1" "%~2" "%~3"
And VBS code without changes

How to call Run() with parameters

I've got this working line of code in Windows Batch
start "" /wait /i "C:\Program Files\Sandboxie\Start.exe" /box:NetBeans /wait "C:\Program Files\NetBeans 7.3\bin\netbeans64.exe"
I would like to run it via VBScript. But I don't know how to pass the path in parameter which has a space inside.
I came up with something like this:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run("C:\Program Files\Sandboxie\Start.exe" /box:NetBeans /wait "C:\Program Files\NetBeans 7.3\bin\netbeans64.exe"), 1, True
But there is an error:
expected: ')'
Within a literal string, a single double-quote character is represented by two double-quote characters. So try the following instead:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run """C:\Program Files\Sandboxie\Start.exe"" /box:NetBeans /wait ""C:\Program Files\NetBeans 7.3\bin\netbeans64.exe""", 1, True
Set objShell = Nothing
I like to use the following system to embed quotes :
strCommand = Quotes("C:\Program Files\Sandboxie\Start.exe") & _
" /box:NetBeans /wait " & _
Quotes("C:\Program Files\NetBeans 7.3\bin\netbeans64.exe")
Function Quotes(ByVal strValue)
Quotes = Chr(34) & strValue & Chr(34)
End Function
It's a lot easier to read.

Resources