简体   繁体   English

为什么脚本化cmdlet列为函数?

[英]Why are scripted cmdlets listed as functions?

If I create a simple Powershell function eg 如果我创建一个简单的Powershell函数,例如

Function Hello {
    [CmdletBinding( )]
    Param (
        [parameter()]
        $Name
    )
    Begin{}
    Process{
        Write-Output "Hello $Name"
    }
    End{}
    }

then use Get-Command to list it with Get-Command Hello , the cmdlet is listed as 'CommandType' function. 然后使用Get-Command将其列为Get-Command Hello ,cmdlet列为“CommandType”函数。 Why isn't it listed as 'CommandType' cmdlet? 为什么不将它列为'CommandType'cmdlet?

When exporting from modules I've also found I have to use FunctionsToExport instead of CmdletsToExport. 从模块导出时,我还发现我必须使用FunctionsToExport而不是CmdletsToExport。

It doesn't seem to affect the use of the functions, I'm just curious why they're listed like this. 它似乎没有影响函数的使用,我只是好奇为什么它们被列出这样。

There potentially isn't much difference between a function and a cmdlet, but that depends on how much work you're willing to put into writing the function. 函数和cmdlet之间可能没有太大区别,但这取决于您愿意为编写函数做多少工作。 Don Jones wrote an article on TechNet back when these first came out to highlight some of these differences. 唐·琼斯在TechNet上发表了一篇文章,当时第一篇文章突出强调了其中的一些差异。

These functions, written entirely in script, have the same capabilities as a “real” cmdlet written in C# or Visual Basic and compiled in Visual Studio. 这些函数完全用脚本编写,具有与用C#或Visual Basic编写并在Visual Studio中编译的“真实”cmdlet相同的功能。 These advanced functions (they were originally called “script cmdlets” early in the v2 development cycle) help you write more flexible functions you can then use seamlessly alongside regular cmdlets. 这些高级函数(它们最初在v2开发周期中称为“脚本cmdlet”)可帮助您编写更灵活的函数,然后可以与常规cmdlet无缝地一起使用。

... ...

The real difference between a mere function and a full cmdlet is that cmdlets support powerful parameter bindings. 纯粹的功能和完整的cmdlet之间的真正区别在于cmdlet支持强大的参数绑定。 You can use positional parameters, named parameters, mandatory parameters, and even do basic parameter validation checks—all by simply describing the parameter to the shell. 您可以通过简单地描述shell的参数来使用位置参数,命名参数,强制参数,甚至进行基本参数验证检查。

The code example you've offered already begins to blur the lines between the two by allowing a host of addition parameters via [CmdletBinding()] , and beginning to describe a brand new parameter called $Name . 您提供的代码示例已经开始通过[CmdletBinding()]允许一系列附加参数来模糊两者之间的界限,并开始描述一个名为$Name全新参数。 For example, you could now use Write-Verbose anywhere in that function, and call the -Verbose flag to see those statements without having to do any additional work. 例如,您现在可以在该函数中的任何位置使用Write-Verbose ,并调用-Verbose标志以查看这些语句,而无需执行任何其他工作。

Functionally, the end results of a compiled cmdlet or a function written in powershell don't have to be very different at all -- it seems to be more a matter of distinguishing compiled cmdlets from scripted functions. 从功能上讲,编译的cmdlet或用PowerShell编写的函数的最终结果根本不必非常不同 - 似乎更多的是将编译的cmdlet与脚本函数区分开来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM