このエントリーをはてなブックマークに追加
[`livedoor` not found]

WSHを使用して、OSの起動時間と起動してからの経過時間を表示する例です。
WMIで各値を取得するのですが、UTCで値が返ってきますので、このサイトで紹介した
UTCを変換する関数を利用しています。

[vb]
Option Explicit

Call Uptime()

‘===========================================================================

Sub Uptime()

Dim OBJ_WMIService
Dim OBJ_Item
Dim COL_Item

Dim STR_Computer
Dim STR_NameSpace

Dim STR_BootUpTime
Dim STR_LocalTime
Dim STR_Work
Dim STR_Day
Dim STR_Hour
Dim STR_Min

STR_Computer = “.”
STR_NameSpace = “/root/cimv2”

Set OBJ_WMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & STR_Computer & STR_NameSpace)

If Err.Number <> 0 Then

WScript.Echo “Error=” & Err.Number
WScript.Echo “ERROR Code=” & Err.Number & ” Description=” & Err.Description
Err.Clear
Exit Sub

End If

Set COL_Item = OBJ_WMIService.ExecQuery(“SELECT * FROM Win32_OperatingSystem”)

For Each OBJ_Item In COL_Item

STR_BootUpTime = OBJ_Item.LastBootUpTime
STR_LocalTime = OBJ_Item.LocalDateTime

Next

STR_BootUpTime = ConvertUTCToDate(STR_BootUpTime)
STR_LocalTime = ConvertUTCToDate(STR_LocalTime)

STR_Work = DateDIff(“n”, STR_BootUpTime, STR_LocalTime)

STR_Day = Fix((STR_Work / 60 ) / 24)
STR_Hour = (STR_Work / 60) Mod 24
STR_Min = STR_Work Mod 60

WScript.Echo “起動時刻: ” & STR_BootUpTime
WScript.Echo “経過時間: ” & STR_Day & ” 日 ” & STR_Hour & ” 時間 ” & STR_Min & ” 分”

End Sub

‘===========================================================================

Function ConvertUTCToDate(STR_DateTime)

ConvertUTCToDate = CDate(Mid(STR_DateTime, 5, 2) & “/” & _
Mid(STR_DateTime, 7, 2) & “/” & Left(STR_DateTime, 4) & ” ” & _
Mid(STR_DateTime, 9, 2) & “:” & _
Mid(STR_DateTime, 11, 2) & “:” & _
Mid(STR_DateTime, 13, 2))

End Function

[/vb]

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Leave a Reply