Windows .vbs脚本赢得';t运行批处理/如何以静默方式运行批处理

Windows .vbs脚本赢得';t运行批处理/如何以静默方式运行批处理,windows,vbscript,batch-file,windows-server-2008,wsh,Windows,Vbscript,Batch File,Windows Server 2008,Wsh,我有一个批处理文件,我想安静地运行。 所以,我四处游荡,想出了以下代码。。。 invMybatchfile.vbs Dim curPath curPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") Set WshShell = CreateObject("WScript.Shell") cmds=WshShell.RUN(sCurPath & "\Mybatchfile.bat", 0, T

我有一个批处理文件,我想安静地运行。 所以,我四处游荡,想出了以下代码。。。 invMybatchfile.vbs

Dim curPath
curPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN(sCurPath & "\Mybatchfile.bat", 0, True)
此代码在我的桌面(Windows7 32位)上可以工作,但由于某些原因,此脚本在服务器计算机(WindowsServer2008R2)上不工作。当我单击invMybatchfile.vbs时,dos窗口会弹出并立即关闭,Mybatchfile.bat根本不会执行。(如果我只是在服务器计算机上运行Mybathfile.bat,它可以正常工作,所以批处理文件在这里不是问题。)

所以我的问题是,有人知道我为什么会有这个问题吗

是否有其他方法可以在不使用.vbs文件或安装其他软件的情况下以静默方式启动批处理文件(不是最小化,我知道这种方法)

谢谢你的帮助
JB

您是否尝试过使用RunAs

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

因为它可以在Windows7上运行,但不能在Server2008R2上运行,所以对我来说这听起来像是一个权限问题

您的示例代码永远不会在任何机器上运行,因为您将变量名声明为“curPath”,并将vlaue分配给“curPath”,但您尝试将其与名称“sCurPath”一起使用(并且该变量在您的代码中不存在)

此外,您不需要设置当前工作目录,因为当您启动文件时,shell会在工作目录中搜索该文件,因此这是您所需的全部代码,只需一行:

CreateObject("WScript.Shell").RUN "Mybatchfile.bat", 0, True
但是,如果您出于任何奇怪的原因对存储或使用当前工作目录感兴趣,您可以使用一个返回当前目录的方法:

.CurrentDirectory

然后您可以通过以下方式使用该方法:

Set Shell = CreateObject("WScript.Shell")
Shell.RUN Shell.CurrentDirectory & "\Mybatchfile.bat", 0, True
Set Shell  = CreateObject("WScript.Shell")
CurDir = Shell.CurrentDirectory & "\"
Shell.RUN CurDir & "Mybatchfile.bat", 0, True
…或以以下方式将当前目录存储在var中:

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

编辑:

关于运行静默文件,您也可以使用NirCMD命令行应用程序来运行静默文件

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

官方网站

我认为您需要通过cmd.exe运行它:

WshShell.Run("%COMSPEC% /c " & CurPath & "\Mybatchfile.bat", 0, True)
查看类似问题和以了解更多信息