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

时间:2018-10-26 06:49:00

标签: powershell

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

我得到了错误:

  

Write-Progress:无法绑定参数'PercentComplete'。不能   将值“ GetPercent”转换为“ System.Int32”。错误:“输入   字符串格式不正确。“

我尝试过

function GetPercent($current, $total) {
    return ($current/$total) * 100 -as [Int32]
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

仅将最后一个参数括在括号中:-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/