PowerTip of the Day-Make Function Parameters Mandatory

本文介绍了PowerShell中Mandatory参数的使用方法。Mandatory参数是一种特殊参数,如果用户未提供该参数,PowerShell会提示用户输入。文章通过示例展示了如何在自定义函数中设置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此处对应中文的意思不是很清楚,强迫?义务?强制?

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

转载于:https://www.cnblogs.com/aspnetx/archive/2010/07/08/1773573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值