頼まれごとで作ったWindowsサービス停止起動VBSのスクリプト。
結局使われることはなかったけど、どこかの現場で使うかなと思いメモ。
Windowsサービス停止起動VBSのソース
Option Explicit
Dim arrService '停止/起動するサービス名
Dim intErrCnt 'エラーカウント
Dim intLoopCnt 'ループカウンタ
Dim intRetryCnt 'サービス停止/起動リトライ回数
Dim intSleepTime 'サービス停止/起動スリープタイム
Dim objFSO 'ファイルシステムオブジェクト
Dim objScriptLog 'このツールのログ
Dim objService 'サービス名
Dim objServiceList 'サービス名取得用
Dim strSql 'SQL
Dim wshShell 'WSH
intRetryCnt = 5
intSleepTime = 15000
Set wshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objScriptLog = objFSO.OpenTextFile(wshShell.CurrentDirectory & "\Script.log", 8, true)
arrService = Array("Trend Micro Deep Security Agent","Trend Micro Deep Security Notifier")
Call subMain()
'##### メイン処理
Sub subMain()
objScriptLog.WriteLine "INF " & Date() & " " & Time() & " ----- サービスチェック処理開始 -----"
Call subCheckService()
objScriptLog.WriteLine "INF " & Date() & " " & Time() & " ----- サービスチェック処理終了 -----"
End Sub
'##### サービス確認関数
Sub subCheckService()
For intLoopCnt = 0 To UBound(arrService) Step 1
strSql = "Select * From Win32_Service Where DisplayName='" & arrService(intLoopCnt) & "'"
Set objServiceList = GetObject("winmgmts:").ExecQuery(strSql)
If objServiceList.Count = 1 Then
objScriptLog.WriteLine "INF " & Date() & " " & Time() & " " & arrService(intLoopCnt) & " サービスが登録されています。"
'自動起動確認
For Each objService In objServiceList
If objService.StartMode <> "Auto" Then
objScriptLog.WriteLine "ERR " & Date() & " " & Time() & " " & arrService(intLoopCnt) & " サービスが自動起動されません。"
End If
Next
Else
objScriptLog.WriteLine "ERR " & Date() & " " & Time() & " " & arrService(intLoopCnt) & " サービスが登録されていません。"
End If
Next
End Sub
'##### サービス停止関数
Sub subStopService()
For intLoopCnt = 0 To UBound(arrService) Step 1
strSql = "Select * From Win32_Service Where DisplayName='" & arrService(intLoopCnt) & "'"
Set objServiceList = GetObject("winmgmts:").ExecQuery(strSql)
intErrCnt = 0
Do While intErrCnt <= intRetryCnt
'停止処理
For Each objService In objServiceList
objService.StopService()
Next
WScript.Sleep(intSleepTime)
'停止確認
Set objServiceList = GetObject("winmgmts:").ExecQuery(strSql)
For Each objService In objServiceList
If objService.State = "Stopped" Then
objScriptLog.WriteLine "INF " & Date() & " " & Time() & " " & arrService(intLoopCnt) & " サービスの停止に成功しました。"
intErrCnt = 0
Exit Do
Else
intErrCnt = intErrCnt + 1
If intErrCnt > intRetryCnt Then
objScriptLog.WriteLine "ERR " & Date() & " " & Time() & " " & arrService(intLoopCnt) & " サービスの停止に失敗しました。"
Exit For
End If
End If
Next
Loop
Next
End Sub
'##### サービス開始関数
Sub subStartService()
For intLoopCnt = 0 To UBound(arrService) Step 1
strSql = "Select * From Win32_Service Where DisplayName='" & arrService(intLoopCnt) & "'"
Set objServiceList = GetObject("winmgmts:").ExecQuery(strSql)
intErrCnt = 0
Do While intErrCnt <= intRetryCnt
'開始処理
For Each objService In objServiceList
objService.StartService()
Next
WScript.Sleep(intSleepTime)
'開始確認
Set objServiceList = GetObject("winmgmts:").ExecQuery(strSql)
For Each objService In objServiceList
If objService.State = "Running" Then
objScriptLog.WriteLine "INF " & Date() & " " & Time() & " " & arrService(intLoopCnt) & " サービスの開始に成功しました。"
intErrCnt = 0
Exit Do
Else
intErrCnt = intErrCnt + 1
If intErrCnt > intRetryCnt Then
objScriptLog.WriteLine "ERR " & Date() & " " & Time() & " " & arrService(intLoopCnt) & " サービスの開始に失敗しました。"
Exit For
End If
End If
Next
Loop
Next
End Sub
Windowsサービス停止起動VBSの解説
net startコマンドで取得した内容をarrServiceに格納すれば動くはずです。
(確か)汎用性が高いようにbatの「net start」で情報を取るんじゃなくてSQL作ってwinmgmtsで実行してた(はず)。
VBSの方が制御しやすかった気がするのでVBSで作った感じですね(うろ覚え)。
著作権について
著作権は当サイトの管理者に帰属します。
商用利用以外であれば著作権フリーですが、念のためコメントにてお知らせください。
