windows 如何在不打开其他命令提示符的情况下从批处理文件调用 VbScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11690142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:43:53  来源:igfitidea点击:

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

windowsvbscriptbatch-file

提问by JChan

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

我有一个 VBScript 文件,我试图从批处理文件中调用它。以下代码我处理到记事本并保存为 MyScript.vbs

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

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

创建一个名为 Run.bat 的批处理文件并在其中添加以下代码。

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

当我尝试通过命令提示符执行批处理文件时,会打开另一个命令提示符。

回答by Alvin Wong

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

OR

或者

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

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

您还可以添加选项//e:vbscript以确保脚本引擎将您的脚本识别为 vbscript。

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

Windows/DOS 批处理文件不需要\像 *nix 那样转义。

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

您仍然可以使用"C:\Users\guest\Desktop\123\MyScript.vbs",但这需要用户已*.vbs关联到wscript

回答by David Candy

If you want to fix vbs associations type

如果要修复 vbs 关联类型

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.

此外,如果您由于管理原因无法使用 vbs,则将您的脚本转换为 vb.net 程序,该程序设计简单,易于使用,只需 5 分钟。

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.

所以编译器安装在所有安装了 .NET 的计算机上。

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.

请在此处查看有关如何制作 .NET exe 的文章。请注意,该示例适用于脚本主机。您不能使用它,您必须将您的 vbs 代码作为 .NET 代码放入。

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

如何将 VBScript 转换为可执行 (EXE) 文件?