有没有办法在PowerShell中将默认参数设置为用户函数的结果?

时间:2015-12-10 20:40:24

标签: powershell

我想为.ps1的默认参数指定函数返回值。

基本上,我有一个参数$Threads,它是"线程的最大数量"它被允许一次产卵。默认情况下,我希望它使用可用的CPU数量。这就是我所拥有的并且有效:

Param(
    $Threads=((get-counter "\Processor(*)\% idle time").countersamples).length-1,
)

问题在于它很难看,当我得到详细的帮助时,它会将默认值表示为:

-Threads <Object>
    States how many threads are to be used to check if there are tests in the 
    files.

    Required?                    false
    Position?                    3
    Default value                ((get-counter "\Processor(*)\% idle 
    time").countersamples).length-1
    Accept pipeline input?       false
    Accept wildcard characters?  false

我希望通过将默认值放入如下函数来更直观:

function number-of-cpus
{
    ((get-counter "\Processor(*)\% idle time").countersamples).length-1
}

然后我会添加到我的Param声明中,例如:

Param(
    $Threads=$(number-of-cpus),
)

然后会在详细的帮助中逐渐显示出来。像这样:

-Threads <Object>
    States how many threads are to be used to check if there are tests in the 
    files.

    Required?                    false
    Position?                    3
    Default value                $(number-of-cpus)
    Accept pipeline input?       false
    Accept wildcard characters?  false

然而,我似乎无法以有效的方式做到这一点。有没有办法做到这一点?

0 个答案:

没有答案