Jump to content

Recommended Posts

Posted

I'm having a hell of a time trying to figure out how I would search for the user specified in script parameter in the log file and what OU they were in that is not "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com"

I'm stuck

Do I have to put each row in the array in it's own array so I can read each value that is delimited by a ":"?

Func _Revert()
    Local $RevertUser = $CmdLine[2]

    Local $aArray
    _FileReadToArray($LogFile, $aArray)
    _ArrayDisplay($aArray)

    For $i = UBound($aArray, 1) - 1 To 1 Step -1 ;Read log file array from bottom to top.
        If StringInStr($aArray[$i], $RevertUser & ":OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com") Then ;Skip row if user is in the disabled OU and move                                                                                                        ;on to the next row until $RevertUser is found again.
            ContinueLoop
        ElseIf

    Exit

EndFunc   ;==>_Revert

 

 

Posted
27 minutes ago, antmar904 said:

how can I check multiple expressions?

If StringInStr($UsersArray[$a][3], $User, 2) And If Not StringInStr($UsersArray[$a][4], "OU=Disabled,OU=Antiquated,DC=na,DC=praxair,DC=com", 2) Then
            MsgBox(0, "", "Found! " & $UsersArray[$a][3] & " " & $UsersArray[$a][4])
            Exit
        EndIf

 

I don't have your full script but StringInStr will return 0 if it does not find what you look for. Try:

If StringInStr($UsersArray[$a][3], $User, 2) And StringInStr($UsersArray[$a][4], "OU=Disabled,OU=Antiquated,DC=na,DC=praxair,DC=com", 2) = 0 Then
            MsgBox(0, "", "Found! " & $UsersArray[$a][3] & " " & $UsersArray[$a][4])
            Exit
        EndIf

 

Always carry a towel.

Posted

I'm still having issues with this:

#include <File.au3>
#include <AutoItConstants.au3>
#include <StringConstants.au3>

Local $LogFileArray, $User = "usaaxftest"


_FileReadToArray(@ScriptDir & "\ADCleanupLog_Log.txt", $LogFileArray)
_ArrayDisplay($LogFileArray, "Log File Array")

For $i = UBound($LogFileArray, 1) - 1 To 1 Step -1 ;Log file array

    Local $UsersArray = StringSplit($LogFileArray[$i], ":") ;Users array

    ;_ArrayDisplay($UsersArray, "Users Array")

    For $a = 3 To UBound($UsersArray) - 1

        If StringInStr($UsersArray[$a][3], $User, 2) And StringInStr($UsersArray[$a][4], "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com", 2) = 0 Then
            MsgBox(0, "", "Found! " & $UsersArray[$a][3] & " " & $UsersArray[$a][4])
            Exit
        EndIf


    Next
Next

Exit

 

Posted

"C:\Stuff\Scripts\AutoIT\Active Directory\ADCleanup\TEST - Array.au3" (19) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If StringInStr($UsersArray[$a][3], $User, 2) And StringInStr($UsersArray[$a][4], "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com", 2) = 0 Then
If StringInStr(^ ERROR

Posted (edited)
2 hours ago, antmar904 said:

Its not finding the user in the txt file

Hmmm.... how about:

#include <File.au3>
#include <AutoItConstants.au3>
#include <StringConstants.au3>

Local $LogFileArray, $User = "usaaxftest"


_FileReadToArray(@ScriptDir & "\ADCleanupLog_Log.txt", $LogFileArray)


For $i = UBound($LogFileArray) - 1 To 1 Step - 1 ;Log file array
    If StringInStr($LogFileArray[$i], $User, 2) And StringInStr($LogFileArray[$i], "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com", 2) = 0 Then
        ConsoleWrite("Found! " & $LogFileArray[$i] & @CRLF)
        Local $UsersArray = StringSplit($LogFileArray[$i], ":") ;Users array
        MsgBox(0, "", "Found! " & @CRLF & StringStripWS($UsersArray[2], 8) & @CRLF & $UsersArray[3] & @CRLF & $UsersArray[4]) ; StringStripWS becuase there is a space in the user ID
        Exit
    EndIf
Next
Exit

Edit: Forgot you want to step through the array from the bottom up.

Edited by ModemJunki
Whoops! Forgot wants to go through array bottom-up

Always carry a towel.

Posted (edited)

That works however I wanted to skip the LINE in the text file if the user "usaaxftest" is found with in OU "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com" and go to the next above line in the text file and keep searching for that same user until a different OU is found other than the one I listed above.

Basically I want to find out what OU the user was in before moving them.  So once I find the line that does not ":OU=Disabled" in it as the first OU then I can get the original OU they were in and move them back if I had to.

 

Exp of text file:

07262018_101132(usera):usaaxftest:CN=Users,DC=na,DC=domain,DC=com:OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com <--GOOD LINE

07232018_153625(usera):usaaxftest:OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com:UNABLE TO MOVE  <--BAD LINE

Edited by antmar904
Posted

Here is the full script:

#include <AD.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

#RequireAdmin

Global $File = @ScriptDir & "\ADCleanupUserList.txt", $Data, $LogFile = @ScriptDir & "\ADCleanupLog.txt", $hFileOpen, $TargetOU = "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com"

If $CmdLine[0] <> 0 Then
    If $CmdLine[1] = "-revert" Then
        _Revert()
    Else
        MsgBox($MB_ICONWARNING, "ERROR", "Wrong parameter entered." & @CRLF & @CRLF & "Usage: -revert <username>" & @CRLF & "(" & "This will re-enable a users account and move them back into their original OU." & ")")
        Exit
    EndIf
EndIf

_CleanUp()

Func _CleanUp()

    $hFileOpen = FileOpen($LogFile, 9)

    If Not FileExists($File) Then
        MsgBox(48, "ERROR", "File does not exist:" & @CRLF & @CRLF & $File)
        FileWrite($hFileOpen, @MON & @MDAY & @YEAR & "_" & @HOUR & @MIN & @SEC & "(" & @UserName & ")" & ": " & "ADCleanupUserList.txt does not exist." & @CRLF)
        FileClose($hFileOpen)
        Exit
    EndIf

    $Data = FileReadToArray($File)

    Local $AD = _AD_Open("", "", "", "server.com")
    If @error Then
        MsgBox(0, "ERROR: Connecting to AD", "ERROR: " & @error & "  Extended: " & @extended)
        FileWrite($hFileOpen, @MON & @MDAY & @YEAR & "_" & @HOUR & @MIN & @SEC & "(" & @UserName & ")" & ": " & "Error connecting to AD: " & "Error code: " & @error & "Extended: " & @extended & @CRLF)
        FileClose($hFileOpen)
        Exit
    EndIf

    For $i = 0 To UBound($Data) - 1
        Local $UserOU = _AD_GetObjectOU($Data[$i]) ;<-- Get users current OU
        If @error = 1 Then
            ;MsgBox(0, "ERROR: Unable to find object", "ERROR: " & @error & "  Extended: " & @extended)
            FileWrite($hFileOpen, @MON & @MDAY & @YEAR & "_" & @HOUR & @MIN & @SEC & "(" & @UserName & ")" & ":" & $Data[$i] & ":" & "NOT FOUND" & @CRLF)
        Else
            _AD_DisableObject($Data[$i]) ;<-- Disable users account
            If @error Then
                ;MsgBox(0, "ERROR: Unable to disable object", "ERROR: " & @error & "  Extended: " & @extended)
                FileWrite($hFileOpen, @MON & @MDAY & @YEAR & "_" & @HOUR & @MIN & @SEC & "(" & @UserName & ")" & ":" & $Data[$i] & ":" & $UserOU & ":UNABLE TO DISABLE" & @CRLF)
            EndIf

            Sleep(500) ;Sleep for 1/2 of second or 500 milliseconds.

            _AD_MoveObject($TargetOU, $Data[$i]) ;<-- Move users object to the "Antiquated" OU
            If @error Then
                ;MsgBox(0, "ERROR: Unable to moving object", "ERROR: " & @error & "  Extended: " & @extended)
                FileWrite($hFileOpen, @MON & @MDAY & @YEAR & "_" & @HOUR & @MIN & @SEC & "(" & @UserName & ")" & ":" & $Data[$i] & ":" & $UserOU & ":UNABLE TO MOVE" & @CRLF)
            Else
                Local $UserOU2 = _AD_GetObjectOU($Data[$i])
                FileWrite($hFileOpen, @MON & @MDAY & @YEAR & "_" & @HOUR & @MIN & @SEC & "(" & @UserName & ")" & ":" & $Data[$i] & ":" & $UserOU & ":" & $UserOU2 & @CRLF)
            EndIf

            Local $GetAttribute = _AD_GetObjectAttribute($Data[$i], "Description") ;Get users Description attribute

            Local $Add = _AD_ModifyAttribute($Data[$i], "Description", $GetAttribute & " - Auto disabled by GIS", 2) ;Add " - Auto disabled by GIS" to the users current "Description" attribute.

        EndIf
    Next

    FileClose($hFileOpen)

    _AD_Close()

    Exit

EndFunc   ;==>_CleanUp

Func _Revert()
; If script is with syntax: ADCleanup.exe -revert <username> this will search the log file for the users last OU,
; re-enable the users account and move them to their orginal OU THAT IS NOT: OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com

Local $LogFileArray, $RevertUser = $CmdLine[2]

_FileReadToArray(@ScriptDir & "\ADCleanupLog.txt", $LogFileArray)


For $i = UBound($LogFileArray) - 1 To 1 Step - 1 ;Log file array
    If StringInStr($LogFileArray[$i], $User, 2) And StringInStr($LogFileArray[$i], "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com", 2) = 0 Then
        ConsoleWrite("Found! " & $LogFileArray[$i] & @CRLF)
        Local $UsersArray = StringSplit($LogFileArray[$i], ":") ;Users array
        MsgBox(0, "", "Found! " & @CRLF & StringStripWS($UsersArray[2], 8) & @CRLF & $UsersArray[3] & @CRLF & $UsersArray[4]) ; StringStripWS becuase there is a space in the user ID
        Exit
    EndIf
Next
Exit
EndFunc   ;==>_Revert

That cleans up some old accounts and moves them to a "Disabled" OU.  However if an account gets disabled that is still in use, I would like an easy way to revert the changes made to that account by running the script like: ADCleanup.exe -revert <user name> which would read the log file from bottom to top and search for the user name until it found the next line with the users original OU.

Attached is a sample of the log file.

You can see in my script how the log file is formatted.

 

ADCleanupLog.txt

Posted
2 hours ago, antmar904 said:

That works however I wanted to skip the LINE in the text file if the user "usaaxftest" is found with in OU "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com" and go to the next above line in the text file and keep searching for that same user until a different OU is found other than the one I listed above.

Ah. My apologies for not reading through the requirement a bit more closely.

Then we can try something like:

#include <File.au3>
#include <AutoItConstants.au3>
#include <StringConstants.au3>

Local $LogFileArray, $User = "usaaxftest"

_FileReadToArray(@ScriptDir & "\ADCleanupLog_Log.txt", $LogFileArray)
$aResult = _ArrayFindAll($LogFileArray, $User, 0, 0, 0, 1)

For $i = UBound($aResult) - 1 To 0 Step - 1 ;Log file array
    Local $UsersArray = StringSplit($LogFileArray[$aResult[$i]], ":") ;Users array
    If StringStripWS($UsersArray[2], 8) = $User And StringInStr($UsersArray[3], "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com", 2) <> 0 Then ; we know the position of the first matching OU string in the array
        ConsoleWrite("Found! " & $LogFileArray[$aResult[$i]] & " at position " & $aResult[$i] & @CRLF)
        For $j = $i - 1 To 0 Step - 1 ; restart the search from "(known position - 1)"
            Local $UsersArray = StringSplit($LogFileArray[$aResult[$j]], ":") ;Users array
            If StringStripWS($UsersArray[2], 8) = $User And StringInStr($UsersArray[3], "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com", 2) = 0 Then ; we know the position of the first NON-matching OU string in the array
                ConsoleWrite("Found the next previous one! " & $LogFileArray[$aResult[$j]] & " at position " & $aResult[$j] & @CRLF)
                Exit
            EndIf
        Next
    EndIf
Next

I'm not expert at this, I just have some idle time today while running some tests (and waiting for results...).

Always carry a towel.

Posted

@ModemJunki That worked great thank you for everyone's help on this.  One last request.  How would I get what the OU is so I can use that and move the user back if needed?

 

Exp:

How can I read what the bold text is after finding it in your above script?

07232018_145925(usera):usaaxftest:OU=General Users,OU=Users,OU=USA,DC=na,DC=domain,DC=com:OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com

Posted
On 7/30/2018 at 8:27 AM, antmar904 said:

How can I read what the bold text is after finding it in your above script?

Use StringSplit then read the information for the array that results.

#include <File.au3>
#include <AutoItConstants.au3>
#include <StringConstants.au3>

Local $LogFileArray, $User = "usaaxftest"

_FileReadToArray(@ScriptDir & "\ADCleanupLog_Log.txt", $LogFileArray)
$aResult = _ArrayFindAll($LogFileArray, $User, 0, 0, 0, 1)

For $i = UBound($aResult) - 1 To 0 Step - 1 ;Log file array
    Local $UsersArray = StringSplit($LogFileArray[$aResult[$i]], ":") ;Users array
    If StringStripWS($UsersArray[2], 8) = $User And StringInStr($UsersArray[3], "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com", 2) <> 0 Then ; we know the position of the first matching OU string in the array
        ConsoleWrite("Found! " & $LogFileArray[$aResult[$i]] & " at position " & $aResult[$i] & @CRLF)
        For $j = $i - 1 To 0 Step - 1 ; restart the search from "(known position - 1)"
            Local $UsersArray = StringSplit($LogFileArray[$aResult[$j]], ":") ;Users array
            If StringStripWS($UsersArray[2], 8) = $User And StringInStr($UsersArray[3], "OU=Disabled,OU=Antiquated,DC=na,DC=domain,DC=com", 2) = 0 Then ; we know the position of the first NON-matching OU string in the array
                ConsoleWrite("Found the next previous one! " & $LogFileArray[$aResult[$j]] & " at position " & $aResult[$j] & @CRLF)
                $a_content = StringSplit($LogFileArray[$aResult[$j]], ":"); split the string up
                _arraydisplay($a_content)
                Exit
            EndIf
        Next
    EndIf
Next

Result in attached image.

$a_content.png

Always carry a towel.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...