【问题标题】:Powershell Write-Progress Command in GUI stops right before it is finishedGUI中的Powershell Write-Progress命令在完成之前停止
【发布时间】:2021-06-18 10:58:49
【问题描述】:

我使用的代码在独立时工作得很好,但是现在我把它放到一个按钮中它就停止了。 例如我有 400571 行,我在其中查找匹配项,进度条停在第 400000 行,因为我告诉他每 1000 行更新一次写入进度以提高性能。

我很确定关键点必须是 if($i % 1000 -eq 0) 因为如果我将它降低到 10 他会停在 400570 并且仍然没有完成,但我不想将它设置为 1,因为每行刷新进度条会占用更多时间。

按钮中包含的代码是:

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(30,10)
$OKButton.Size = New-Object System.Drawing.Size(300,92)
$OKButton.Text = "Filtern"
$OKButton.Name = "Filter"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::None
$OKButton.Add_Click({$i= 0
$path = "C:\temp\smtpfilter\LNS5filter.txt"
$length = (Get-Content $path).Length

#Datum, Hostname und Message Nummer
$result = Get-Content $path | ForEach-Object {
    if($_ -match '(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2}).*\(((?:\d{1,3}\.){3}\d{1,3})\) disconnected\.?\s+(\d+) message\[s\]'){
        try {
            #$dns = [System.Net.Dns]::GetHostEntry($matches[2]).HostName
        }
        catch { 
            #$dns = 'Not available' 
        }
        [PsCustomObject]@{
            IP       = $matches[2]
            Messages = [int]$matches[3]
            #DNSName  = $dns
            Date     = [datetime]::ParseExact($matches[1], 'dd.MM.yyyy HH:mm:ss', $null)
        }
        $i++
    if($i % 1000 -eq 0){
        Write-Progress -activity "Searching for matches" -status "Scanned: $i of $($length)" -percentComplete (($i / $length)  * 100)
    }
 }}

 #Messages Counted
 $cumulative = $result | Group-Object -Property IP | ForEach-Object {
    [PsCustomObject]@{
        IP = $_.Name
        Messages = ($_.Group | Measure-Object -Property Messages -Sum).Sum
        #DNSName = $_.Group[0].DNSName
        Date    = ($_.Group | Sort-Object Date)[-1].Date
    }
}})
$objForm.Controls.Add($OKButton)

(我 # dns 命令,因为我现在远程工作,解析名称需要很长时间)

【问题讨论】:

    标签: powershell user-interface progress-bar


    【解决方案1】:

    您的foreach-object 正在运行 400571 次 - 您只需在迭代 400000 时停止更新进度条。

    如果您在 foreach-object 完成后再次更新进度条,您将达到 100% 完成的魔力...

    $result = Get-Content $path | ForEach-Object {
     ...
     }}
    
    # final update to progress bar with total items processed
    Write-Progress -activity "Searching for matches" -status "Scanned: $i of $($length)" -percentComplete (($i / $length)  * 100)
    
    #Messages Counted
    $cumulative = ...
    

    更新

    您还需要将计数器逻辑移到 if 子句之外,否则您只会计算处理的匹配条记录...

    $result = Get-Content $path | ForEach-Object {
        if($_ -match '(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2}).*\(((?:\d{1,3}\.){3}\d{1,3})\) disconnected\.?\s+(\d+) message\[s\]'){
            ...
            # move these lines out from the "if( $_ -match ..."
            $i++
            if($i % 1000 -eq 0)
            {
                Write-Progress -activity "Searching for matches" -status "Scanned: $i of $($length)" -percentComplete (($i / $length)  * 100)
            }
        }
    }
    

    变成

    $result = Get-Content $path | ForEach-Object {
    
        if($_ -match '(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2}).*\(((?:\d{1,3}\.){3}\d{1,3})\) disconnected\.?\s+(\d+) message\[s\]')
        {
            ...
        }
    
        # now we're counting *every* record, not just the ones that match
        $i++
        if( $i % 1000 -eq 0 )
        {
            Write-Progress -activity "Searching for matches" -status "Scanned: $i of $($length)" -percentComplete (($i / $length)  * 100)
        }
    
    }
    
    # final update to progress bar with total items processed
    Write-Progress -activity "Searching for matches" -status "Scanned: $i of $($length)" -percentComplete (($i / $length)  * 100)
    
    ...
    

    【讨论】:

    • 啊,您需要将计数器移动 if($_ -match { ... } 之后,否则您只会计算与 if 子句匹配的迭代次数。
    • 等等,难道我在一个按钮中有 2 个单独的命令?
    • @Trippin - 见上面的更新。请注意缩进以及 {} 与原始代码相比移动到的位置。
    • 非常感谢,进度条现在完成了,但显然这不是脚本中唯一的问题,因为它之后的 $cumulative 变量仍然是空的。当我在 ISE 中标记它们并在没有按钮的情况下运行它们时,它们工作得很好。
    猜你喜欢
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多