在 powershell 5.1 中调用函数的最佳方式或不同方式
the best way or different ways to call a function in powershell 5.1
我正在寻找在 powershell 中调用函数的最佳方式。
我在 google 上搜索并找到以下两种调用函数的方法。参考 - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-5.1
- func -尺寸 50 -长度 100
- 函数 50 100
注意 - 假设函数名称为 'func' 和 2 个参数($Size,$Length)
在 PowerShell 中调用函数除了上面提到的以外还有什么不同的方法吗?
两种方式都有效,第一种因为变量名更清晰
正如 @Santiago 评论的那样:
Both ways are valid, there is no "better way" except for named parameters being more verbose could help others get a better understanding of what your function is doing
最佳实践(针对这个特定问题)实际上包含在 PSScriptAnalyzer that comes standard with e.g. the PowerShell Extension for Visual Studio Code 中,并即时向您发出(最佳实践)警告。
您也可以安装 PSScriptAnalyzer
并使用 Invoke-ScriptAnalyzer
检查您的脚本。
适用于您的问题的具体规则可以通过 Get-ScriptAnalyzerRule
:
检查
(Get-ScriptAnalyzerRule PSAvoidUsingPositionalParameters).Description
Readability and clarity should be the goal of any script we expect to maintain over time. When calling a command that takes parameters, where possible consider using name parameters as opposed to positional parameters. To fix a violation of this rule, please use named parameters instead of positional parameters when calling a command.
我正在寻找在 powershell 中调用函数的最佳方式。
我在 google 上搜索并找到以下两种调用函数的方法。参考 - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-5.1
- func -尺寸 50 -长度 100
- 函数 50 100
注意 - 假设函数名称为 'func' 和 2 个参数($Size,$Length)
在 PowerShell 中调用函数除了上面提到的以外还有什么不同的方法吗?
两种方式都有效,第一种因为变量名更清晰
正如 @Santiago 评论的那样:
Both ways are valid, there is no "better way" except for named parameters being more verbose could help others get a better understanding of what your function is doing
最佳实践(针对这个特定问题)实际上包含在 PSScriptAnalyzer that comes standard with e.g. the PowerShell Extension for Visual Studio Code 中,并即时向您发出(最佳实践)警告。
您也可以安装 PSScriptAnalyzer
并使用 Invoke-ScriptAnalyzer
检查您的脚本。
适用于您的问题的具体规则可以通过 Get-ScriptAnalyzerRule
:
(Get-ScriptAnalyzerRule PSAvoidUsingPositionalParameters).Description
Readability and clarity should be the goal of any script we expect to maintain over time. When calling a command that takes parameters, where possible consider using name parameters as opposed to positional parameters. To fix a violation of this rule, please use named parameters instead of positional parameters when calling a command.