简体   繁体   English

.vbs脚本将无法批处理运行/如何以静默方式运行批处理

[英].vbs script won't run batch / how to run batch silently

I have a batch file that I want to run silently. 我有一个要静默运行的批处理文件。 So, i was goolging around and come up with following code... invMybatchfile.vbs 所以,我一直在闲逛,拿出下面的代码... invMybatchfile.vbs

Dim curPath
curPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN(sCurPath & "\Mybatchfile.bat", 0, True)

This code DOES work on my desktop (Window 7 32bit), but for some reason, this script does not work on server computer(Windows Server 2008 R2). 该代码在我的桌面(Window 7 32bit)上可以运行,但是由于某些原因,该脚本在服务器计算机(Windows Server 2008 R2)上不起作用。 When I click invMybatchfile.vbs, dos windows pops up and closes right away and Mybatchfile.bat is not executed at all. 当我单击invMybatchfile.vbs时,dos窗口立即弹出并关闭,并且Mybatchfile.bat根本不执行。 (If i just run Mybathfile.bat on server computer, it works fine, so batch file is not the problem here.) (如果我只是在服务器计算机上运行Mybathfile.bat,它可以正常工作,因此批处理文件不是这里的问题。)

So my question is, does anyone know why I am having this problem? 所以我的问题是,有人知道我为什么会遇到这个问题吗?

OR 要么

is there any other ways to launch batch file silently(not minimized, i know this way) with out using .vbs file or installing other software? 还有其他方法可以不使用.vbs文件或安装其他软件而以静默方式启动批处理文件(这种方法没有最小化)?

Thanks for your help JB 感谢您的帮助JB

The code of your example never will run in any machine because you are declaring the variable name as "curPath" and assign the vlaue to "CurPath" but you are trying to use it with the name "sCurPath" (And that variable doesn't exist on your code). 您的示例代码永远不会在任何计算机上运行,​​因为您将变量名称声明为“ curPath”,并将vlaue分配给“ CurPath”,但是您尝试将其与名称“ sCurPath”一起使用(而且该变量不会存在于您的代码中)。

Also you don't need to set the current working dir because when you launch a file, the shell searchs for the file in the working dir, so this is all the code what you need, in only one line: 另外,您不需要设置当前的工作目录,因为在启动文件时,shell会在工作目录中搜索文件,因此,这就是您需要的所有代码,仅需一行:

CreateObject("WScript.Shell").RUN "Mybatchfile.bat", 0, True

But if you are interested to store or to use the current working directory for any strange reason you have a method that returns the current dir: 但是,如果出于任何奇怪的原因而有兴趣存储或使用当前工作目录,则可以使用一种返回当前目录的方法:

.CurrentDirectory

Then you can use the method this way: 然后,您可以通过以下方式使用该方法:

Set Shell = CreateObject("WScript.Shell")
Shell.RUN Shell.CurrentDirectory & "\Mybatchfile.bat", 0, True

...Or store the current dir in a var this way: ...或通过以下方式将当前目录存储在var中:

Set Shell  = CreateObject("WScript.Shell")
CurDir = Shell.CurrentDirectory & "\"
Shell.RUN CurDir & "Mybatchfile.bat", 0, True

MSDN : http://msdn.microsoft.com/en-us/library/3cc5edzd%28v=vs.84%29.aspx MSDNhttp : //msdn.microsoft.com/en-us/library/3cc5edzd%28v=vs.84%29.aspx

EDIT: 编辑:

About running silent files, you can do it too using NirCMD commandline application. 关于运行静默文件,您也可以使用NirCMD命令行应用程序来执行。

NirCMD exec hide "Path to Batch File\Batch File.bat"

Official site : http://www.nirsoft.net/utils/nircmd2.html 官方网站http : //www.nirsoft.net/utils/nircmd2.html

I think you need to run it through cmd.exe: 我认为您需要通过cmd.exe运行它:

WshShell.Run("%COMSPEC% /c " & CurPath & "\Mybatchfile.bat", 0, True)

Check similar questions here and here for more information. 在此处此处检查类似的问题以获取更多信息。

Have you tried using RunAs? 您是否尝试过使用RunAs?

    cmds=WshShell.RUN("runas /noprofile /user:mymachine\administrator " _
    & sCurPath & "\Mybatchfile.bat", 0, True)

Since it works on Windows 7 but not on Server 2008 R2, It sounds like a permissions issue to me. 由于它可以在Windows 7上运行,但不能在Server 2008 R2上运行,所以这听起来像是我的权限问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM