Sunday, October 11, 2009

Reboot Computers by Operating System

What if you wanted to reboot every computer in your Active Directory domain that has a specific version of Windows installed?  One example is requesting every Windows 7 Ultimate computer to reboot (see below).  Just replace the “YOUR-DOMAIN” with your actual NetBIOS domain name and change the operating system string to whatever you prefer.  Remember to test in an isolated environment.

Set objDomain = GetObject("WinNT://YOUR-DOMAIN")

For each objX in objDomain
If Lcase(objX.Class) = "computer" Then
n = objX.Name
wscript.echo n
End If
Next

Sub Reboot(strComputer)
Dim objWMIService, colOS, objShare, objRetval, cap
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
If err.Number <> 0 Then
wscript.echo "unavailable: " & strComputer
Else
Set colOS = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
For each objItem in colOS
cap = objItem.Caption
If cap = "Microsoft Windows 7 Ultimate" Then
Set objShare = objWMIService.Get("Win32_OperatingSystem.ReplaceKeyProperty=ReplacePropertyValue")
Set objRetval = objWMIService.ExecMethod("Win32_OperatingSystem.ReplaceKeyProperty=ReplacePropertyValue", "Reboot")
End If
Next
End If
End Sub

No comments:

Post a Comment