【问题标题】:Powershell: Cannot bind parameter 'PercentComplete'. Cannot convert value "GetPercent" to type "System.Int32"Powershell:无法绑定参数“PercentComplete”。无法将值“GetPercent”转换为类型“System.Int32”
【发布时间】:2018-10-26 14:58:01
【问题描述】:

我是 powershell 的新手,我正在尝试编写进度活动。

首先我创建了一个函数:

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]
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    有一些很好的教程如何为你的 PoSh 脚本添加进度条,例如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/

    【讨论】:

      【解决方案2】:

      在 PowerShell 中,对函数的调用以空格分隔,而不是逗号分隔。将代码更改为

      Write-Progress -Activity "Compile BLABLA project" -Status "Compiling and deploying BLABLA" -PercentComplete (GetPercent 1 $steps)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-10
        • 2020-08-07
        • 1970-01-01
        • 1970-01-01
        • 2019-12-28
        • 2020-03-18
        • 2013-10-06
        相关资源
        最近更新 更多