Ping応答があったら再起動を実行するスクリプトです。各端末の管理者権限を持つアカウントを作成してタスクスケジュールすれば一台の管理サーバから集中して再起動が管理出来ます。
動作確認はWindows XP、Windows 2003のみです。
Cscript ping2reboot.vbs PC名
こんな感じで実行します。
'*Ping応答があったら再起動を実行するスクリプト '*Ver 1.0 '*実行例:Cscript ping2reboot.vbs PC名 Option Explicit Dim objArgs Dim strComputer Set objArgs = WScript.Arguments If objArgs.count = 1 Then strComputer = objArgs.item(0) Else Wscript.Echo "引数が不正です。" & vbCrLf & "実行例:Cscript ping2reboot.vbs PC名" Wscript.Quit '終了 End If If PingResult(strComputer) = True Then Wscript.Echo objArgs.item(0) & "のPing応答ありました。再起動を開始します。" Call ShutDownWindows(strComputer) Else Wscript.Echo "応答無いです、端末落ちてるかも。" End If Wscript.Echo "スクリプト終了" Set objArgs = Nothing '------------------------------------------------------------- Function PingResult(strComputer) 'strComputerにpingを行って成功したらPingResultにTrueを返す Dim objWMIService Dim colItems Dim objItem Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_PingStatus " & _ "Where Address = '" & strComputer & "'") For Each objItem in colItems If objItem.StatusCode = 0 Then PingResult = True Else PingResult = False End If Next Set objWMIService = Nothing Set colItems = Nothing End Function Function ShutDownWindows(strComputer) On error resume Next Const LogOff = 0 'ログオフ Const REBOOT = 2 '再起動 Const SHUTDOWN = 8 'シャットダウン Dim objWMIService Dim colOperatingSystems Dim objOperatingSystem Set objWMIService = GetObject("winmgmts:{impersonationLevel= impersonate,(Shutdown)}\\" & strComputer & "\root\cimv2") Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") For Each objOperatingSystem in colOperatingSystems objOperatingSystem.Win32Shutdown(REBOOT) Next Set objWMIService = Nothing Set colOperatingSystems = Nothing Call ErrorCheck End Function Sub ErrorCheck If Err <> 0 Then WScript.Echo "エラー番号:" & Err.Number & vbCrLf &_ Err.Description & vbCrLf &_ strDirPath ' WScript.Quit End If End Sub
>icemanさん
こんばんは、コメントありがとうございます。
単純に30秒待つという事なら、Sleepを利用すると良いと思います。
例えば以下のVBSを実行すると、「30秒後に30秒経過しました!」と表示されます。
WScript.Sleep(30000)
Wscript.Echo(“30秒経過しました!”)
今回のスクリプトの場合、20行目辺りにWScript.Sleep(30000)を追加すると30秒待ってからShutDownWindows()を呼ぶようになります。
※改めて見ると、ShutDownWindowsはFunctionではなくSubの方が適切でした。失礼しました。
VBSを参考にご利用させていただいてます。再起動する時間を30秒後などじする場合はどうしたら良いのでしょうか?