【问题标题】:.vbs script won't run batch / how to run batch silently.vbs 脚本不会运行批处理/如何静默运行批处理
【发布时间】:2013-04-25 17:49:28
【问题描述】:

我有一个想要静默运行的批处理文件。 所以,我正在四处寻找并想出以下代码...... invMybatchfile.vbs

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

此代码在我的桌面(Window 7 32 位)上运行,但由于某种原因,此脚本在服务器计算机上运行(Windows Server 2008 R2)。当我单击 invMybatchfile.vbs 时,dos 窗口会弹出并立即关闭,并且根本不执行 Mybatchfile.bat。 (如果我只是在服务器计算机上运行 Mybathfile.bat,它工作正常,所以批处理文件不是这里的问题。)

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

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

感谢您的帮助 JB

【问题讨论】:

    标签: windows vbscript batch-file windows-server-2008 wsh


    【解决方案1】:

    您的示例代码永远不会在任何机器上运行,因为您将变量名称声明为“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
    

    ...或者以这种方式将当前目录存储在 var 中:

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

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

    编辑:

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

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

    官网http://www.nirsoft.net/utils/nircmd2.html

    【讨论】:

      【解决方案2】:

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

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

      查看类似问题herehere 了解更多信息。

      【讨论】:

        【解决方案3】:

        您是否尝试过使用 RunAs?

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

        由于它适用于 Windows 7 但不适用于 Server 2008 R2,这对我来说听起来像是权限问题。

        【讨论】:

          猜你喜欢
          • 2011-06-15
          • 2013-07-30
          • 1970-01-01
          • 1970-01-01
          • 2020-11-17
          • 1970-01-01
          • 2021-09-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多