什么是 [cmdletbinding()] 以及它是如何工作的? [英] What is [cmdletbinding()] and how does it work?

查看:20
本文介绍了什么是 [cmdletbinding()] 以及它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据get-help about_Functions_CmdletBindingAttribute

CmdletBinding 属性是函数的一个属性,使它们像编译的 cmdlet 一样运行

The CmdletBinding attribute is an attribute of functions that makes them operate like compiled cmdlets

我们可以在脚本的顶部使用它.这种情况下的功能是什么?PowerShell 引擎为其所有输入调用的内部隐式main"函数?

We can use it on the top of our scripts. What is the function in this case? An internal implicit "main" function called by the PowerShell engine for all its inputs?

关于这个语法:

[CmdletBinding(ConfirmImpact=<String>,
                     DefaultParameterSetName=<String>,
                     HelpURI=<URI>,
                     SupportsPaging=<Boolean>,
                     SupportsShouldProcess=<Boolean>,
                     PositionalBinding=<Boolean>)]

我们在做什么?实例化一个 cmdlbinding 对象并将参数列表传递给它的构造函数?此语法可以在 param() 中找到 - 例如:[Parameter(ValueFromPipeline=$true)].这个语法有特定的名称吗,可以在别处找到吗?

What are we doing? Instantiating a cmdlbinding object and passing an argument list to its constructor? This syntax can be found in param() - for example: [Parameter(ValueFromPipeline=$true)]. Does this syntax have a particular name, and can it be found elsewhere?

最后,作为简单的 PowerShell 用户,我们能否通过设置属性来模仿此功能并修改脚本的行为?

Lastly, are we able, as simple PowerShellers, to mimic this functionality and modify the behavior of scripts by setting an attribute?

推荐答案

CmdletBinding、Parameter 等是脚本编写者可以用来定义 PowerShell 行为的特殊属性类,例如使用 Cmdlet 功能使函数成为高级函数.

CmdletBinding, Parameter etc. are special attribute classes that scripters can use to define PowerShell's behavior, e.g. make a function an Advanced function with Cmdlet capabilites.

当您通过例如呼叫他们时[CmdletBinding()] 你初始化一个新的类实例.

When you call them via e.g. [CmdletBinding()] you initialize a new instance of the class.

在以下位置阅读有关 CmdletBindingAttribute 类的更多信息:MSDN

Read more about the CmdletBindingAttribute class at: MSDN

在以下位置阅读有关 ParameterAttribute 类的更多信息:MSDN

Read more about the ParameterAttribute class at: MSDN

有关属性类的更多信息此处此处

More about Attribute classes here and here

这篇关于什么是 [cmdletbinding()] 以及它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆