由于@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"
}
}
你也可以定义不同的参数设置,如果你的函数应支持不同的参数组合。此外,如果您是Parameter
属性的Mandatory
和Positional
属性,则还会通过Get-Help
获得“开箱即用”文档。例如: -
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
基础上Age
参数你会认为这是可选参数的出括号。所以关于描述,验证和文档。
希望有所帮助。
感谢您的信息。澄清了很多。将需要我的新脚本的第二块。 – Jos