2013-12-08 189 views
0

我想在后台运行Windows 7上的脚本作为计划任务。现在,脚本显示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) 

非常感谢

+0

不使用'Exec'来启动'CMD'实例。 –

回答

0

选项1 - 如果任务是在您的用户凭据运行(如果没有,MSGBOX将不可见)

有对CMD两个可能的来源窗口。

a)脚本本身。如果任务执行cscript,控制台窗口将是可见的,避免它调用wscript代替

二)Shell.exec调用。隐藏这个窗口的唯一方法是隐藏调用脚本。开始您的脚本测试以确定参数的存在。如果不存在,则使用WshShell对象的Run方法使脚本调用自变量,并指示使用隐藏窗口运行脚本。脚本的第二个实例将以特殊参数开始,因此它将运行,但这次窗口将被隐藏。

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

在这种情况下,没有窗口可见。所有将在一个单独的会话中运行。但msgbox不会被看到。拨打电话msg.exe更改MsgBox,并向当前控制台用户发送消息。

+0

太棒了,选项2适合我。谢谢 – Cicik