I have a batch script MyBatch.bat, it successfully calls MyVbScript.vbs by the statement:
cscript //nologo %~dp0\MyVbScript.vbs %NewPort%
Where NewPort is a environment variable value.
MyVbScript.vbs is getting called successfully (tested) and value of NewPort is passed to VBS successfully (tested) from batch script.
But I am NOT able to generate the URL for launching in default browser. I want to generate the URL like http://localhost:7006/MyWebApplication where 7006 is value of NewPort variable.
MyVbScript.vbs contains:
Option Explicit
Dim wsh
Dim port
Dim myNum
Set wsh=WScript.CreateObject("WScript.Shell")
port = WScript.Arguments(0)
'MsgBox port//For testing
wsh.Run " "http://localhost:"&port&"/MyWebApplication" "
'End of VB script
How to generate the URL and launch it in default browser. I don't want to specify Chrome or Mozilla and IE because user may not have a particular browser.
The following worked:
wsh.Run "http://localhost:"&port&"/MyWebApplication"
Any seasoned programmer can suggest other code too if I have written more LOC unnecessarily.
Related
Can anyone help me with this problem, i need to call/execute bat file. below are my codes for bat file and asp classic.
test.bat
echo off
start C:\inetpub\wwwroot\Texting\Notification.lnk
That test.bat will run a shortcut and call the application, that will update received file.
sample.asp this code is for classic asp
<%
dim fs,tfile,loc,locadd,loctime1,nameFile,wshell
locadd = Month(Date)&Day(Date)&Year(Date)
loctime1 = stripNonNumeric(FormatDateTime(Now,3))
nameFile = "\\85new\Accts85new\Texting\Reply\Karing\"
loc = "\\85new\Accts85new\Texting\Reply\Karing\"&locadd&"_"&loctime1&".txt"
set fs= Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(loc)
For Each Item in Request.QueryString
tfile.Write Item &";"& Request.QueryString(Item)&"$&$"
next
tfile.close
set tfile=nothing
set fs=nothing
Function stripNonNumeric(inputString)
Set regEx = New RegExp
regEx.Global = True
regEx.Pattern = "\D"
stripNonNumeric = regEx.Replace(inputString,"")
End Function
set wshell = CreateObject("WScript.Shell")
wshell.Run "cmd.exe /c C:\inetpub\wwwroot\Texting\test1.bat"
set wshell = nothing
%>
that sample.asp will get the value of parameters from querystring.
The code is working, no error received when i hit ENTER on the browser.
Thank you in advance!
Typically this should be done in another way, but assuming you understand the security implications of calling bat file from a web application, you need to try to redirect output to a file to ensure it is executed and what is the output, i.e.
wshell.Run "test1.bat>debug.txt"
CreateObject("WScript.Shell") is executed in a separate process and you will not get any error in the browser, that's why your bat file needs to output its result somewhere.
I'm doing a mash between VbScript and CMD, i can call the VBScript easily with
cscript.exe //NoLogo "%~dp0TASK.vbs" >>"%~dp0output.txt"
But I need to disable the feature of users clicking on the VBScript and calling all sorts of errors, rather than it being called through a batch file.
My first attempt was a mess of setting a variable into a text file before i ran cscript.exe and use error handling in VBScript to tell if that variable could be collected, but it added too much time to the script.
Does VBScript have a way to tell whether it was started by CMD, or simply by double clicking, and able to act accordingly?
Here is a simple function, detecting the parent process caption. You can check if the process was started by CMD shell (the caption is cmd.exe) or by double-click (explorer.exe):
If LCase(GetParentProcessCaption()) <> "cmd.exe" Then WScript.Quit
' the rest part of your code here
Function GetParentProcessCaption()
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & CreateObject("WScript.Shell").Exec("rundll32 kernel32,Sleep").ProcessId & "'")
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
With GetObject("winmgmts:\\.\root\CIMV2:Win32_Process.Handle='" & .ParentProcessId & "'")
GetParentProcessCaption = .Caption
End With
End With
.Terminate
End With
End Function
In the context of your question another method allowing to pass parameters from CMD shell process to WSH script child process may be useful. It uses environment variable and WScript.Shell object. Consider the below example.
There is code for task.cmd file:
set myvar=myvalue
wscript "%~dp0task.vbs"
And for task.vbs file:
WScript.Echo CreateObject("WScript.Shell").Environment("process").Item("myvar")
I have got the output as follows:
Note, process environment variables are accessible for child processes only.
One way is for your VBS file to check for the presence of parameters and if they do not exist then stop the execution.
In your VBS script:
If WScript.Arguments.Count = 0 then
' No parameters provided. Can stop here.
End If
When you call your VBS file, just passing any parameter will satisfy the condition:
REM This will work.
cscript.exe //NoLogo "%~dp0TASK.vbs" "hello world"
REM So will this.
cscript.exe //NoLogo "%~dp0TASK.vbs" 1 2 3 4
REM This will not.
cscript.exe //NoLogo "%~dp0TASK.vbs"
This will not stop people from running it manually (with a parameter) or creating a shortcut which has a parameter. It would only really stop running the VBS directly (as a parameter will not be passed).
When you double click on a .vbs file, the action is determined by the following registry key:
Computer\HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command
If you were to change the key, you will be changing the double click action, but you will not be affecting your ability to launch the command explicitly via invoking cscript.exe directly.
If the bat file will keep the cmd.exe open while the vbs file runs, you can try to detect the cmd process inside the vbs file to continue execution.
Put this at the start of your vbs file:
Set shell = CreateObject("WScript.Shell")
list_str = shell.Exec("tasklist").stdOut.ReadAll 'get a list of processes by calling the windows program 'tasklist.exe'
If InStr(list_str, "cmd.exe") = 0 Then WScript.Quit 'quit if process is not found
Take the following script that I have:
x=msgbox ("Do you want to recycle the Premiere Pro Media Cache?" ,4, "Recycle Premiere Pro Media Cache")
If box =6 Then
CreateObject("wscript.shell").run "C:\Expedited\Scripts\PrMCRecycler1"
End If
My goal is to get this VBS file (which brings up a message box) to run the batch file (the same way it would run when double-clicking it) when the yes button is pressed. I'm not sure what I'm doing wrong above. When I click No, nothing needs to happen, so I didn't specify anything for it.
Basically, since it brings up a yes/no message box, I just need to make the yes button execute the specified batch file. I could really use some assistance in figuring out what's wrong. When I try the code listed above, nothing happens upon choosing yes (besides the dialogue box going away).
Try this example and change the path of your batch file.
Option Explicit
Dim ws,Question,PathProgram
Set ws = CreateObject("wscript.shell")
'change the path of your batch file
PathProgram = "C:\Program Files\Internet Explorer\iexplore.exe"
Question = Msgbox("Do you want to recycle the Premiere Pro Media Cache?",VbYesNO + VbQuestion, "Recycle Premiere Pro Media Cache")
If Question = VbYes Then
ws.run DblQuote(PathProgram)
End If
'***************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***************************************
I have a legacy application which doesn't support utilizing the default applications defined in windows which requires that I specify a specific an executable for a file format to be opened within the application. Since Microsoft no longer includes MODI with Office by default I have been looking at using launching Windows Picture Viewer for .TIF, .TIFF, & .BMP files since it is built into Windows; however Microsoft does not have a direct executable for Windows Picture Viewer which can be called forcing me to create a script which executes the command which calls for Windows Picture Viewer to execute. After research the only way I have been able to call the application to open a specific file is by creating a batch file such as below:
GIFTS.BAT
rundll32.exe C:\WINDOWS\system32\shimgvw.dll,ImageView_Fullscreen %~1
If I execute the above code such as GIFTS.BAT "C:\Example Directory\Sample File.tif" from a command prompt or from the application launches and the Sample File.tif opens without a problem; however a command prompt opens along with the file when launching from the application.
Upon which I tried to create a vbscript to hide the batch file from executing however I can't seem to pass my argument if the argument has a "space" within the argument.
GIFTS.VBS
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """C:\GIFTS.BAT"" " & WScript.Arguments.Item(0), 0
Set WshShell = Nothing
If I try to execute the VBScript from a command prompt such as GIFTS.VBS "C:\Example Directory\Sample File.tif" the application never launches and the command prompt returns no message or error. If I simplify the execute command (removing spaces to GIFTS.VBS "C:\Sample_File.tif" the application launches as well as the file Sample_File.tif is displayed and there is no command prompt displayed when the application executes the VBScript.
My question is how can I pass an argument into the VBS script that in return passes the the batch file when the argument contains spaces (which there is always going to be spaces since the argument will be a file name and path)?
There might be an easier approach to what I want to accomplish; however I am looking for a solution that Windows 7 - 8.1 can utilize with no additional software to install or manage on each workstation. The batch files works great I just need to be able to hide the command prompt that opens along with the application as my end users won't know what to do with it.
Thanks!
Sometimes, nested levels of escaping characters requires intimate knowledge of the undocumented behavior of CMD or some voodoo. Another way to attack the problem is to guarantee that you won't have any spaces in the name of the file. Windows has a concept of a short path (no spaces or special chars) for which every existing file has a unique one.
Here's a modified version of your program for which invoked subcommand doesn't need quotes around the file name.
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFile = fso.GetFile(WScript.Arguments.Item(0))
WshShell.Run """c:\GIFTS.BAT"" " & fsoFile.ShortPath, 0
Set WshShell = Nothing
You may wish to add your own error checking. The specified file must exist in order for the GetFile() command to succeed.
I'm working on setting up Host Scripts for Mokafive player on Windows. I've made a few VBSCripts and have worked with batch files trying to gather information on Mokafive Player start. I'm trying to capture data such as Firewall status, and Encryption status. I've tried multiple variations on gathering this data back to the Mokafive Management Server, such as echoing the results out in VBScrpt, Echoing the results back into the .bat file needed to run the VBScript, with no success.
Does anyone have any code, or examples of a working Host Script for Mokafive Player?
After meeting with several engineers it looks like the VBScript must echo out the results from each condition to an output file. This file will then be read by the launcher XML as a key value pair and parsed out to the database. so per each line that needs to be output you'd use the following...
Set objfs = CreateObject("Scripting.FileSystemObject")
outputfile = "outputfile.out
Set objFile = objfs.CreateTextFile(outputfile,True)
'Your Script Here
objFile.Write "Key=" & "Value" & vbNewLine