Jump to content

Automated Network Folder Auditor


 Share

Recommended Posts

I am trying to write a program that will go through a *.txt file produced by Windows PowerShell using this command 'Get-Acl @:* | Format-List >dump.txt'. This command produces a list containing the names of all the files in the '@' directory along with their attributes such as: Owner, Access, etc. The issue I am having, is that when I am trying to parse the names of the directories without the '@:' portion of the path name as well as with it, the program is skipping folders listed in the *.txt file. I apologize for all the 'If' statements in the one function. It has been a while since I had to put some serious thought into my code. I am a little embarassed to admit it but I have been at this for too long now, and I could really use some help. Also please note that, the code that is under the 'M:' If condition is the most up to date as I have been debugging with files from an external drive I mounted as M:.

Again Thank You For Any Help!

;Day 1 Date: 8/14/2013
;Written By: James Anderson
;Purpose: The following program was written as apart of a security audit on all \\
#include <File.au3>


MsgBox(64, "Permission Modifier (Requires PowerShell Permission Dump File)", "This program has been designed to read a *.txt file created by checking directory permissions using the command 'Get-Acl @:\* | Format-List >dump.txt'. With this file it will use the dumped directories to edit permissions and set access for only the IRS/Administrator and the IRS/User", 15)

sleep(750)

Global $dirCount = 1  ;Tracks which occurence of directory/ which user dump we are on in the read/stored dumpFile

Dim $fileContents  ;Stores what is left after previously used information has been truncated from read/stored dumpFile
Dim $fileDump

Global $dumpFileLoc = "C:\Users\janderson\Desktop\ModifyPermissions\dumpFile.txt"

Global $userName  ;Stores username with out directory NAME ONLY

Local $contFlag = False  ;Used for checking dumpFile validity
Local $buttonClick = 0  ;Used to check whether user would like to continue with the operation

Dim $userDirectory  ;Stores user directory (Ie. M:\janderson)
Dim $usersWithAccess  ;Stores list of users with access to current user directory

;This While loop is for receiving a valid dumpFile
While $contFlag = False
   Local $fileName = FileOpenDialog("Select PowerShell Dump File", "c:" & "\", "Text (*.txt)", 1 + 2)

   $buttonClick = MsgBox(1, "Would you like to continue?", "You have selected:"&$fileName)
   
   If $buttonClick = 1 Then ;Check to see if it is valid dumpFile
      $found = 0
      $find = "PowerShell"

      ;Searches for keyword:'PowerShell'
      _FileReadToArray($fileName, $fileContents)
      For $i = 1 To UBound($fileContents) - 1
         If StringInStr($fileContents[$i], $find) Then
            ;MsgBox(1, "Debug", "Debug: "&$fileContents[$i], 1)
            $fileContents[$i] = ''          
            $found += 1
            MsgBox(1, "Debug", "Found: "&$found)
         EndIf
      Next
      If $found > 0 Then ;If it is a valid dumpFile
         $contFlag = True
      Else              ;If it is NOT a valid dumpFile
         MsgBox(1, "Error", "You have selected an Invalid Dump File!", 10)
      EndIf
   EndIf
WEnd

;This For loop will check for the directory for which permissions need to be modified based on dumpFile and modify those permissions
$fileDump = FileRead($fileName)

For $i = 0 To $found
   
   If $i = 0 Then
      $fileDump = FileRead($fileName)
   Else
      $fileDump = FileRead($dumpFileLoc)
   EndIf
   
   FindUserDir($fileDump)
   ;GetUsersWithAccess()

   MsgBox(1,"Debug - userDirectory", $userDirectory)
   MsgBox(1,"Debug - userName", $userName)
Next
Exit  

Func UpdateDumpFile($array)
   $dumpFile = FileOpen($dumpFileLoc, 2)
   FileWrite($dumpFile, $array)
   FileClose($dumpFile)
   Return
EndFunc

;This function will compose a list of all IRS domain users with access to the current user directory
Func GetUsersWithAccess ()
   Dim $users
   
   ;$a = StringInStr($fileContents, "Access : ")
   ;StringTrimLeft($fileContents, $a)
   
   ;MsgBox(1,"Debug - 'Access:' Location", $a)
   ;MsgBox(1,"Debug - 'Access:' Location", $fileContents)
   ;$users = StringSplit($fileContents, "Access:")
   
   ;MsgBox(1,"Debug", $users[1])
   
EndFunc

;This function will return the user directory (Ie. M:\janderson), it also defines the global userName variable & the fileContents variable
Func FindUserDir ($array)
   Dim $dir
   Dim $temp
      MsgBox(1,"Debug - FindUserDir(Begin)", $array)
   
   If StringInStr($array, "A:\") Then
      $dirPos = StringInStr($array, "A:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "B:\")Then
      $dirPos = StringInStr($array, "B:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "C:\")Then
      $dirPos = StringInStr($array, "C:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return
   ElseIf StringInStr($array, "D:\")Then
      $dirPos = StringInStr($array, "D:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "E:\")Then
      $dirPos = StringInStr($array, "E:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "F:\")Then
      $dirPos = StringInStr($array, "F:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return
   ElseIf StringInStr($array, "G:\")Then
      $dirPos = StringInStr($array, "G:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "H:\")Then
      $dirPos = StringInStr($array, "H:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "I:\")Then
      $dirPos = StringInStr($array, "I:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "J:\")Then
      $dirPos = StringInStr($array, "J:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "K:\")Then
      $dirPos = StringInStr($array, "K:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return
   ElseIf StringInStr($array, "L:\")Then
      $dirPos = StringInStr($array, "L:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      MsgBox(1, "Debug - kkkkkkk", "kkkkk")
      Return 
   ElseIf StringInStr($array, "M:\")Then
      $dirPos = StringInStr($array, "M:\",0,$dirCount)
      MsgBox(1,"Debug - FindUserDir(1)", $array)
      $array = StringMid($array, $dirPos)
      MsgBox(1,"Debug - FindUserDir(2)", $array)
      $dirCount += 1
      $dir = StringSplit($array, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      UpdateDumpFile($array)
      Return
   ElseIf StringInStr($array, "N:\")Then
      $dirPos = StringInStr($array, "N:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return  
   ElseIf StringInStr($array, "O:\")Then
      $dirPos = StringInStr($array, "O:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return  
   ElseIf StringInStr($array, "P:\")Then
      $dirPos = StringInStr($array, "P:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return  
   ElseIf StringInStr($array, "Q:\")Then
      $dirPos = StringInStr($array, "Q:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "R:\")Then
      $dirPos = StringInStr($array, "R:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return
   ElseIf StringInStr($array, "S:\")Then
      $dirPos = StringInStr($array, "S:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return
   ElseIf StringInStr($array, "T:\")Then
      $dirPos = StringInStr($array, "T:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "U:\")Then
      $dirPos = StringInStr($array, "U:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "V:\")Then
      $dirPos = StringInStr($array, "V:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "W:\")Then
      $dirPos = StringInStr($array, "W:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return
   ElseIf StringInStr($array, "X:\")Then
      $dirPos = StringInStr($array, "X:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   ElseIf StringInStr($array, "Y:\")Then
      $dirPos = StringInStr($array, "Y:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return
   ElseIf StringInStr($array, "Z:\")Then
    $dirPos = StringInStr($array, "Z:\",0,$dirCount)
      $fileContents = StringMid($array, $dirPos)
      $dirCount += 1
      $dir = StringSplit($fileContents, @CRLF)
      $userName = StringTrimLeft($dir[1], 3)
      $userDirectory = $dir[1]
      Return 
   Else
      Exit
   EndIf
EndFunc
Link to comment
Share on other sites

I have resolved this issue. I was incrementing the occurence of the drive letter I was searching for and overwriting my temp file as opposed to appending to it. Which meant that the next occurrence(actually) was the first occurrence in the temp file. Now that I am opening the file and appending to it the (actual)previous occurrence is in the file and when it looks for the second(actual) occurrence it is now in the temp file as well. If that makes any sense...? :sweating:

Link to comment
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
 Share

  • Recently Browsing   0 members

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