我有一个NTFS审核脚本,想要添加一个基本进度条。目前我有一个我称之为正常的功能,$i = 100
表示我的最大%。我通过
Get-ChildItem -Path $pathToFolders -Recurse -Force | ?{ $_.PSIsContainer } | % {$counter++}
要获取文件夹的总数,然后将$i
除以该数字,以获得每次评估文件夹时我需要增加进度条的%增量。问题是,在脚本完成时,它只会将进度条填充到40-50%左右?我错过了一些完全明显的东西吗
请参阅我的代码(首先注意$j = 0
):
# 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
}
}
答案 0 :(得分:3)
Powershell的Write-Progress
取0到100之间的数字来表示进度。你在这里编码的是0到100之间的数字,但是数字代表已经处理的文件夹数量(可以是5或50或2000),这不是百分比。
以下是您可能需要做的事情:
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++
}
这一部分特别是:
-PercentComplete ([int](($j / $Folders.Count) * 100))
这将正确计算百分比,假设每次循环时增加$j
。