在上面的示例函数中,参数$p1是必选参数,因为设置了Mandatory=$true,而$p2没有做任何设置,默认是可选的。按照PowerShell函数定义的Best Practices,可选参数都要设置一个默认值的,这点要记住。 在调用这个函数的时候,如果我们直接运行Test-Function而不输入参数,系统提示我们输入p1。 复制代码 代码如下: PS> Test-Function cmdlet Test-Me at command pipeline position 1 Supply values for the following parameters: p1: