【问题标题】:How can I set the default argument of a function to the output of another function in PowerShell 2?如何将函数的默认参数设置为 PowerShell 2 中另一个函数的输出?
【发布时间】:2012-09-06 19:12:54
【问题描述】:

在 PowerShell 2 中,我有一个返回当前季度的函数 Get-CurrentQuarter。

我有另一个函数接受参数 Quarter。我希望使用 Get-CurrentQuarter 函数将其默认为当前季度。

我试过了:

function Test-ParameterByFunction
{
  param(
    [string]$quarter = Get-CurrentQuarter
  )
}

Test-ParameterByFunction

PowerShell 报告:

PS > .\test.ps1
Missing expression after '='.
At .\test.ps1:4 char:23
+     [string]$quarter = <<<<  Get-CurrentQuarter
    + CategoryInfo          : ParserError: (=:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterToken

这对于清理我的代码非常方便。有没有我遗漏的语法?

谢谢!

【问题讨论】:

    标签: parameters powershell-2.0 default-value


    【解决方案1】:

    试试这个:

    [string]$quarter = [string](Get-CurrentQuarter)
    

    【讨论】:

    • 谢谢,好像可以了。 :-)
    猜你喜欢
    • 2017-02-03
    • 2023-03-11
    • 1970-01-01
    • 2011-11-06
    • 2015-03-31
    • 2011-04-01
    • 2014-04-25
    • 2019-04-14
    • 1970-01-01
    相关资源
    最近更新 更多