Sign in to follow this  
Followers 0
jazzyjeff

Create log files by security group

3 posts in this topic

Hello,

So what I am trying to do is create a csv file to log all the users the log into a PC on the network. It will tell me the date, time, user and the computer that they're using. What I would like to happen is depending on what security group the user is a member of, the user will be added to a particular log file.

I have been able to get the script to add users without using the _ADIsMemberOf function, but this would mean I would need to create a separate script for each security group. I am trying to achieve this with just 1 script.

So this is what does work:

#include <WindowsConstants.au3>

$TITLE = ("Username, Computer Name, Time, Date")
$Login = (@UserName & "," & @ComputerName & "," & @HOUR & ":" & @MIN & ":" & @SEC & "," & @MON & "/" & @MDAY & "/" & @YEAR)

If FileReadLine("\\app2\logs$\WSH\Student Log.csv", 1) = $TITLE Then
    FileWriteLine("\\app2\logs$\WSH\Student Log.csv", $Login)
Else
    FileWriteLine("\\app2\logs$\WSH\Student Log.csv", $TITLE)
    FileWriteLine("\\app2\logs$\WSH\Student Log.csv", $Login)
EndIf

What I'd like to get work is this:

#include <WindowsConstants.au3>
#include <ADFunctions.au3>

$TITLE = ("Username, Computer Name, Time, Date")
$Login = (@UserName & "," & @ComputerName & "," & @HOUR & ":" & @MIN & ":" & @SEC & "," & @MON & "/" & @MDAY & "/" & @YEAR)

If _ADIsMemberOf("Domain Admins","") Then
    Call ("DomainAdmins")
EndIf

If _ADIsMemberOf("Teachers","") Then
    Call ("Teachers")
EndIf

If _ADIsMemberOf("Students","") Then
    Call ("Students")
EndIf

If _ADIsMemberOf("Office","") Then
    Call ("Office")
EndIf

Func DomainAdmins()
    If FileReadLine("\\app2\logs$\WSH\Domain Admins Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH\Domain Admins Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH\Domain Admins Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH\Domain Admins Log.csv", $Login)
    EndIf
EndFunc

Func Teachers()
    If FileReadLine("\\app2\logs$\WSH\WSH - Teachers Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH\WSH - Teachers Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH\WSH - Teachers Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH\WSH - Teachers Log.csv", $Login)
    EndIf
EndFunc

Func Students()
    If FileReadLine("\\app2\logs$\WSH\WSH - Students Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH\WSH - Students Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH\WSH - Students Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH\WSH - Students Log.csv", $Login)
    EndIf
EndFunc

Func Office()
    If FileReadLine("\\app2\logs$\WSH\WSH - Office Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH\WSH - Office Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH\WSH - Office Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH\WSH - Office Log.csv", $Login)
    EndIf
EndFunc

The script executes, but I then receive the following error:

We Intercepted a COM Error!

Number is: 80072032

Windescription is: And invalid dn syntax has been specified.

Script Line number is: 1869

If anyone can help as to why this doesn't work I'd appreciate it.

Share this post


Link to post
Share on other sites



Ok, so I added the ElseIf statement and just inputted the _ADIsMemberOf function and removed the #include <adfunctions.au3>.

The script now looks like this and works. Sorry for posting this and not looking into it further.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=TeacherLogTest.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WindowsConstants.au3>

; _ADIsMemberOf
; Takes the groupname (SamAccountName with or without the leading 'CN=', and the SamAccountName of the user
; Returns 1 if the the user is a member of the group, 0 otherwise
; Alternate test by Arcker, uses only AD search
; really fast

Func _ADIsMemberOf($group, $user)
    If StringLeft($group, 3) <> "CN=" Then
        $group = "CN=" & $group
    EndIf
    Dim $usergroups[1], $i = 1

    Dim $objConnection, $oUsr,$groupdn


    $objConnection = ObjCreate("ADODB.Connection")  ; Create COM object to AD
    $objConnection.Provider = "ADsDSOObject"
    $objConnection.Open ("Active Directory Provider")  ; Open connection to AD
    $objRootDSE = ObjGet("LDAP://RootDSE")
    Global $strDNSDomain = $objRootDSE.Get ("defaultNamingContext")  ; Retrieve the current AD domain name

    $strQuery = "<LDAP://" & $strDNSDomain & ">;(" &$group& ");distinguishedName;subtree"
    $objRecordSet = $objConnection.Execute ($strQuery)  ; Retrieve the FQDN for the logged on user
    if $objRecordSet.eof then
        SetError(2)
        Return 0 ;group non found
    Else
        $groupdn=$objRecordSet.fields(0).value
    EndIf
    $strQuery = "<LDAP://" & $strDNSDomain & ">;((sAMAccountName=" & $user & ")(memberof="&$groupdn&"));ADsPath;subtree"
    $objRecordSet = $objConnection.Execute ($strQuery)  ; Retrieve the FQDN for the logged on user
    if $objRecordSet.eof then
        SetError(1)
        Return 0
    Else
        return 1
    EndIf

EndFunc   ;==>_ADIsMemberOf

$TITLE = ("Username, Computer Name, Time, Date")
$Login = (@UserName & "," & @ComputerName & "," & @HOUR & ":" & @MIN & ":" & @SEC & "," & @MON & "/" & @MDAY & "/" & @YEAR)

If _ADIsMemberOf("Domain Admins","") Then
    Call ("DomainAdmins")

ElseIf _ADIsMemberOf("Teachers","") Then
    Call ("Teachers")

ElseIf _ADIsMemberOf("Students","") Then
    Call ("Students")

ElseIf _ADIsMemberOf("Office","") Then
    Call ("Office")
EndIf

Func DomainAdmins()
    If FileReadLine("\\app2\logs$\WSH\Domain Admins Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH\Domain Admins Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH\Domain Admins Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH\Domain Admins Log.csv", $Login)
    EndIf
EndFunc

Func Teachers()
    If FileReadLine("\\app2\logs$\WSH\WSH - Teachers Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH\WSH - Teachers Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH\WSH - Teachers Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH\WSH - Teachers Log.csv", $Login)
    EndIf
EndFunc

Func Students()
    If FileReadLine("\\app2\logs$\WSH\WSH - Students Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH\WSH - Students Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH\WSH - Students Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH\WSH - Students Log.csv", $Login)
    EndIf
EndFunc

Func Office()
    If FileReadLine("\\app2\logs$\WSH\WSH - Office Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH\WSH - Office Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH\WSH - Office Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH\WSH - Office Log.csv", $Login)
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Ok I am being far too quick to say I fixed things! The script compiles without errors and it works for the "Domain Admins" security group, which is the first one that the script checks. The other groups though are not working.

Can anyone see what is wrong with the script please?

#include <WindowsConstants.au3>
#include <adfunctions.au3>

$TITLE = ("Username, Computer Name, IP Address, Time, Date")
$Login = (@UserName & "," & @ComputerName & "," & @IPAddress1 & "," & @HOUR & ":" & @MIN & ":" & @SEC & "," & @MON & "/" & @MDAY & "/" & @YEAR)

;==> This is where the script will look for the functions that will add a record to the log depending on the security group the user is a member of.

;==> Current school security groups
If _ADIsMemberOf("Domain Admins","") Then
    Call ("DomainAdmins")

ElseIf _ADIsMemberOf("Teachers","") Then
    Call ("Teachers")

ElseIf _ADIsMemberOf("Students","") Then
    Call ("Students")

ElseIf _ADIsMemberOf("Office","") Then
    Call ("Office")

;==> Central High School
ElseIf _ADIsMemberOf("CSH_Teachers","") Then
    Call ("CSH_Teachers")

ElseIf _ADIsMemberOf("CSH_Students","") Then
    Call ("CSH_Students")

ElseIf _ADIsMemberOf("CSH_Office","") Then
    Call ("CSH_Office")

;==> East Middle School
ElseIf _ADIsMemberOf("EMS_Teachers","") Then
    Call ("EMS_Teachers")

ElseIf _ADIsMemberOf("EMS_Students","") Then
    Call ("EMS_Students")

ElseIf _ADIsMemberOf("EMS_Office","") Then
    Call ("EMS_Office")

;==> Traverse City High School
ElseIf _ADIsMemberOf("TCHS_Teachers","") Then
    Call ("TCHS_Teachers")

ElseIf _ADIsMemberOf("TCHS_Students","") Then
    Call ("TCHS_Students")

ElseIf _ADIsMemberOf("TCHS_Office","") Then
    Call ("TCHS_Office")

;==> West Middle School
ElseIf _ADIsMemberOf("WMS_Teachers","") Then
    Call ("WMS_Teachers")

ElseIf _ADIsMemberOf("WMS_Students","") Then
    Call ("WMS_Students")

ElseIf _ADIsMemberOf("WMS_Office","") Then
    Call ("WMS_Office")

;==> West High School
ElseIf _ADIsMemberOf("WSH_Teachers","") Then
    Call ("WSH_Teachers")

ElseIf _ADIsMemberOf("WSH_Students","") Then
    Call ("WSH_Students")

ElseIf _ADIsMemberOf("WSH_Office","") Then
    Call ("WSH_Office")

EndIf

;==> These functions will do the part of adding a record to the log in the CSV file.
;==> The records are added to the particular log depending on the security group.
;==> Each function is named by the security group.

Func DomainAdmins()
    If FileReadLine("\\app2\logs$\Domain Admins Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\Domain Admins Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\Domain Admins Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\Domain Admins Log.csv", $Login)
    EndIf
EndFunc

Func Teachers()
    If FileReadLine("\\app2\logs$\WSH - Teachers Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH - Teachers Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH - Teachers Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH - Teachers Log.csv", $Login)
    EndIf
EndFunc

Func Students()
    If FileReadLine("\\app2\logs$\WSH - Students Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH - Students Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH - Students Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH - Students Log.csv", $Login)
    EndIf
EndFunc

Func Office()
    If FileReadLine("\\app2\logs$\WSH - Office Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH - Office Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH - Office Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH - Office Log.csv", $Login)
    EndIf
EndFunc

Func CSH_Teachers()
    If FileReadLine("\\app2\logs$\CSH - Teachers Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\CSH - Teachers Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\CSH - Teachers Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\CSH - Teachers Log.csv", $Login)
    EndIf
EndFunc

Func CSH_Students()
    If FileReadLine("\\app2\logs$\CSH - Students Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\CSH - Students Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\CSH - Students Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\CSH - Students Log.csv", $Login)
    EndIf
EndFunc

Func CSH_Office()
    If FileReadLine("\\app2\logs$\CSH - Office Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\CSH - Office Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\CSH - Office Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\CSH - Office Log.csv", $Login)
    EndIf
EndFunc

Func EMS_Teachers()
    If FileReadLine("\\app2\logs$\EMS - Teachers Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\EMS - Teachers Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\EMS - Teachers Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\EMS - Teachers Log.csv", $Login)
    EndIf
EndFunc

Func EMS_Students()
    If FileReadLine("\\app2\logs$\EMS - Students Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\EMS - Students Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\EMS - Students Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\EMS - Students Log.csv", $Login)
    EndIf
EndFunc

Func EMS_Office()
    If FileReadLine("\\app2\logs$\EMS - Office Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\EMS - Office Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\EMS - Office Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\EMS - Office Log.csv", $Login)
    EndIf
EndFunc

Func TCHS_Teachers()
    If FileReadLine("\\app2\logs$\TCHS - Teachers Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\TCHS - Teachers Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\TCHS - Teachers Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\TCHS - Teachers Log.csv", $Login)
    EndIf
EndFunc

Func TCHS_Students()
    If FileReadLine("\\app2\logs$\TCHS - Students Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\TCHS - Students Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\TCHS - Students Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\TCHS - Students Log.csv", $Login)
    EndIf
EndFunc

Func TCHS_Office()
    If FileReadLine("\\app2\logs$\TCHS - Office Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\TCHS - Office Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\TCHS - Office Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\TCHS - Office Log.csv", $Login)
    EndIf
EndFunc

Func WMS_Teachers()
    If FileReadLine("\\app2\logs$\WMS - Teachers Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WMS - Teachers Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WMS - Teachers Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WMS - Teachers Log.csv", $Login)
    EndIf
EndFunc

Func WMS_Students()
    If FileReadLine("\\app2\logs$\WMS - Students Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WMS - Students Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WMS - Students Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WMS - Students Log.csv", $Login)
    EndIf
EndFunc

Func WMS_Office()
    If FileReadLine("\\app2\logs$\WMS - Office Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WMS - Office Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WMS - Office Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WMS - Office Log.csv", $Login)
    EndIf
EndFunc

Func WSH_Teachers()
    If FileReadLine("\\app2\logs$\WSH - Teachers Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH - Teachers Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH - Teachers Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH - Teachers Log.csv", $Login)
    EndIf
EndFunc

Func WSH_Students()
    If FileReadLine("\\app2\logs$\WSH - Students Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH - Students Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH - Students Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH - Students Log.csv", $Login)
    EndIf
EndFunc

Func WSH_Office()
    If FileReadLine("\\app2\logs$\WSH - Office Log.csv", 1) = $TITLE Then
        FileWriteLine("\\app2\logs$\WSH - Office Log.csv", $Login)
    Else
        FileWriteLine("\\app2\logs$\WSH - Office Log.csv", $TITLE)
        FileWriteLine("\\app2\logs$\WSH - Office Log.csv", $Login)
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0