PowerTip of the Day-Make Function Parameters Mandatory

原文地址:http://app.en25.com/e/es.aspx?s=1403&e=5441&elq=3d32771b868b437aa97567a6a50b5563

原文:

Mandatory parameters are special because if you do not submit it, PowerShell will automatically ask for it. You can use mandatory parameters in your own functions as well. You should simply mark a function as mandatory by adding the appropriate [Parameter()] attribute like so:

function Test-Me {
   
param(
          [
Parameter(Mandatory=$true)]
          
$name
    )
   
"You entered $name."
}

Test-Me

The parameter attributes used in this tip were introduced with PowerShell v2.

 

 

翻译:

Mandatory参数是很特殊的因为这种参数是在你不提交的时候,PowerShell会自动让你输入的。同样可以在你自己的函数里使用Mandatory参数。只需要用标记[Parameter()]属性标记在方法前面就可以了,比如:

function Test-Me {
   
param(
          [
Parameter(Mandatory=$true)]
          
$name
    )
   
"You entered $name."
}

Test-Me

这种参数属性的使用在PowerShell v2中就已经被介绍过了。

 

 

笔记:

Mandatory此处对应中文的意思不是很清楚,强迫?义务?强制?

最后一句话看得不是很懂。

posted @ 2010-07-08 14:14  哥本哈士奇(aspnetx)  阅读(380)  评论(0编辑  收藏  举报