2013-12-08 188 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