How to open webpage in VBS - vbscript

My code is
Set WshShell = CreateObject("WScript.shell")
for i = 0 to 50
WshShell.SendKeys(chr(175))
next
Process.Start("CMD", "/C start chrome.exe http://www.example.com")
It sets the volume to full, then opens chrome to example.com. But when I run it I get this error:
Cannot use parentheses while calling a Sub
How can I get it to raise the volume, and go to the webpage?

Try like this way :
Option Explicit
Dim URL,WshShell,i
URL = "www.yahoo.com"
Set WshShell = CreateObject("WScript.shell")
For i = 0 to 50
WshShell.SendKeys(chr(175))
Next
WshShell.run "CMD /C start chrome.exe " & URL & "",0,False

wshshell.run "www.youtube.com"

VBScript requires the CALL keyword when calling a sub with parenthesis. You can either write the code like this:
Set WshShell = CreateObject("WScript.shell")
For i = 0 To 50
WshShell.SendKeys Chr(175)
Next
Process.Start "CMD", "/C start chrome.exe http://www.example.com"
...or like this:
Set WshShell = CreateObject("WScript.shell")
For i = 0 To 50
Call WshShell.SendKeys(Chr(175))
Next
Call Process.Start("CMD", "/C start chrome.exe http://www.example.com")
Note: you don't get this error when calling a function and using its return value, like this:
Dim strTest
strTest = SomeFunction()
...because VBScript always requires the parenthesis when a function is used in an assignment.

This way works for me.
set wshshell = wscript.CreateObject("wscript.shell")
wshshell.run "chrome.exe https://stackoverflow.com"
You can change chrome.exe to whatever browser or the website to whatever you want

Related

Calling a executable from VBScript is not showing any response [duplicate]

I can run successfully test.vbs with syntax:
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
sEXE = """\\uncpath\file.exe"""
with CreateObject("WScript.Shell")
.Run sEXE & " ", 1, true ' Wait for finish or False to not wait
end with
however I want to store the output to \\\uncpath\%computername%.txt
This doesn't work:
sEXE = """\\uncpath\file.exe>>\\uncpath\%computername%.txt"""
with CreateObject("WScript.Shell")
.Run sEXE & " ", 1, true ' Wait for finish or False to not wait
end with
error with line: with CreateObject("WScript.Shell")
This doesn't work either.
sEXE = """\\uncpath\file.exe"""
with CreateObject("WScript.Shell")
.Run sEXE & " >>\\uncpath\%computername%.txt", 1, true ' Wait for finish or False to not wait
end with
any help?
The .Run() method doesn't have the ability to read the standard output from a task you use .Exec() for that, but you need a few changes to simulate the blocking that .Run() does for you automatically.
Dim WshShell, sEXE, cmd, result
Set WshShell = CreateObject("WScript.Shell")
sEXE = """\\uncpath\file.exe"""
With CreateObject("WScript.Shell")
Set cmd = .Exec(sEXE)
'Block until complete.
Do While cmd.Status <> 1
WScript.Sleep 100
Loop
'Get output
result = cmd.StdOut.Readall()
'Check the output
WScript.Echo result
Set cmd = Nothing
End With
The other approach is to prefix the sEXE variable so you are using cmd /c (as the >> command is part of that).
This should work
sEXE = "cmd /c ""\\uncpath\file.exe >> \\uncpath\%computername%.txt"""
With CreateObject("WScript.Shell")
.Run sEXE & " ", 1, true ' Wait for finish or False to not wait
End With
Useful Links
WshScriptExec Object (Returned by .Exec())
TextStream Object (Returned by .StdIn, StdOut and StdErr)

Issues trying to close a bat file from a vbs script

i'm trying to help my little brother with a vbs script file, i've never used vbs, and i'm having serious issues on finding out how to end a bat file that i've opened with the vbs script after 2 seconds
I've tried terminate but it doesn't work, even running another shell with taskkill and the name of process but nothing
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "C:\\Users\me\Desktop\Samples\t.bat"
Wscript.Sleep 2000`
I would like the bat file to close itself after 2 seconds
Use the Exec command instead of Run.
https://ss64.com/vb/exec.html
"Unlike .Run method, .Exec returns an object which returns additional information about the process started."
This example uses cmd.exe /k (the /k will keep the cmd.exe window open, which will be killed after your 2 second timeout even if your bat script logic finishes before that)
Dim shll : Set shll = CreateObject("WScript.Shell")
Set Rt = shll.Exec("cmd.exe /k C:\Temp\test.bat") : wscript.sleep 2000 :
Rt.Terminate
If you want to return the output of the bat script you will need to read this WScript.Shell.Exec - read output from stdout, and use logic similar to:
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "C:\Temp\test.bat"
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(strCommand)
Do While oExec.Status = 0
WScript.Sleep 1000
If Not oExec.StdErr.AtEndOfStream Then
vErrStr = vErrStr & oExec.StdErr.ReadAll
End If
If Not oExec.StdOut.AtEndOfStream Then
vOutStr = vOutStr & oExec.StdOut.ReadAll
End If
Loop
WScript.StdOut.Write(vErrStr)
WScript.Echo(vOutStr)
It all depends on what your bat file is doing really, and the reason you need to kill it after x seconds.
Edit:
Because your batch file is a continuous loop, it may confuse ReadAll of the output stream. You might be best using something such as (note that you will not see real-time output):
Dim strCommand : strCommand = "C:\Temp\test.bat"
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
'execute command
Dim oExec : Set oExec = WshShell.Exec(strCommand)
'wait 2 seconds
WScript.Sleep 2000
'terminate command
oExec.terminate
'get output
wscript.echo oExec.StdOut.ReadAll
Set oExec = Nothing
Set WshShell = Nothing

How to stop a specific script in VBScript

Is there a code to stop a specific script? I've seen a way that allows you to end all scripts using taskkill.
Option Explicit
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "taskkill /f /im Cscript.exe", , True
WshShell.Run "taskkill /f /im wscript.exe", , True
I simply want to end one specific script that is sleeping in a wscript.sleep not every script that is running at a time.
Try below. You need to preserve the ProcessId of the process you want to kill. Below code opens a notepad and kills it. This example shows you how to capture the ProcessId of the script when it was started and then kill it.
Private Sub KillTest()
Dim killCmd
Dim pid
Set WshShell = CreateObject("WScript.Shell")
Set EngineRun = WshShell.Exec("notepad")
pid = EngineRun.ProcessID
killCmd = "taskkill /pid " & CStr(pid)
Set EngineRun = WshShell.Exec(killCmd)
Set EngineRun = Nothing
Set WshShell = Nothing
End Sub

Google chrome - not launching from top and left 0 pixels

Google Chrome Stable i am using in Windows 8.1 and in Windows 8.1 Pro from the startup path as following:
timeout 5 > nul
ping 127.0.0.1 -n 11 > nul
#start /b cmd /c "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --kiosk http://www.stackoverflow.com
10% times on boot it works exactly as full-screen mode.
BUT, 90% times on boot it does not work, it launch the Chrome but it does not launch it from top, left 0 pixel
if i manually run that batch file then 100% time it works successfully, but when its automated then it start to mess
Can anyone please help? (i have been having this issue for about 3 months now, and its getting worst because cant find any solution yet to resolve this issue permanently)
I'm working on windows 7 32 bits, so i created this vbscript in order to create a shortcut in the startup folder and to launch the batch file in hidden mode.
Shortcut4Batch.vbs
Option Explicit
Dim PathApplication,ShortcutName,VbsPath
VbsPath = Wscript.ScriptFullName
PathApplication = "C:\Users\Hackoo\Desktop\Hackoo\BAT\stackoverflow\KioskChrome.bat"
ShortcutName = "KioskChrome"
Call Shortcut(VbsPath,ShortcutName)
Call Hidden_Run(Dblquote(PathApplication))
'*********************************************************************************
Sub Shortcut(PathApplication,ShortcutName)
Dim objShell,StartFolder,objShortCut,MyTab
Set objShell = CreateObject("WScript.Shell")
MyTab = Split(PathApplication,"\")
If ShortcutName = "" Then
ShortcutName = MyTab(UBound(MyTab))
End if
StartFolder = objShell.SpecialFolders("Startup")
Set objShortCut = objShell.CreateShortcut(StartFolder & "\" & ShortcutName & ".lnk")
objShortCut.TargetPath = Dblquote(PathApplication)
ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-25"
objShortCut.Save
End Sub
'*********************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************************
Function Hidden_Run(MyProgram)
Dim ws,Result
Set ws = CreateObject("wscript.Shell")
Result = ws.run(MyProgram,0,True) '0 to hide the program
Hidden_Run = Result
End Function
'*********************************************************************************
KioskChrome.bat
#echo off
#start /b cmd /c "%programfiles%\Google\Chrome\Application\chrome.exe" --kiosk http://www.stackoverflow.com

WScript.Shell and blocking execution?

I'm using WScript to automate some tasks, by using WScript.Shell to call external programs.
However, right now it does not wait for the external program to finish, and instead moves on. This causes issues because I have some tasks dependent on others finishing first.
I am using code like:
ZipCommand = "7za.exe a -r -y " & ZipDest & BuildLabel & ".zip " & buildSourceDir
Set wshShell = WScript.CreateObject("Wscript.Shell")
wshShell.run ZipCommand
Is there a way to do this so it blocks until the shell executed program returns?
Turns out, that while loop is severe CPU hog :P
I found a better way:
ZipCommand = "7za.exe a -r -y " & ZipDest & BuildLabel & ".zip " & buildSourceDir
Set wshShell = WScript.CreateObject("Wscript.Shell")
wshShell.Run ZipCommand,1,1
The last two arguments are Show window and Block Execution :)
If you use the "Exec" method, it returns a reference, so you can poll the "Status" property to determine when it is complete. Here is a sample from msdn:
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(ZipCommand)
Do While oExec.Status = 0
WScript.Sleep 100
Loop

Resources