Showing posts with label adsi. Show all posts
Showing posts with label adsi. Show all posts

Sunday, April 15, 2012

Query AD Computers with Custom HOSTS File Entries

'****************************************************************
' Filename..: enum_ad_host_files.vbs
' Author....: David M. Stein
' Date......: 04/15/2012
' Purpose...: search for hosts files with custom entries
'****************************************************************
dns_netbios = "short_name_of_your_active_directory_domain"

Const ForReading = 1
Const ForWriting = 2

wscript.echo "info: scanning domain = " & dns_netbios

Set objDom = GetObject( "WinNT://" & dns_netbios )
Set objFSO = CreateObject("Scripting.FileSystemObject")

tcount = 0

For each obj in objDom
 If Lcase(obj.Class) = "computer" Then
  computerName = obj.Name
  wscript.echo "info: " & computerName
  tcount = tcount + 1
  CheckHosts computerName
 End If
Next

Sub CheckHosts(cn)
 Dim filename, objFile, strLine, found
 filename = "\\" & cn & "\admin$\system32\drivers\etc\hosts"
 wscript.echo "info: searching for: " & filename
 If objFSO.FileExists(filename) Then
  On Error Resume Next
  Set objFile = objFSO.OpenTextFile(filename, ForReading)
  If err.Number = 0 Then
   Do Until objFile.AtEndOfStream
    strLine = Trim(objFile.Readline)
    If Left(strLine,1) <> "#" And strLine <> "" Then
     found = True
    End If
   Loop
   objFile.Close
   
   If found = True Then 
    wscript.echo "info: custom entry found!"
   Else
    wscript.echo "info: no custom entries found."
   End If
  Else
   wscript.echo "fail: error (" & err.Number & ") = " & err.Description
  End If
   
 Else
  wscript.echo "fail: unable to locate hosts file on " & cn
 End If
End Sub

wscript.echo "info: " & tcount & " account objects found"

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