• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

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
0
ntr2def
Asked:
ntr2def
  • 2
1 Solution
 
prashanthdCommented:
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

0
 
ntr2defAuthor Commented:
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.
0
 
prashanthdCommented:
Modified the script...try the following
On Error Resume Next

Set objRootDSE = GetObject("LDAP://rootDSE")
strConfigNC = objRootDSE.Get("configurationNamingContext")

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

strADsPath = "<LDAP://" & strConfigNC & ">;"
strFilter = "(objectClass=nTDSDSA);"
strAtts = "ADsPath;"
strScope = "subtree"

strQuery = strADsPath & strFilter & strAtts & strScope

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = strQuery
Set objRecordset = objCommand.Execute

' Iterate through the results
If objRecordset.Eof And objRecordSet.Bof Then
    WScript.Echo "No Domain Controllers were found"
Else
    While Not objRecordset.EOF
        Set objParent = GetObject(GetObject(objRecordset.Fields("ADsPath")).Parent)
        strADSPath = Replace(objRecordset.Fields("ADSPath"),"LDAP://","")
        'strDCFQDN = objParent.Get("dnsHostName")
        ServerName = objParent.Get("sAMAccountName")
        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
        
        objRecordset.MoveNext
    Wend
End If

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now