PowerShell functions return behavior

后端 未结 3 2198
失恋的感觉
失恋的感觉 2021-02-12 21:58

I am seeing some rather weird behavior with PowerShell, it looks like custom functions might need a \"parenthesis wrapper\" to evaluate as you might expect them. Given a simple

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-12 22:30

    If you use PowerShell V2's editor, you would see that -eq in the first example is blue, because it is an argument and -eq in the second example is gray because it is an operator

    Also in V2, you can be strict about arguments, with CmdletBinding and param

    function Return-True
    {
        [CmdletBinding()]
        param()
        return $true
    }
    Return-True -eq $false
    
    Return-True -eq $false
    Return-True : A parameter cannot be found that matches parameter name 'eq'.
    At line:7 char:16
    + Return-True -eq <<<<  $false
        + CategoryInfo          : InvalidArgument: (:) [Return-True], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Return-True
    

提交回复
热议问题