作成 2009.12.29
更新 2009.12.29
更新 2009.12.29
サービスの状態をファイルに書き込むサンプルコード
サービスがいつの間にか停止しているらしいということで即興で書いたサンプルコードです。
Windows Server 2003 のタスク スケジューラーでも動作確認済み
Option Explicit
Const SAVE_DIR = "C:\temp\"
Const ForWriting = 2
Dim FSO
Dim objFile
Dim logtime
Dim wmiService
Dim objEnumerator
Dim objInstance
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(SAVE_DIR) Then FSO.CreateFolder(SAVE_DIR)
logtime = fGetTime()
Set objFile = FSO.OpenTextFile(SAVE_DIR & logtime & ".txt", ForWriting, True)
Set objEnumerator = wmiService.InstancesOf("Win32_Service")
For Each objInstance In objEnumerator
objFile.WriteLine objInstance.Caption & vbTab & objInstance.StartMode & vbTab &objInstance.State
Next
objFile.Close
Function fGetTime
Dim time, strMon, strDay, strHour, strMin, strSec
Dim tempTimer, strMilli
time = Now()
tempTimer = Timer
strMon = Right("0" & Month(time), 2)
strDay = Right("0" & Day(time), 2)
strHour = Right("0" & Hour(time), 2)
strMin = Right("0" & Minute(time), 2)
strSec = Right("0" & Second(time), 2)
strMilli = Right("000" & Int((tempTimer - Int(tempTimer)) * 1000), 3)
fGetTime = Year(time) & strMon & strDay & "-" & _
strHour & strMin & strSec & "." & strMilli
End Function