2013-02-28 81 views

回答

2

使用主腳本通過.Exec啓動(子)腳本;監視exec對象的狀態屬性;記錄/顯示執行對象狀態更改爲WshFinished的時間。

1

我的建議是基於WMI。 (新鮮的想法下面滾動)

Option Explicit 

Dim objWMIService, colProcesses, objProcess 
Dim iCount, iLoop, sFileName 

Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" _ 
    & ".\root\cimv2") 

' wait all scripts to finish 
Do While True 
    WScript.Sleep 200 
    ' snapshot running scripts 
    Set colProcesses = objWMIService.ExecQuery(_ 
     "Select * From Win32_Process " _ 
     & "Where Name = 'WScript.exe' " _ 
     & "OR Name = 'CScript.exe'", , 48) 

    iCount = 0 
    For Each objProcess In colProcesses 
     ' skip current "monitor" script, test the rest 
     If InStr (objProcess.CommandLine, WScript.ScriptName) = 0 Then 
      sFileName = Split(objProcess.CommandLine, """")(3) 
      iCount = iCount + 1 
     End If 
    Next 
    If iCount < 1 Then Exit Do 
    iLoop = iLoop + 1 
Loop 

' and show what we get 
If iLoop > 0 Then 
    WScript.Echo "LastOne:" & vbNewLine & sFileName 
Else 
    WScript.Echo "No other .vbs running except Me" 
End If 

[編輯]好了,一個新的想法出現在我的腦海,現在,也許你會發現它很有趣,或者至少給它一個嘗試。

' do your work here... 
WScript.Sleep 3000 

Call SelfLogged 

Sub SelfLogged() 
    Const ForAppending = 8 
    With CreateObject("Scripting.FileSystemObject") 
     With .OpenTextFile(WScript.ScriptFullName, ForAppending) 
      .Write Chr(0) 
     End With 
    End With 
End Sub 

這個想法是通過在文件中附加一個字符來改變文件DateLastModified屬性。

2

基本日誌記錄將做的工作,在腳本開始和結束時,你也可以登錄時間,當寫日誌條目。您可以將結果寫入日誌文件。結果以秒爲單位。

startTime=timer 
wscript.echo "started at " & startTime 
'do your stuff' 
wScript.sleep 500 
stopTime=timer 
wscript.echo "stopped at " & StopTime & " duration was " & stopTime - startTime 

'started at 81558,17 
'stopped at 81558,67 duration was 0,5