首页 > 解决方案 > PowerShell:写入进度条消失

问题描述

所以我对每个循环都有这个。我在这个循环中做的第一件事是调用我的一个 cmdlet 女巫,其中包括调用

 Write-Progress -Activity "Testing Progress" -Status "$($progressDisplay)% Complete."  -PercentComplete $progress -CurrentOperation "$($op)"

循环的每次迭代需要 30 分钟到 1 小时,但在迭代完成之前,进度条已经消失。

我没有发现他们的时间限制,如何延长时间限制。

通过将数据添加到我遍历的列表中的每个项目来完成进度。

Function Add-ProgressData{
 [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [System.Object]$list,
        [Switch]$NoCmdletEntryOutput)
    Begin{}
    
    Process{

        # calcuatle what 1% progress would be for 
        # the given list

        $OnePercent = [math]::Round(100/([double]$list.Count),2)
  
        # calculate start value for progress

        $Currentpercent = $OnePercent

        # enumerate the list of add progress data for each item

        foreach($i in $list) {

            # add ProgressData properties to node 
            # and set it to the current progress data

            $CurrentProgressDsiplay = [math]::Round(($Currentpercent),0)

            $po = [PSCustomObject]@{
                Type = "Percent"
                CurrentProgress = $Currentpercent
                CurrentProgressDsiplay = $CurrentProgressDsiplay
                CurrentOperation = "Testing $($i.Type) $($i.string)"
            }

            Add-Member -InputObject $i -NotePropertyName ProgressData -NotePropertyValue $po

            # Calcualte the next progress percent

            $Currentpercent = $Currentpercent + $OnePercent

        }

     
       }

    End{}

 }

这是此特定列表的数据的样子

@{Type=Percent; CurrentProgress=2.78; CurrentProgressDsiplay=3; CurrentOperation=Testing …    
@{Type=Percent; CurrentProgress=5.56; CurrentProgressDsiplay=6; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=8.34; CurrentProgressDsiplay=8; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=11.12; CurrentProgressDsiplay=11; CurrentOperation=Testing …    
@{Type=Percent; CurrentProgress=13.9; CurrentProgressDsiplay=14; CurrentOperation=Testing …   
@{Type=Percent; CurrentProgress=16.68; CurrentProgressDsiplay=17; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=19.46; CurrentProgressDsiplay=19; CurrentOperation=Testing …  
@{Type=Percent; CurrentProgress=22.24; CurrentProgressDsiplay=22; CurrentOperation=Testing …    
@{Type=Percent; CurrentProgress=25.02; CurrentProgressDsiplay=25; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=27.8; CurrentProgressDsiplay=28; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=30.58; CurrentProgressDsiplay=31; CurrentOperation=Testing …  
@{Type=Percent; CurrentProgress=33.36; CurrentProgressDsiplay=33; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=36.14; CurrentProgressDsiplay=36; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=38.92; CurrentProgressDsiplay=39; CurrentOperation=Testing …  
@{Type=Percent; CurrentProgress=41.7; CurrentProgressDsiplay=42; CurrentOperation=Testing …   
@{Type=Percent; CurrentProgress=44.48; CurrentProgressDsiplay=44; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=47.26; CurrentProgressDsiplay=47; CurrentOperation=Testing …  
@{Type=Percent; CurrentProgress=50.04; CurrentProgressDsiplay=50; CurrentOperation=Testing …  
@{Type=Percent; CurrentProgress=52.82; CurrentProgressDsiplay=53; CurrentOperation=Testing …  
@{Type=Percent; CurrentProgress=55.6; CurrentProgressDsiplay=56; CurrentOperation=Testing …   
@{Type=Percent; CurrentProgress=58.38; CurrentProgressDsiplay=58; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=61.16; CurrentProgressDsiplay=61; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=63.94; CurrentProgressDsiplay=64; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=66.72; CurrentProgressDsiplay=67; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=69.5; CurrentProgressDsiplay=70; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=72.28; CurrentProgressDsiplay=72; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=75.06; CurrentProgressDsiplay=75; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=77.84; CurrentProgressDsiplay=78; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=80.62; CurrentProgressDsiplay=81; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=83.4; CurrentProgressDsiplay=83; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=86.18; CurrentProgressDsiplay=86; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=88.96; CurrentProgressDsiplay=89; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=91.74; CurrentProgressDsiplay=92; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=94.52; CurrentProgressDsiplay=95; CurrentOperation=Testing …
@{Type=Percent; CurrentProgress=97.3; CurrentProgressDsiplay=97; CurrentOperation=Testing …   
@{Type=Percent; CurrentProgress=100.08; CurrentProgressDsiplay=100; CurrentOperation=Testing …   

其他部分

foreach( $g in $list) {


        try {

            #
            #  Start test case
            #

            $tc = Start-TestCase -name "Test"  -Progress $g

……

Start-Test 用例调用 Out-ProgressData -Object $Progress

 Function Out-ProgressData{
 [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [System.Object]$Object,
        [Switch]$NoCmdletEntryOutput)
    Begin{}
    
    Process{

        # prep variables 

        $progressData = $Object.ProgressData

        $progress         = $progressData.CurrentProgress
        $progressDisplay  = $progressData.CurrentProgressDsiplay
        $op               = $ProgressData.CurrentOperation

        # Disaplyes progress

        Write-Progress -Activity "Testing Progress" -Status "$($progressDisplay)% Complete."  -PercentComplete $progress -CurrentOperation "$($op)"
             
       }

    End{}

 }

标签: powershell

解决方案


推荐阅读