1. ホーム
  2. スクリプト・コラム
  3. vbs

コンピュータのオン/オフ時間を問い合わせるためのvbsコード

2022-02-08 02:56:22

コアとなるコードです。

strComputer = ". "
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}! \\\" _
& strComputer & "\root\cimv2")
Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent " _
& "Where Logfile = 'System' And EventCode = '6005' Or EventCode = '6006'")
For Each objEvent In colLoggedEvents
  Flag = Flag + 1
  If Flag = 1 Then
    Wscript.Echo "This boot time: " & FormatWMIUTC(objEvent.TimeWritten)
  ElseIf Flag = 2 Then
    Wscript.Echo "Last shutdown time: " & FormatWMIUTC(objEvent.TimeWritten)
  ElseIf Flag = 3 Then
    Wscript.Echo "Last boot time: " & FormatWMIUTC(objEvent.TimeWritten)
    Exit For
  End If
End For

'FormatUTC
Function FormatWMIUTC(WMIDateString)
  DS = " // :: "
  FormatWMIUTC = Left(WMIDateString,2)
  For i = 2 To 7
    FormatWMIUTC = FormatWMIUTC & Mid(WMIDateString, i * 2 - 1, 2) & Mid(DS,i,1)
  Next
  'FormatWMIUTC = Mid(WMIDateString, 1, 4) & "year" _
  ' & Mid(WMIDateString, 5, 2) & "month" _
  ' & Mid (WMIDateString, 7, 2) & "day " _
  ' & Mid (WMIDateString, 9, 2) & "" _
  ' & Mid(WMIDateString, 11, 2) & ":" _
  ' & Mid(WMIDateString,13, 2)
End Function

上記のコードを接尾辞がvbsのファイルとして保存し、ダブルクリックして実行してみてください。今回の開始時刻と前回の切り替え時刻をご覧ください。