?
Solved

Report If Backup_Administrators Exist in Local Admins

Posted on 2011-03-18
3
Medium Priority
?
297 Views
Last Modified: 2012-05-11
I'm querying from a list of servers to report on whether or not the Backup_Administrator group exists within the Local Administrators group.

I'm also pulling the OU location of the server in ADUC, as well as the Last Logon Date. These are working fine, but BackUp_Admin check is always coming up false, even when it exists.
'Option Explicit

Dim strSourceFile, objExcel, objWorkbook, IntRow
Dim strMachine, strDomain, strBUAdmin
Dim objConnection, objCommand, objRecordSet
Dim strCategory, strTarget, strFilter
Dim strLocation, objMachine, objMemberOf, strResult, strOU, i, strOrgUnit
Dim arrRealAdmins, adminGroup, groupMember, ret


' Reference Section
strSourceFile = "C$\Server_input.xls"

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(strSourceFile)
IntRow = 1
strDomain = "dc=domain,dc=com"

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection


' Worker Section
Do Until objExcel.Cells(IntRow, 1).Value = ""
arrRealAdmins = Array("Backup_Administrators")
strMachine = objExcel.Cells(IntRow, 1).Value
WScript.Echo "Querying " & strMachine
strCategory = "(objectCategory=Computer)"
strTarget = "(cn=" & strMachine & ")"
strFilter = strCategory & strTarget

objCommand.CommandText = "<GC://" & strDomain & ">;(&" & strFilter & ")" & ";LastLogonTimeStamp,DistinguishedName;subtree"
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
 
  strLocation = objRecordSet.Fields("DistinguishedName")
  strOU = Split(strLocation,",")
  
    For i = LBound(strOU) To UBound(strOU)
      If InStr(strOU(i), "_") > 0 Then
        strOrgUnit = mid(strOU(i), 4)
      Else
      End If
    Next
  
  Set objMachine = GetObject("LDAP://" & strLocation )'  
    
  strDate = objRecordSet.Fields("LastLogonTimeStamp")
  Set lngDate = strDate
  strDate = #1/1/1601#
  
  If (lngDate.HighPart = 0) And (lngDate.LowPart = 0 ) Then
    strDate = #1/1/1601#
  Else
    strDate = #1/1/1601# + (((lngDate.HighPart * (2 ^ 32)) + lngDate.LowPart)/600000000)/1440
  End If
  strDateOnly = Split(strDate," ")
   
  Call IsBUAdminPresent(strBUAdmin,MemberName)
     
  ' Output Section within loop
  'WScript.Echo objMachine.Name
	  If strBUAdmin = True Then
	  	  objExcel.Cells(IntRow, 2).Value = "Yes"
	      objExcel.Cells(IntRow, 3).Value = strDateOnly 
		  objExcel.Cells(IntRow, 4).Value = objRecordSet.Fields("DistinguishedName")
	  ElseIf strBUAdmin = False Then
	  	  objExcel.Cells(IntRow, 2).Value = "No"
	      objExcel.Cells(IntRow, 3).Value = strDateOnly 
		  objExcel.Cells(IntRow, 4).Value = objRecordSet.Fields("DistinguishedName")
	  End If
  objRecordSet.MoveNext
  
Wend

IntRow = IntRow + 1

Loop

Function IsBUAdminPresent(strBUAdmin, MemberName)
	dim i
	for i = lbound(arrRealAdmins) to ubound(arrRealAdmins)
		if ucase(MemberName) = ucase(arrRealAdmins(i)) then
			strBUAdmin = True
			exit Function
		end If
	Next
	strBUAdmin = False
end Function

objConnection.Close
objExcel.Quit

Open in new window

0
Comment
Question by:JB4375
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Accepted Solution

by:
prashanthd earned 2000 total points
ID: 35170241
Hi,

The code for checking the group was missing.

Try the following code

regards
Prashanth
'Option Explicit
On Error Resume Next

Dim strSourceFile, objExcel, objWorkbook, IntRow
Dim strMachine, strDomain, strBUAdmin
Dim objConnection, objCommand, objRecordSet
Dim strCategory, strTarget, strFilter
Dim strLocation, objMachine, objMemberOf, strResult, strOU, i, strOrgUnit
Dim arrRealAdmins, adminGroup, groupMember, ret


' Reference Section
strSourceFile = "C$\Server_input.xls"

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(strSourceFile)
IntRow = 1
strDomain = "dc=domain,dc=com"

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection


' Worker Section
Do Until objExcel.Cells(IntRow, 1).Value = ""
    arrRealAdmins = "Backup_Administrators"
    strMachine = objExcel.Cells(IntRow, 1).Value
    
    WScript.Echo "Querying " & strMachine
    strCategory = "(objectCategory=Computer)"
    strTarget = "(cn=" & strMachine & ")"
    strFilter = strCategory & strTarget
    
    objCommand.CommandText = "<GC://" & strDomain & ">;(&" & strFilter & ")" & ";LastLogonTimeStamp,DistinguishedName;subtree"
    Set objRecordSet = objCommand.Execute
    
    While Not objRecordSet.EOF
        
        strLocation = objRecordSet.Fields("DistinguishedName")
        strOU = Split(strLocation,",")
        
        For i = LBound(strOU) To UBound(strOU)
            If InStr(strOU(i), "_") > 0 Then
                strOrgUnit = Mid(strOU(i), 4)
            Else
            End If
        Next
        
        Set objMachine = GetObject("LDAP://" & strLocation )'  
        
        strDate = objRecordSet.Fields("LastLogonTimeStamp")
        Set lngDate = strDate
        strDate = #1/1/1601#
        
        If (lngDate.HighPart = 0) And (lngDate.LowPart = 0 ) Then
            strDate = #1/1/1601#
        Else
            strDate = #1/1/1601# + (((lngDate.HighPart * (2 ^ 32)) + lngDate.LowPart)/600000000)/1440
        End If
        strDateOnly = Split(strDate," ")
        
        'Check for backup admin 
        strBUAdmin = False
        Set objGroup = GetObject("WinNT://" & strMachine & "/Administrators,group")
        
        For Each mem In objGroup.Members
            WScript.echo strMachine & " - " & mem.name
            If StrComp(LCase(mem.name),LCase(arrRealAdmins)) Then
                strBUAdmin = True
                Exit For
            End If            
        Next
        
        ' Output Section within loop
        'WScript.Echo objMachine.Name
        If strBUAdmin = True Then
            objExcel.Cells(IntRow, 2).Value = "Yes"
            objExcel.Cells(IntRow, 3).Value = strDateOnly 
            objExcel.Cells(IntRow, 4).Value = objRecordSet.Fields("DistinguishedName")
        ElseIf strBUAdmin = False Then
            objExcel.Cells(IntRow, 2).Value = "No"
            objExcel.Cells(IntRow, 3).Value = strDateOnly 
            objExcel.Cells(IntRow, 4).Value = objRecordSet.Fields("DistinguishedName")
        End If
        objRecordSet.MoveNext
        
    Wend
    
    IntRow = IntRow + 1
    
Loop

objConnection.Close
objExcel.Quit

Open in new window

0
 
LVL 15

Expert Comment

by:markdmac
ID: 35172123
Another way to check it:
Dim strFindName: strFindName = "Backup_Administrator"
Dim strTarget: strTarget = "."

WScript.Echo CheckAdmin(strTarget, strFindName)


Function CheckAdmin(strComputer, strAdmin)
	Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
	BackupPresent = False	
	If Err.Number = 0 Then
		For Each objUser In objGroup.Members
	        If UCase(objUser.Name) = UCase(strAdmin) Then
	        	BackupPresent = True
	        End If
	   	Next
	End If
	CheckAdmin = BackupPresent
End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:JB4375
ID: 35180382
Prashanthd,
That worked perfectly!!
Thanks,
JB
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question