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

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

问题描述

根据获得关于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?

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

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天全站免登陆