4

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.

John Conde
  • 217,595
  • 99
  • 455
  • 496
JChan
  • 1,411
  • 4
  • 24
  • 34

2 Answers2

11
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.

Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
  • when I execute MyScript.vbs in Win 764 bit system, I am getting the error Input Error: There is no script engine for file extension ".vbs". – JChan Jul 27 '12 at 14:49
  • @JChan I am seeing [this post](http://www.sevenforums.com/general-discussion/168356-problem-vbs-file-win-7-64-bit.html#post1637368), perhaps it may help. I can't test on any Windows 7 machines right now and this is XP. – Alvin Wong Jul 27 '12 at 14:52
  • OK thanks It worked on Win XP. I will try your Win7 64 bit solution. – JChan Jul 27 '12 at 15:00
  • @JChan - that is odd (no engine for .vbs). Have you tried explicitly specifying the engine via `//e:vbscript` option? – dbenham Jul 27 '12 at 15:32
  • @Alvin, There is no way to ensure that in the end user machine has .vbs associated to wscript. your solution is right, but because of the file association, I can't implement the solution. – JChan Jul 30 '12 at 20:57
  • @dbenham, can you please give some example ie, using //e:vbscript – JChan Jul 30 '12 at 20:57
  • @JChan - `wscript //e:vbscript "C:\\Users\\guest\\Desktop\\123\\MyScript.vbs"` – dbenham Jul 30 '12 at 21:24
  • @dbenham... your solution is working. you helped us... thanks you so much – JChan Jul 30 '12 at 21:59
0

If you want to fix vbs associations type

regsvr32 vbscript.dll
regsvr32 jscript.dll
regsvr32 wshext.dll
regsvr32 wshom.ocx
regsvr32 wshcon.dll
regsvr32 scrrun.dll

Also if you can't use vbs due to management then convert your script to a vb.net program which is designed to be easy, is easy, and takes 5 minutes.

Big difference is functions and subs are both called using brackets rather than just functions.

So the compilers are installed on all computers with .NET installed.

See this article here on how to make a .NET exe. Note the sample is for a scripting host. You can't use this, you have to put your vbs code in as .NET code.

How can I convert a VBScript to an executable (EXE) file?

Community
  • 1
  • 1
David Candy
  • 735
  • 5
  • 8