頼まれごとで作ったWindowsサービス停止起動VBSのスクリプト。
結局使われることはなかったけど、どこかの現場で使うかなと思いメモ。
Windowsサービス停止起動VBSのソース
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
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で作った感じですね(うろ覚え)。
著作権について
著作権は当サイトの管理者に帰属します。
商用利用以外であれば著作権フリーですが、念のためコメントにてお知らせください。