Powershell进度条

时间:2012-08-01 09:46:38

标签: powershell progress-bar powershell-v2.0

我是Powershell的新手,在使用foreach-object循环获取进度条时遇到问题(如果可能的话)

感谢Chris下面是我到目前为止,我的问题是进度条到达某个点然后我得到错误:101参数大于100的最大允许范围:

$FolderList = Get-Content C:\Folders.txt
$i = 0

foreach( $Folder in $FolderList )
{

Write-Host $Folder
Get-ChildItem $Folder -Recurse *.pdf | foreach-object{

$fileCount = (Get-ChildItem $Folder).Count
$i += 1
Write-Progress -Activity "Counting Files" -status "Searching...." -percentComplete (($i / $fileCount)*100)

$pdf = c:\pdftk.exe $_.FullName dump_data
$NumberOfPages = [regex]::match($pdf,'NumberOfPages: (\d+)').Groups[1].Value

    New-Object PSObject -Property @{
    Name = $_.Name
    FullName = $_.FullName
    NumberOfPages = $NumberOfPages 
     } 
   } 
 }

1 个答案:

答案 0 :(得分:3)

以下是解决问题的方法:

$i = 0
$pdfFiles = @()

#First, get the files and add them to a collection:
foreach ($folder in $FolderList){
    Get-ChildItem $Folder -Recurse *.pdf | %{$pdfFiles += $_}
}

#Measure the collection
$fileCount = ($pdfFiles | Measure-Object).Count

#Do work on the collection
$pdfFiles | foreach-object{
    $pdf = c:\pdftk.exe $_.FullName dump_data
    $NumberOfPages = [regex]::match($pdf,'NumberOfPages: (\d+)').Groups[1].Value
    New-Object PSObject -Property @{
        Name = $_.Name
        FullName = $_.FullName
        NumberOfPages = $NumberOfPages 
    }
    $i += 1
    Write-Progress -Activity "Counting Files" -status "Searching...." -percentComplete (($i / $fileCount)*100)
}