使用 Exec() 时隐藏命令提示符窗口 [英] Hide command prompt window when using Exec()

查看:65
本文介绍了使用 Exec() 时隐藏命令提示符窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行这个简单的测试脚本,但是在我执行脚本后出现了一个命令 shell 窗口.:

I'm trying to execute this simple test script, but a command shell window is appearing after I execute the script.:

Set objShell = WScript.CreateObject("WScript.Shell")

strCommand = "cmd /C tasklist"

Set objExecObject = objShell.Exec(strCommand)

wscript.echo "Test"

如何防止它出现?

更新

我能够通过此代码更改来改进它:

I was able to improve it with this code change:

strCommand = "cmd /C /Q tasklist"

现在窗口只显示一秒钟.但我根本不希望它出现.

Now the window only shows up for a split second. But I don't want it to show up at all.

推荐答案

使用 Exec(),您总是会得到一个窗口闪烁.您可以改用 Run() 在隐藏窗口中执行命令.但是您不能使用 Run() 直接捕获命令的输出.您必须将输出重定向到一个临时文件,然后您的 VBScript 才能打开、读取和删除该文件.

You're always going to get a window flash with Exec(). You can use Run() instead to execute the command in a hidden window. But you can't directly capture the command's output with Run(). You'd have to redirect the output to a temporary file that your VBScript could then open, read, and delete.

例如:

With CreateObject("WScript.Shell")

    ' Pass 0 as the second parameter to hide the window...
    .Run "cmd /c tasklist.exe > c:\out.txt", 0, True

End With

' Read the output and remove the file when done...
Dim strOutput
With CreateObject("Scripting.FileSystemObject")

    strOutput = .OpenTextFile("c:\out.txt").ReadAll()
    .DeleteFile "c:\out.txt"

End With

FileSystemObject 类具有类似 GetSpecialFolder() 检索 Windows 临时文件夹的路径和 GetTempName() 生成一个临时文件名,您可以使用它而不是像我上面所做的那样对输出文件名进行硬编码.

The FileSystemObject class has methods like GetSpecialFolder() to retrieve the path of Windows temp folder and GetTempName() to generate a temporary filename that you can use instead of hardcoding an output filename as I've done above.

另请注意,您可以将 /FO CSV 参数与 tasklist.exe 一起使用来创建 CSV 文件,这将使解析更容易.

Also note that you can use the /FO CSV argument with tasklist.exe to create a CSV file which should make parsing it much easier.

最后,还有 VBScript本机"方法来检索正在运行的进程列表.WMI 的 Win32_Process 例如,不需要 Run/Exec 就可以做到这一点.

Finally, there are VBScript "native" ways to retrieve the list of running processes. WMI's Win32_Process class, for example, can do this without the need for Run/Exec.

编辑:

为了完整起见,我应该提到您的脚本可以在隐藏的控制台窗口中重新启动,您可以在其中静默运行 Exec().不幸的是,这个隐藏的控制台窗口也会隐藏诸如 WScript.Echo() 之类的函数的输出.然而,除此之外,您可能不会注意到在 cscriptwscript 下运行脚本的任何差异.以下是此方法的示例:

For the sake of completeness, I should mention that your script can relaunch itself in a hidden console window where you can run Exec() silently. Unfortunately, this hidden console window will also hide your output from functions like WScript.Echo(). Aside from that, however, you probably won't notice any differences running your script under cscript vs wscript. Here's an example of this method:

' If running under wscript.exe, relaunch under cscript.exe in a hidden window...
If InStr(1, WScript.FullName, "wscript.exe", vbTextCompare) > 0 Then
    With CreateObject("WScript.Shell")
        WScript.Quit .Run("cscript.exe """ & WScript.ScriptFullName & """", 0, True)
    End With
End If

' "Real" start of script. We can run Exec() hidden now...
Dim strOutput
strOutput = CreateObject("WScript.Shell").Exec("tasklist.exe").StdOut.ReadAll()

' Need to use MsgBox() since WScript.Echo() is sent to hidden console window...
MsgBox strOutput  

当然,如果您的脚本需要命令行参数,则在重新启动脚本时也需要转发这些参数.

Of course, if your script expects command-line parameters, those would need to be forwarded when relaunching your script as well.

编辑 2:

另一种可能性是使用 Windows 剪贴板.您可以将命令的输出通过管道传送到 clip.exe 实用程序.然后,通过可以访问剪贴板内容的任意数量的可用 COM 对象检索文本.例如:

Yet another possibility is to use the Windows clipboard. You can pipe the output of your command to the clip.exe utility. Then, retrieve the text via any number of available COM objects that can access the contents of the clipboard. For example:

' Using a hidden window, pipe the output of the command to the CLIP.EXE utility...
CreateObject("WScript.Shell").Run "cmd /c tasklist.exe | clip", 0, True

' Now read the clipboard text...
Dim strOutput
strOutput = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")

这篇关于使用 Exec() 时隐藏命令提示符窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆