0

I have an NTFS Audit script and would like to add a basic progress bar. At the moment I have a function that I call like normal, with $i = 100 to indicate my max %. I run through the folders via

Get-ChildItem -Path $pathToFolders -Recurse -Force | ?{ $_.PSIsContainer } | % {$counter++}

To get a total number of folders, then divide my $i by that number to get the % increment I need to increase my progress bar by each time it evaluates a folder. Problem is it only fills the progress bar up to around 40-50% by the time the script completes? Am I missing something completely obvious?

See my code (note $j = 0 at first):

# Main 
ForEach ($Folder in $Folders){
    $ACLs = Get-ACL $Folder.FullName | % { $_.Access  }
    ForEach ($ACL in $ACLs){
        $OutInfo = $Folder.Fullname + "," + $ACL.IdentityReference  + "," + $ACL.AccessControlType + "," + $ACL.FileSystemRights + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
        Add-Content -Value $OutInfo -Path $outputCSV
    }
    Write-Progress -Activity "Auditing NTFS Permissions in ${$pathToFolders}..." -Status 'Progress ->' -PercentComplete $j
    $j = $j + $i
}
}
4

1 回答 1

3

Powershell's Write-Progress takes a number from 0 to 100 to indicate progress. What you've coded here is a number from 0 to 100, but the number represents the number of folders that have been processed (which could be 5 or 50 or 2000), which is not a percentage.

Here's what you probably need to do:

foreach ($Folder in $Folders)
{
    $ACLs = Get-ACL $Folder.FullName | % { $_.Access }
    foreach ($ACL in $ACLs)
    {
        $OutInfo = $Folder.Fullname + "," + $ACL.IdentityReference  + "," + $ACL.AccessControlType + "," + $ACL.FileSystemRights + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
        Add-Content -Value $OutInfo -Path $outputCSV
    }
    Write-Progress -Activity "Auditing NTFS Permissions in ${$pathToFolders}..." -Status 'Progress ->' -PercentComplete (([int]($j / $Folders.Count)) * 100)
    $j++
}

This part in particular:

-PercentComplete ([int](($j / $Folders.Count) * 100))

That will calculate the percentage correctly, assuming you increment $j each time it loops.

于 2013-10-23T21:23:39.250 回答