【问题标题】:Writing an output of shellExecute command with arguments into a log file in VBScript将带有参数的 shellExecute 命令的输出写入 VBScript 中的日志文件
【发布时间】:2016-08-15 20:40:13
【问题描述】:

我已经尝试了很多方法来实现这一点,但还没有以我想要的方式成功。 我有一个 hta 应用程序,它从复选框中收集参数,然后运行一个 cmd 文件,在那里传递这些参数。 如果选中了日志复选框,我想创建该进程的日志文件而不创建任何新的包装文件。

我的逻辑是运行一个带有另一个参数的脚本,并将重定向参数作为参数。而且我无法正确使用语法(或者甚至可能在同一个文件中)。我的简化代码:

Sub RunCmd
dim shell
dim shellWithLog
dim command
dim ARGS
set shell = createobject("Shell.Application")
set shellWithLog = createobject("wscript.shell")

command = "gui.cmd"


if (checkbox1.checked) then
ARGS = ARGS + " Do_This"
end if

if (checkbox2.checked) then
ARGS = ARGS + " Do_That"
end if
if (logFile.checked) then
    shellWithLog.run (shell.shellExecute command, ARGS), " &>"  + "publishLog.log", 1
else
    shell.shellExecute command, , "runas", 1
end if
End Sub

这显然行不通,但至少显示了我想要实现的目标。

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    要在 VBScript 中连接字符串,使用 & 运算符。 + 运算符仅用于数字加法。

    Sub RunCmd
        dim shell, ARGS
    
        set shell = createobject("Shell.Application")
        ARGS = ""
    
        if checkbox1.checked then ARGS = ARGS & " Do_This"
        if checkbox2.checked then ARGS = ARGS & " Do_That"
        if logFile.checked then ARGS = ARGS & ARGS " > publishLog.log"
    
        ' ShellExecute(sFile, [vArgs], [vDirectory], [vOperation], [vShow])
        ' https://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx
    
        shell.shellExecute "gui.cmd", ARGS, , "runas", 1
    End Sub
    

    【讨论】:

    • 感谢您纠正运算符,即使它以前与 + 一起使用。在我的情况下,将日志记录作为参数与其他参数一起添加到一个空的日志文件中,因为它作为参数传递到 cmd 文件并在其中运行(我需要执行整个 cmd 文件的日志)。所以最后我要做的是: exec gui.cmd > publishLog.log 在我的 hta 文件中。 gui.cmd 中应该已经包含所有必要的参数。
    • 我不明白。我的回答还显示了如何重定向到日志文件。
    • 是的,确实如此。我以前也试过。这样我得到一个空的日志文件。
    • 谢谢!似乎最后语法有一些问题。现在可以使用了!
    • 这听起来像是一个损坏的命令行。注释掉 shell.shellExecute "gui.cmd", ARGS 并改用 WScript.Echo "gui.cmd " & ARGS。查看将生成的命令行。查找错误放置的引号、带有空格的未引用路径等。在脚本中修复这些问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2011-07-20
    • 2012-10-06
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多