【问题标题】:Powershell Progress BarPowershell 进度条
【发布时间】:2012-08-01 09:46:37
【问题描述】:

我是 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 
     } 
   } 
 }

【问题讨论】:

  • 请在其中发布 Write-Progress 的脚本。 IOW,向我们展示您的尝试,并告诉我们问题所在。还是您的问题是“我如何将这段代码放入那段代码中?”在这种情况下,请再次尝试看起来正确的方法,然后在此处发布。

标签: powershell progress-bar powershell-2.0


【解决方案1】:

这是我解决问题的方法:

$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)
}

【讨论】:

  • 嗨,克里斯,感谢您的帮助。这为我指明了正确的方向,但由于每个文件夹中的对象数量众多,我希望使进度条在 foreach-object 循环中工作。您向我展示的方式有时会使进度条看起来像在移动到列表中的下一个文件夹之前不会移动。我已经编辑了我的问题以反映我所做的更改,但又遇到了另一个问题。
  • 非常感谢克里斯,我首先无法让它工作,但使用你的答案的顶部和我的答案管理了一个解决方法,它工作正常。就是有点乱!我决定再给它一次清理东西,它起作用了,所以不确定我第一次做错了什么!再次感谢!
猜你喜欢
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
相关资源
最近更新 更多