Function 参数的正确使用

Function 参数的正确使用,function,powershell,Function,Powershell,处理参数的最佳实践是什么,何时选择哪个选项 示例:我通常编写如下函数: function Do-Something ($variable1, $variable2, $variable3....) { Do Stuff } Function Test-Port ([string]$Server,[uint16]$Port=80,[uint16]$Timeout = 3000) { #code here } function Get-SiteCert{ [Cmdlet

处理参数的最佳实践是什么,何时选择哪个选项

示例:我通常编写如下函数:

function Do-Something ($variable1, $variable2, $variable3....) 
    { Do Stuff }
Function Test-Port ([string]$Server,[uint16]$Port=80,[uint16]$Timeout = 3000) {
    #code here
}
function Get-SiteCert{
    [CmdletBinding(DefaultParameterSetName = "Return")]
    Param(
        [Parameter(Mandatory=$true,Position=0)]
        [string]$Server,
        [Parameter(Position=1)]
        [uint16]$Port=443,
        [Parameter(Mandatory=$false, ParameterSetName="RawObj")]
        [switch]$Raw,
        [Parameter(Mandatory=$false, ParameterSetName="Export")]
        [switch]$Export,
        [Parameter(Mandatory=$true, ParameterSetName="Export")]
        [string]$Path,
        [uint16]$Timeout=3000
    )
    #code here
}
显然,这也是一种选择:

Param(
    [Parameter(Position=0,
        Mandatory=$True,
        ValueFromPipeline=$True)]
    [string]$userName,
    ...
)
但是,我不知道为什么要使用第二个,或者使用第二个的好处是什么。

第二个
Param()
块允许您进行大量高级参数验证

如果我需要编写一个具有最小输入验证的短函数,我将使用如下内容:

function Do-Something ($variable1, $variable2, $variable3....) 
    { Do Stuff }
Function Test-Port ([string]$Server,[uint16]$Port=80,[uint16]$Timeout = 3000) {
    #code here
}
function Get-SiteCert{
    [CmdletBinding(DefaultParameterSetName = "Return")]
    Param(
        [Parameter(Mandatory=$true,Position=0)]
        [string]$Server,
        [Parameter(Position=1)]
        [uint16]$Port=443,
        [Parameter(Mandatory=$false, ParameterSetName="RawObj")]
        [switch]$Raw,
        [Parameter(Mandatory=$false, ParameterSetName="Export")]
        [switch]$Export,
        [Parameter(Mandatory=$true, ParameterSetName="Export")]
        [string]$Path,
        [uint16]$Timeout=3000
    )
    #code here
}
但是如果我需要用高级检查写一些东西,比如:

function Do-Something ($variable1, $variable2, $variable3....) 
    { Do Stuff }
Function Test-Port ([string]$Server,[uint16]$Port=80,[uint16]$Timeout = 3000) {
    #code here
}
function Get-SiteCert{
    [CmdletBinding(DefaultParameterSetName = "Return")]
    Param(
        [Parameter(Mandatory=$true,Position=0)]
        [string]$Server,
        [Parameter(Position=1)]
        [uint16]$Port=443,
        [Parameter(Mandatory=$false, ParameterSetName="RawObj")]
        [switch]$Raw,
        [Parameter(Mandatory=$false, ParameterSetName="Export")]
        [switch]$Export,
        [Parameter(Mandatory=$true, ParameterSetName="Export")]
        [string]$Path,
        [uint16]$Timeout=3000
    )
    #code here
}
我肯定不会把它们都放在最上面的栏中,尽管它们是相似的脚本,但第二个脚本“做”的更多。这实际上只是一个个案的基础


您可以查看扩展参数的示例,但如果您不需要这些参数,请随意继续使用任何您喜欢的参数。

正如@ConnorLSW在上述答案中所述,验证是最大的好处之一。使用
Param
块,您可以使用
Validate
属性,如:

Function Foo
{
Param(
    [Parameter(Mandatory=$true,Position=0)]
    [ValidateSet("Tom","Dick","Jane")]
    [String]
    $Name
,
    [ValidateRange(21,65)]
    [Int]
    $Age
,
    [ValidateScript({Test-Path $_ -PathType 'Container'})]
    [string]
    $Path
)
Process
{
    write-host "New-Foo"
}
}

如果函数应支持不同的参数组合,则还可以定义不同的参数集。此外,如果您是
参数
属性的
强制属性
位置属性
属性,您还可以通过
获取帮助
获取“开箱即用”文档。例如:

 get-help Foo -Detailed

 NAME
   Foo

 SYNTAX
Foo [-Name] {Tom | Dick | Jane} [[-Age] <int>] [-Path <string>]  [<CommonParameters>]


PARAMETERS
-Age <int>

-Name <string>

-Path <string>

<CommonParameters>
    This cmdlet supports the common parameters: Verbose, Debug,
    ErrorAction, ErrorVariable, WarningAction, WarningVariable,
    OutBuffer, PipelineVariable, and OutVariable. For more information, see 
    about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 


ALIASES
   None

REMARKS
   None
获取帮助Foo-详细信息
名称
福
语法
Foo[-Name]{Tom | Dick | Jane}[-Age][-Path][]
参数
-年龄
-名字
-路径
此cmdlet支持常用参数:详细、调试、,
ErrorAction,ErrorVariable,WarningAction,WarningVariable,
exputffer、PipelineVariable和OutVariable。有关详细信息,请参阅
关于公共参数(http://go.microsoft.com/fwlink/?LinkID=113216). 
别名
没有一个
评论
没有一个
根据
Age
参数的外括号,您将看到它是一个可选参数。因此,所有这些都是关于描述、验证和文档的


希望能有所帮助。

谢谢您提供的信息。澄清了不少。我的新脚本需要第二个块。