Windows 7 在后台运行脚本

Windows 7 在后台运行脚本,windows-7,vbscript,Windows 7,Vbscript,我想在后台在Windows7上按计划任务运行以下脚本。现在,脚本显示cmd窗口,我可以在没有可见cmd窗口的情况下运行脚本吗 Option Explicit Dim WshShell, oExec Dim RegexParse Dim hasError : hasError = 0 Set WshShell = WScript.CreateObject("WScript.Shell") Set RegexParse = New RegExp Set oExec = WshShell.Exe

我想在后台在Windows7上按计划任务运行以下脚本。现在,脚本显示cmd窗口,我可以在没有可见cmd窗口的情况下运行脚本吗

Option Explicit

Dim WshShell, oExec
Dim RegexParse
Dim hasError : hasError = 0

Set WshShell = WScript.CreateObject("WScript.Shell")
Set RegexParse = New RegExp

Set oExec = WshShell.Exec("%comspec% /c echo list volume | diskpart.exe")

RegexParse.Pattern = "\s\s(Volume\s\d)\s+([A-Z])\s+(.*)\s\s(NTFS|FAT)\s+(Mirror|RAID-5)\s+(\d+)\s+(..)\s\s([A-Za-z]*\s?[A-Za-z]*)(\s\s)*.*"

While Not oExec.StdOut.AtEndOfStream
    Dim regexMatches
    Dim Volume, Drive, Description, Redundancy, RaidStatus
    Dim CurrentLine : CurrentLine = oExec.StdOut.ReadLine

    Set regexMatches = RegexParse.Execute(CurrentLine)
    If (regexMatches.Count > 0) Then
        Dim match
        Set match = regexMatches(0)

        If match.SubMatches.Count >= 8 Then
            Volume      = match.SubMatches(0)
            Drive       = match.SubMatches(1)
            Description = Trim(match.SubMatches(2))
            Redundancy  = match.SubMatches(4)
            RaidStatus  = Trim(match.SubMatches(7))
        End If

        If RaidStatus <> "Healthy" Then
            hasError = 1
            'WScript.StdOut.Write "WARNING "
            MsgBox "Status of " & Redundancy & " " & Drive & ": (" & Description & ") is """ & RaidStatus & """", 16, "RAID error"
        End If
    End If
Wend

WScript.Quit(hasError)
非常感谢选项1-如果任务在您的用户凭据下运行,则msgbox将不可见

cmd窗口有两种可能的来源

a脚本本身。如果任务正在执行cscript,控制台窗口将可见,请避免调用wscript

b Shell.exec调用。隐藏此窗口的唯一方法是启动隐藏的调用脚本。在脚本开始时,测试是否存在某些参数。如果不存在,则使用WshShell对象的Run方法,并指示使用隐藏窗口运行脚本,使用参数调用脚本本身。脚本的第二个实例将以特殊参数开始,因此它将运行,但这次窗口将被隐藏

选项2-在系统凭据下运行任务

在这种情况下,将看不到任何窗口。所有这些都将在单独的会话中运行。但msgbox不会被看到。通过调用msg.exe更改MsgBox调用,并向当前控制台用户发送消息。

在使用Exec启动CMD实例时,不会这样做。