Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a VbScript from a Batch File without opening an additional command prompt

I have a VBScript file which I am trying to call from a Batch file. The following code I coped to a notepad and saved as MyScript.vbs

(http://gallery.technet.microsoft.com/scriptcenter/8bbed56f-a7aa-491f-a296-687dd96098a3#content)

    Const HIDDEN_WINDOW = 12 

    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:" _ 
               & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    Set objStartup = objWMIService.Get("Win32_ProcessStartup") 

    Set objConfig = objStartup.SpawnInstance_ 
    objConfig.ShowWindow = HIDDEN_WINDOW 
    Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process") 
    errReturn = objProcess.Create("Notepad.exe", null, objConfig, intProcessID) 

Created a batch file named Run.bat and added the below code in it.

    @echo off

    start "C:\\Users\\guest\\Desktop\\123\\MyScript.vbs"

When I try to execute the batch file through the command prompt, which opening another command prompt.

like image 412
JChan Avatar asked Dec 20 '22 18:12

JChan


1 Answers

rem This is the command line version
cscript "C:\Users\guest\Desktop\123\MyScript.vbs"

OR

rem This is the windowed version
wscript "C:\Users\guest\Desktop\123\MyScript.vbs"

You can also add the option //e:vbscript to make sure the scripting engine will recognize your script as a vbscript.

Windows/DOS batch files doesn't require escaping \ like *nix.

You can still use "C:\Users\guest\Desktop\123\MyScript.vbs", but this requires the user has *.vbs associated to wscript.

like image 69
Alvin Wong Avatar answered Jan 24 '23 11:01

Alvin Wong