2016-12-28 101 views
0

我寫的VBScript代碼來監控系統開始和結束的時間,但它不是在同一天「這意味着不公開記錄,寫在同一日期的文件該如何解決這個我的VBscript代碼有什麼問題?

Dim objFS, objFile 
Dim crdate,ckdate,fname 
Set objFS = CreateObject("Scripting.FileSystemObject") 

do 
    crdate = Day(now) & "-" & Month(now) & "-" & Year(now) 
    fname = crdate & ".txt" 
    if (objFS.FileExists(fname)) Then 
     do 
      ckdate = Day(now) & "-" & Month(now) & "-" & Year(now) 
      objFile.WriteLine(Time & "\n") 
      wscript.sleep 300000 
     loop until ckdate <> crdate 
    else 
     Set objFile = objFS.CreateTextFile(fname) 
    end if 
loop 
+3

Windows已經自己監視啓動和關閉時間。檢查系統事件日誌中是否有源「eventlog」和事件ID 60xx的消息。 –

+0

@AnsgarWiechers我相信內核電源事件提供更準確的關於啓動/關閉w/12/13事件ID的描述。 –

+0

你的意思是Kernel-General,而不是Kernel-Power。 –

回答

0

我找到了答案在谷歌認爲被關閉的文件, objFile.Close

Dim objFS, objFile 
Dim crdate,ckdate,fname 
Set objFS = CreateObject("Scripting.FileSystemObject") 

do 
    crdate = Day(now) & "-" & Month(now) & "-" & Year(now) 
    fname = crdate & ".txt" 
    if (objFS.FileExists(fname)) Then 
     Set objFile = objFS.OpenTextFile(fname, 8, True) 
     objFile.WriteLine("-------------------------------") 
     do 
      ckdate = Day(now) & "-" & Month(now) & "-" & Year(now) 
      objFile.WriteLine(Time) 
      wscript.sleep 300000 
     loop until ckdate <> crdate 
     objFile.Close 
    else 
     Set objFile = objFS.CreateTextFile(fname) 
     objFile.Close 
    end if 
loop 
0

的Windows做到這一點本身......你不必推倒重來。您可以通過Windows事件本地或遠程更精確地獲取此信息。

PS C:\Users\steve> Get-WinEvent -Log System | where {($_.ID -eq "12" -or $_.ID -eq "13")} | format-table -property Message 

Message 
------- 
The operating system started at system time ?2017?-?01?-?01T00:51:20.610798500Z. 
The operating system is shutting down at system time ?2017?-?01?-?01T00:50:53.812034600Z.