Jump to content

iCacls hanging


iamtheky
 Share

Recommended Posts

More than occasionally the icacls portion of the loop hangs.  And if it hangs on one item, it never returns to form.  I can timerdiff the process and if it hangs kill it, but like i said all subsequent trips through the loop exhibit the same behavior.

This has nothing to do with the validity of the strings, I just want to know what processes i need to kill when icacls hangs to get the next trip through the loop to have a chance at success.

Win 8.1 x64 - in case this turns out to be anomalous behavior on this system.  i will check my others tonight.

 

#RequireAdmin
DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)

#include <WinAPI.au3>
#include <WinAPIShellEx.au3>
#include <File.au3>

;-------specific paths to be checked----------------

Global $aArray[17]

$aArray[0] = "C:\Program Files"
$aArray[1] = "C:\Program Files\Common Files"
$aArray[2] = "C:\Program Files (x86)"
$aArray[3] = "C:\Program Files (x86)\Common Files"
$aArray[4] = "C:\ProgramData"
$aArray[5] = "C:\Windows"
$aArray[6] = "C:\Windows\System32"
$aArray[7] = "C:\Windows\System32\Drivers"
$aArray[8] = "C:\Windows\System32\Drivers\etc"
$aArray[9] = "C:\Windows\System32\Sysprep"
$aArray[10] = "C:\Windows\System32\wbem"
$aArray[11] = "C:\Windows\System32\WindowsPowerShell\v1.0"
$aArray[12] = "C:\Windows\Web"
$aArray[13] = "C:\Windows\SysWOW64"
$aArray[14] = "C:\Windows\SysWOW64\Drivers"
$aArray[15] = "C:\Windows\SysWOW64\wbem"
$aArray[16] = "C:\Windows\SysWOW64\WindowsPowerShell\v1.0"


;----------Folders to be recursed and all contents checked-----------------------------
;"C:\Users\Public","C:\Users\" & @UserName & "\AppData\Local","C:\Users\" & @UserName & "\AppData\Local\Temp","C:\Users\" & @UserName & "\AppData\LocalLow"
;"C:\Users\" & @UserName & "\AppData\Roaming"

local $aRecThese = ["C:\Boot","C:\Perflogs","C:\Users\All Users\Microsoft\Windows\Start Menu\Programs\Startup", _
"C:\Windows\Scripts","C:\Windows\System","C:\Windows\System32\Tasks", _
"C:\Windows\system32\config\systemprofile\AppData","C:\Windows\sysWOW64\sysprep", _
"C:\Windows\sysWOW64\config\systemprofile\AppData"]

;~ _ArrayDisplay($aRecThese)

for $i = 0 to ubound($aRecThese) - 1
  local $aList[0]
    $aList = _FileListToArrayRec($aRecThese[$i] , "*" , 0 , 1 , 0 , 2)
;~      _ArrayDisplay($aList)
        _ArrayDelete($aList , 0)
        _ArrayAdd($aArray , $aRecThese[$i])
        if ubound($aList) > 1 Then _ArrayAdd($aArray , $aList)
;~      _ArrayDisplay($aArray)
next

;-------------------------------------------------------------------------------------

Global $aOut = [["PATH","ATTRIB","ACL","Modified","Created","Accessed"]]   ; Declare Final Array
;~ _ArrayDisplay($aOut)

;--------START LOOP THROUGH ARRAY OF Files and Folders----------------------------------

For $i = 0 to ubound($aArray) - 1
    tooltip("icacls " & $i & " of " & ubound($aArray) - 1)

    ;-----------------------------GET ATTRIBUTES------------------------------------------------

    $sAttrib = FileGetAttrib($aArray[$i])

    ;---------------------------GET ACLs----------------------------------------------------------
   $TIMER = timerinit()
   $iPID = run ('cmd /c icacls "' & $aArray[$i] & '" /C' ,  "", @SW_HIDE , $stdout_child)

    $sOutput = ""

         While 1
                $sOutput &= StdoutRead($iPID)
                If @error Then ExitLoop
                If TimerDiff($TIMER) > 2000 Then
                    processclose($iPID)
                    processwaitclose($iPID)
;~                  run("cmd /c taskkill /IM cmd.exe /F")
;~                  run("cmd /c taskkill /IM icacls.exe /F")
                    $TIMER = 0
                    exitloop
                EndIf
         WEnd

processclose($iPID)


sleep(50)

$aSplit = stringsplit($sOutput , @LF , 2)
tooltip("")

;-----------------------Get Times--------------



;------------Add All THat Stuff to the Final Array-----------------------------------------------------------

    _ArrayAdd($aOut , $aArray[$i])  ; Create a new row, Add the path to column 1
    $aOut[ubound($aOut) - 1][1] = $sAttrib ; add the attribute string to column 2
    $aOut[ubound($aOut) - 1][2] = stringregexpreplace(_ArrayToString($aSplit , ",," , 1 , ubound($aSplit) - 4) , "\s\s+" , "") ;ACL string in column 3
    $aOut[ubound($aOut) - 1][3] = FileGetTime($aArray[$i]  , 0 , 1)
    $aOut[ubound($aOut) - 1][4] = FileGetTime($aArray[$i]  , 0 , 1)
    $aOut[ubound($aOut) - 1][5] = FileGetTime($aArray[$i]  , 0 , 1)

Next

_ArrayDisplay($aOut)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

You might want to look at this UDF that FredAI wrote a while back that doesn't use Run or the icacls.exe program to set/get permissions.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Here is a sloooowww but reliable version using powershell, it does allow for two additional pieces of content, Owner and Group.

#RequireAdmin
DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)

#include <WinAPI.au3>
#include <WinAPIShellEx.au3>
#include <File.au3>

;-------specific paths to be checked----------------

Global $aArray[17]

$aArray[0] = "C:\Program Files"
$aArray[1] = "C:\Program Files\Common Files"
$aArray[2] = "C:\Program Files (x86)"
$aArray[3] = "C:\Program Files (x86)\Common Files"
$aArray[4] = "C:\ProgramData"
$aArray[5] = "C:\Windows"
$aArray[6] = "C:\Windows\System32"
$aArray[7] = "C:\Windows\System32\Drivers"
$aArray[8] = "C:\Windows\System32\Drivers\etc"
$aArray[9] = "C:\Windows\System32\Sysprep"
$aArray[10] = "C:\Windows\System32\wbem"
$aArray[11] = "C:\Windows\System32\WindowsPowerShell\v1.0"
$aArray[12] = "C:\Windows\Web"
$aArray[13] = "C:\Windows\SysWOW64"
$aArray[14] = "C:\Windows\SysWOW64\Drivers"
$aArray[15] = "C:\Windows\SysWOW64\wbem"
$aArray[16] = "C:\Windows\SysWOW64\WindowsPowerShell\v1.0"


;----------Folders to be recursed and all contents checked-----------------------------
;"C:\Users\Public","C:\Users\" & @UserName & "\AppData\Local","C:\Users\" & @UserName & "\AppData\Local\Temp","C:\Users\" & @UserName & "\AppData\LocalLow"
;"C:\Users\" & @UserName & "\AppData\Roaming"

local $aRecThese = ["C:\Boot","C:\Perflogs","C:\Users\All Users\Microsoft\Windows\Start Menu\Programs\Startup", _
"C:\Windows\Scripts","C:\Windows\System","C:\Windows\System32\Tasks", _
"C:\Windows\system32\config\systemprofile\AppData","C:\Windows\sysWOW64\sysprep", _
"C:\Windows\sysWOW64\config\systemprofile\AppData"]


for $i = 0 to ubound($aRecThese) - 1
  local $aList[0]
    $aList = _FileListToArrayRec($aRecThese[$i] , "*" , 0 , 1 , 0 , 2)

        _ArrayDelete($aList , 0)
        _ArrayAdd($aArray , $aRecThese[$i])
;~      _ArrayDisplay($aList)
        if ubound($aList) > 1 Then _ArrayAdd($aArray , $aList)

next

;-------------------------------------------------------------------------------------

Global $aOut = [["PATH","ATTRIB","OWNER","GROUP","ACL","Modified","Created","Accessed"]]   ; Declare Final Array

;--------START LOOP THROUGH ARRAY OF Files and Folders----------------------------------

For $i = 0 to ubound($aArray) - 1

    ;-----------------------------GET ATTRIBUTES------------------------------------------------

    $sAttrib = FileGetAttrib($aArray[$i])

;~     ;---------------------------GET ACLs----------------------------------------------------------
    $iPID = run ("powershell get-acl '"& $aArray[$i] & "' | format-list"  ,  "", @SW_HIDE , $stdout_child)

    tooltip ($i & " of " & ubound($aArray) - 1 , 0 ,0)

    $sOutput = ""

         While 1
                $sOutput &= StdoutRead($iPID)
                If @error Then
                    ExitLoop
                EndIf
         WEnd

    ProcessClose($iPID)

If $sOutput = "" Then continueloop
$aSplit = stringsplit($sOutput , @LF , 2)

$iOwner = _ArraySearch($aSplit , "Owner  :" , 0 , 0 ,0 ,1 ,0 ) - 1
$iAudit = _ArraySearch($aSplit , "Audit  :" , 0 , 0 ,0 ,1 ,0 )

_ArrayDelete($aSplit ,"0-" & $iOwner & ";" & $iAudit & "-" & ubound($aSplit) - 1)


;------------Add All THat Stuff to the Final Array AND GET TIMES-----------------------------------------------------------

    _ArrayAdd($aOut , $aArray[$i])  ; Create a new row, Add the path to column 1
    $aOut[ubound($aOut) - 1][1] = $sAttrib ; add the attribute string to column 2
    $aOut[ubound($aOut) - 1][2] =  stringreplace($aSplit[0] , "OWNER  :" , "" , 1)   ;OWNER
    $aOut[ubound($aOut) - 1][3] = stringreplace($aSplit[1] , "GROUP  :" , "" , 1)   ;GROUP
    $aOut[ubound($aOut) - 1][4] = stringreplace(stringregexpreplace(_ArrayToString($aSplit , ",," , 2 , ubound($aSplit) - 4) , "\s\s+" , "") , "ACCESS :" , "" , 1) ;ACL string in column 3
    $aOut[ubound($aOut) - 1][5] = FileGetTime($aArray[$i]  , 0 , 1)
    $aOut[ubound($aOut) - 1][6] = FileGetTime($aArray[$i]  , 0 , 1)
    $aOut[ubound($aOut) - 1][7] = FileGetTime($aArray[$i]  , 0 , 1)

tooltip ("")
Next

_ArrayDisplay($aOut)

Hopefully Brewman's suggestion ends up speedier.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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...