Link to home
Start Free TrialLog in
Avatar of ntr2def
ntr2def

asked on

VB script to run net view command

VB script to enumerate all DC's in my environment and run net view against them if one fails i want it to create a custom event ID with the DC name that failed. Any help would be cgreatly appreaciated
Avatar of prashanthd
prashanthd
Flag of India image

Try the following, it will create an application error event 100 NetViewTest, if any error while executing
On Error Resume Next

Dim objRootDSE, strDNSDomain, adoConnection, adoCommand, strQuery
Dim strFilter, strAttributes, adoRecordset, strDN, strDNSHostName
Dim strName, strBase

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

' Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on DC's.
strFilter = "(&(objectCategory=computer)" _
& "(userAccountControl:1.2.840.113556.1.4.803:=8192))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

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

Set adoRecordset = adoCommand.Execute

Do Until adoRecordset.EOF
    ServerName = adoRecordset.Fields("sAMAccountName").Value
    ServerName = Left(ServerName, Len(ServerName) - 1)

    WScript.Echo "Domain Controller: " & ServerName
    
    'Net View Test
    strout=""
    
    Set objShell = CreateObject("Wscript.Shell")
    dcocommand = "NET VIEW \\"& ServerName 
    
    Set objExec = objShell.Exec(dcoCommand) 
    
    strout=objExec.StdOut.ReadAll()
    
    'wscript.echo strout   
    If InStr(LCase(strout),"error")> 0 Then
    	strout=servername & vbCrLf & strout            
        strCommand = "eventcreate /T Error /ID 100 /so NetViewTest /L Application /D " & _
        Chr(34) & strout & Chr(34)
        'objShell.Run strcommand
	wscript.echo "error"
    End If
    
    adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

WScript.Echo "Done"

Open in new window

Avatar of ntr2def
ntr2def

ASKER

this seems to be looking at a single domain, i guess i should have also said i have a multi-domain environment with a empty root domain so i would need to enumerate throughout the entire forest also hitting the child domains.
ASKER CERTIFIED SOLUTION
Avatar of prashanthd
prashanthd
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial