代码之家  ›  专栏  ›  技术社区  ›  Benjamin Jones

vbscript.run显示输出

  •  1
  • Benjamin Jones  · 技术社区  · 10 年前

    我可以成功运行测试。vbs语法:

    Dim WshShell
    Set WshShell = CreateObject("WScript.Shell")
    
    sEXE = """\\uncpath\file.exe"""
    with CreateObject("WScript.Shell")
      .Run sEXE & " ", 1, true ' Wait for finish or False to not wait
    end with
    

    但是,我想将输出存储到 \\\uncpath\%computername%.txt

    这不起作用:

    sEXE = """\\uncpath\file.exe>>\\uncpath\%computername%.txt"""
    with CreateObject("WScript.Shell")
      .Run sEXE & " ", 1, true ' Wait for finish or False to not wait
    end with
    

    第行出错:CreateObject(“WScript.Shell”)

    这也不管用。

    sEXE = """\\uncpath\file.exe"""
    with CreateObject("WScript.Shell")
      .Run sEXE & " >>\\uncpath\%computername%.txt", 1, true ' Wait for finish or False to not wait
    end with
    

    有什么帮助吗?

    1 回复  |  直到 10 年前
        1
  •  1
  •   user692942    10 年前

    这个 .Run() 方法无法读取您使用的任务的标准输出 .Exec() 为此,您需要进行一些更改来模拟 .运行() 自动为您执行。

    Dim WshShell, sEXE, cmd, result
    Set WshShell = CreateObject("WScript.Shell")
    
    sEXE = """\\uncpath\file.exe"""
    With CreateObject("WScript.Shell")
      Set cmd = .Exec(sEXE)
      'Block until complete.
      Do While cmd.Status <> 1
         WScript.Sleep 100
      Loop
      'Get output
      result = cmd.StdOut.Readall()
      'Check the output
      WScript.Echo result
      Set cmd = Nothing
    End With
    

    另一种方法是在 sEXE 变量,因此您正在使用 cmd /c (作为 >> 命令是其中的一部分) .

    这应该管用

    sEXE = "cmd /c ""\\uncpath\file.exe >> \\uncpath\%computername%.txt"""
    With CreateObject("WScript.Shell")
      .Run sEXE & " ", 1, true ' Wait for finish or False to not wait
    End With
    

    有用的链接