Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I exclude Domain Controllers

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
Hi all,

I have a script that pings all of my servers and writes them to text files. How would I exclude domain controllers from being written to the files?

Thanks

Code:
'==========================================================================
' 
' NAME:		Get Servers.vbs	
' 
' AUTHOR:	Gene Magerr
' EMAIL:	genemagerr@hotmail.com
'
' COMMENT:
'
' VERSION HISTORY:
' 1.0   01/17/2008  Initial release
'
'==========================================================================
'Option Explicit

'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objShell, objNetwork, objFSO, TestMode

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FilesystemObject")

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
Dim objRootDSE, strDNSDomain, objConnection, objCommand, strQuery
Dim objRecordSet, strComputerDN, strOS

 'Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

 'Use ADO to search Active Directory for all computers.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

strQuery = "<LDAP://" & strDNSDomain & ">;(objectCategory=computer);name,operatingSystem;subtree"

objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute

If Not objFSO.FolderExists("PWChangeLogs") Then
objFSO.CreateFolder("PWChangeLogs")
End If 

If Not objFSO.FileExists("PWChangeLogs\PingSuccess.txt") Then
objFSO.CreateTextFile("PWChangeLogs\PingSuccess.txt")
End If

If Not objFSO.FileExists("PWChangeLogs\PingFail.txt") Then
objFSO.CreateTextFile("PWChangeLogs\PingFail.txt")
End If

Set strSuccess = objFSO.OpenTextFile("PWChangeLogs\PingSuccess.txt", 2)
Set strFail = objFSO.OpenTextFile("PWChangeLogs\PingFail.txt", 2)

 'Enumerate computer objects with server operating systems.
Do Until objRecordSet.EOF
  strOS = objRecordSet.Fields("operatingSystem")
  If InStr(UCase(strOS), "SERVER") > 0 Then
    strComputerDN = objRecordSet.Fields("name")
    If Ping(strComputerDN) Then    
    strSuccess.WriteLine strComputerDN
    Else
    strFail.WriteLine strComputerDN
    End If
    End If
  objRecordSet.MoveNext
Loop

 'Clean up.
objConnection.Close
strSuccess.Close
strFail.Close

Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
' Ping
' Function to Secretely call up the commnad shell and ping a server
' This is need for compatibility sake.  Currently only XP and 2003 server have built
' in WMI versions
' [URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1125863&page=1[/URL]
Function Ping(hostname)
    Set objShell = CreateObject("WScript.Shell")
    Ping = Not CBool(objShell.run("ping -n 1 " & hostname,0,True))
End Function

WScript.Echo "Done"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top