2008/5/15 木曜日

Ping応答があったら再起動を実行するスクリプト@VBS

Filed under: Windows XP, Windows Server 2003, スクリプト — pnpk @ 10:27:31

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

Popularity: 4%

コメント (0) »

この記事にはまだコメントがついていません。

コメント RSS トラックバック URI

コメントをどうぞ

HTML convert time: 0.496 sec. Powered by WordPress ME