WMIを使ったリモートPCをシャットダウンするスクリプト

WMIを使ってリモートPCをシャットダウンさせるスクリプト。

objOperatingSystem.Win32Shutdown(REBOOT)

を変更する事で再起動やログオフも可能です。また、実行には対象PCに対する管理者権限が必要です。なお、動作確認はWindows XP SP2でのみです。

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
End Function

呼び出し方

Call ShutDownWindows("PC名")

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です