代码之家  ›  专栏  ›  技术社区  ›  Snake Eyes

Powershell:无法绑定参数“PercentComplete”。无法将值“GetPercent”转换为类型“System.Int32”

  •  0
  • Snake Eyes  · 技术社区  · 7 年前

    首先我创建了一个函数:

    function GetPercent($current, $total) {
        return ($current/$total) * 100
    }
    

    然后写

    [Int32]$steps = 10
    
    Write-Progress -Activity "Compile BLABLA project" -Status "Compiling and deploying BLABLA" -PercentComplete GetPercent(1, $steps)
    

    我犯了个错误:

    写入进度:无法绑定参数“PercentComplete”。不能 将值“GetPercent”转换为类型“System.Int32”。错误:“输入 字符串格式不正确。“

    我试过了

    function GetPercent($current, $total) {
        return ($current/$total) * 100 -as [Int32]
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Vivek Kumar Singh    7 年前

    Write-Progress -Activity "Compile BLABLA project" -Status "Compiling and deploying BLABLA" -PercentComplete (GetPercent 1 $steps)
    
        2
  •  0
  •   wp78de    7 年前

    只需将最后一个参数括在括号中: -PercentComplete (GetPercent 1, $steps)

    有一些不错的教程如何添加一个进度条到您的豪华脚本,例如。 https://blogs.technet.microsoft.com/heyscriptingguy/2011/01/29/add-a-progress-bar-to-your-powershell-script/

    $files = Get-ChildItem -Path c:\
    For($i = 1; $i -le $files.count; $i++)
    { Write-Progress -Activity "Collecting files" -status "Finding file $i" `
    -percentComplete ($i / $files.count*100)}
    $files | Select name
    

    https://www.credera.com/blog/technology-insights/perfect-progress-bars-for-powershell/

    推荐文章