2013-02-28 83 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