vbscript change from parsing groups to OUs

I've got a vbscript (below) that parses AD groups.  For each member it tries to ping, if successful it tries to connect to the WMI DB, if successful it quieries for NIC properties.  It works very well, but I'm having trouble adapting it to look at an OU (and sub OUs) rather than a group.  

Thanks!

--------------------------------------------------------------------------------
on error resume next
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("D:\Data\Tools\Scripts\WMI-Reports\Reports\AdapterReport3-23-10.csv", ForAppending, True)

Const ADS_SCOPE_SUBTREE = 2

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

Set objCOmmand.ActiveConnection = objConnection
Set objGroup = GetObject _
      ("LDAP://CN=Comp-All,OU=Groups,DC=ourdomain,DC=domain,DC=com")

strName = objGroup.Get("name")
arrMemberOf = objGroup.GetEx("member")

      objTextFile.WriteLine "Date,Time,Hostname,MAC,IP,Adapter,Driver Version"
      objTextFile.WriteLine Date & "," & TIME & VbCrLf


For Each strMember in arrMemberOf
      strMember = Mid(strMember, 4, 330)
      arrGroup = Split(strMember, "," )

      Set objShell = CreateObject("WScript.Shell")
      Set objScriptExec = objShell.Exec( _
          "ping -n 2 -w 500 " & arrgroup(0))
      strPingResults = LCase(objScriptExec.StdOut.ReadAll)
      If InStr(strPingResults, "reply from") Then
          If InStr(strPingResults, "destination net unreachable") Then
            objTextFile.Write vbcrlf & "," & TIME & "," & arrgroup(0) & "," & "NO RESPONSE"
          Else
            'objTextFile.Write vbcrlf & "," & TIME & "," & arrgroup(0) & "," & "RESPONDED"

            Err.Clear
            Set objWMIService = GetObject("winmgmts:" _
             & "{impersonationLevel=impersonate}!\\" & arrgroup(0) & "\root\cimv2")

            If Err.Number <> 0 Then
                        objTextFile.Write  vbcrlf & "," & TIME & "," & arrgroup(0) & "," & "WMI FAILED"
            Else

                  Set colAdapters = objWMIService.ExecQuery _
                   ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True and DHCPEnabled=False")

                        For Each objAdapter in colAdapters
                            If Not IsNull(objAdapter.IPAddress) Then
                              For i = LBound(objAdapter.IPAddress) To UBound(objAdapter.IPAddress)
                                objTextFile.Write vbcrlf & "," & TIME & "," & arrgroup(0) & "," & objAdapter.MACAddress & "," & objAdapter.IPAddress(i) & "," & objAdapter.Description & "," & GetDriverVersion(objAdapter.Description)
                                'adapter = adapter & objAdapter.Description & vbCrLf & "    MAC address: " & objAdapter.MACAddress & vbCrLf & "    IP Address: " & objAdapter.IPAddress(i) & vbCrLf & "    Driver Version:  " & GetDriverVersion(objAdapter.Description) & vbCrLf & vbCrLf
                              Next
                            End If
                        Next

            End If

          End If
        Else
      objTextFile.Write vbcrlf & "," & TIME & "," & arrgroup(0) & "," & "NO PING RESPONSE"
        End If

Next

objTextFile.WriteLine VBcrlf & Date & "," & TIME
objTextFile.Write VBcrlf


Function GetDriverVersion(myAdapter)

       ON ERROR RESUME NEXT

       Set colDrivers = objWMIService.ExecQuery _
             ("select * from Win32_PnPSignedDriver where DeviceClass='NET'")

       For Each objDriver in colDrivers
             If left(myAdapter,len(objDriver.Description))=objDriver.Description then
                   drv=objDriver.DriverVersion
             End If
       Next

       GetDriverVersion=drv
End Function
--------------------------------------------------------------------------------
mbrombAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
Hi, to go through an OU, you would use the code below to start off the query:

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = "SELECT adsPath FROM 'LDAP://OU=SecondOU,OU=FirstOU,dc=My,dc=Domain,dc=com' WHERE objectCategory='user'"
 
Set objRecordSet = objCommand.Execute

objTextFile.WriteLine "Date,Time,Hostname,MAC,IP,Adapter,Driver Version"
objTextFile.WriteLine Date & "," & TIME & VbCrLf


While Not objRecordSet.EOF
strUserPath = objRecordSet("adsPath")
'.........
'.........
objRecordSet.MoveNext
Wend




But it's not clear how you're obtaining strMember and strGroup values, so you will need to have a look at the adspath in strUserPath and see if you can work with that.

Regards,

Rob.
0
 
mbrombAuthor Commented:
Sorry for the delayed reply.  I was distracted and will have to revisit this to test, but thank you for the info.
0
All Courses

From novice to tech pro — start learning today.