我们可以查看 PowerShell cmdlet 的源代码吗?

发布于 2024-07-07 17:34:36 字数 165 浏览 14 评论 0原文

我正在学习一些 PowerShell。 是否可以查看内置 cmdlet 的源代码,例如 Get-ChildItem< /a>?

I'm learning some PowerShell. Is it possible to see the source code for a built-in cmdlet like Get-ChildItem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

╰沐子 2024-07-14 17:34:36

Powershell 源代码现已在 Github 上提供。
可以找到 Get-ChildItem 的源代码 此处

The source for Powershell is now available on Github.
The source for Get-ChildItem can be found here.

微凉徒眸意 2024-07-14 17:34:36

实际上,最好的选择是查看 PowerShell 社区扩展。 这个开源软件社区项目“旨在提供一组广泛有用的附加 cmdlet...”。 该项目的开发人员都是 PowerShell MVP,并且了解他们的知识。

至于在现有 PowerShell cmdlet 上使用反射,PowerShell MVP Oisin Grehan 制作了一个方便的函数,名为“Reflect-Cmdlet"。 我不会窃取他的代码并将其放在这里,但基本上你要做的是:

Get-Command Get-ChildItem | Reflect-Cmdlet

然后 .NET Reflector 弹出,右侧组件打开并展开,以及所有内容。 真的很酷。

Actually, your best bet is to go check out PowerShell Community Extensions. This open source software community project is "aimed at providing a widely useful set of additional cmdlets...". The developers on the project are PowerShell MVPs and know their stuff.

As far as using reflection on the existing PowerShell cmdlets, PowerShell MVP Oisin Grehan made a handy function titled "Reflect-Cmdlet". I won't steal his code and place it here, but basically what you do is:

Get-Command Get-ChildItem | Reflect-Cmdlet

And then .NET Reflector pops up with the right assembly opened up and expanded and everything. It's really pretty cool.

靖瑶 2024-07-14 17:34:36

对于编译 Cmdlet,您可以使用以下方式获取 .dll 的路径:(

(Get-Command Get-ChildItem).DLL

Get-ChildItem 替换为您感兴趣的 cmdlet)

< strong>示例:

PS C:\Windows\system32> (Get-Command Get-StoragePool).DLL

PS C:\Windows\system32> 

一旦知道 .dll 的路径,您就可以使用 .NET 反汇编程序打开它,例如 dotPeek

& dotPeek64.exe (Get-Command Get-ChildItem).DLL

For compiled Cmdlets, you can get the path to the .dll with:

(Get-Command Get-ChildItem).DLL

(Replace Get-ChildItem with the cmdlet you are interested in)

Example:

PS C:\Windows\system32> (Get-Command Get-StoragePool).DLL

PS C:\Windows\system32> 

Once you know the path to the .dll, you can open it with a .NET disassembler like dotPeek:

& dotPeek64.exe (Get-Command Get-ChildItem).DLL
假面具 2024-07-14 17:34:36

我想如果您刚刚启动 PowerShell,这就是您所需要的:

$metadata = New-Object system.management.automation.commandmetadata (Get-Command Get-Process)
[System.management.automation.proxycommand]::Create($MetaData) | out-file C:\powershell\get-process.ps1

这将创建一个脚本,显示如何 Get-Process 运行。 输入要替换 Get-Process 的任何 cmdlet。 如果您想通过谷歌搜索更多相关信息,这就是创建代理函数的方法。

I think if you were just starting PowerShell, this is what you'd be looking for:

$metadata = New-Object system.management.automation.commandmetadata (Get-Command Get-Process)
[System.management.automation.proxycommand]::Create($MetaData) | out-file C:\powershell\get-process.ps1

This will create a script which shows how Get-Process runs. Put in any cmdlet you want to replace Get-Process. If you want to google more about it, this is how you would create a proxy function.

痴意少年 2024-07-14 17:34:36

您可能还想查看 CodePlex 上的 Windows Installer PowerShell 管理单元。 这是一个比社区扩展更小的项目,因此您可以更轻松地了解正在发生的事情。

查看《专业 Windows PowerShell 编程:管理单元、Cmdlet、主机和提供程序》(Wrox 专业指南),ISBN:0470173939 - 这是我发现的关于编写 cmdlet 和提供程序最有用的书籍之一。

You might also like to take a look at Windows Installer PowerShell Snap-In on CodePlex. It's a smaller project than the community extensions, so it is easier to get your head around what's going on.

Check out Professional Windows PowerShell Programming: Snapins, Cmdlets, Hosts and Providers (Wrox Professional Guides), ISBN: 0470173939 - it's one of the most useful books I've found for writing cmdlets and providers.

送舟行 2024-07-14 17:34:36

您应该能够使用 .NET Reflector 来“查看”源代码。 虽然您需要了解程序集,但也应该使用 GetType 方法或类似方法来访问它。

PowerShellLanguage .NET Reflector 加载项也许有用。

You should be able to use .NET Reflector to "see" the source code. You need to know the assembly though, but it should also accessible using the GetType method or similar.

This PowerShellLanguage .NET Reflector Add-In can perhaps be useful.

绝對不後悔。 2024-07-14 17:34:36

PowerShell cmdlet 的程序集位于 GAC 中。 您可以在以下位置找到“Get-ChildItem”cmdlet:

Microsoft.PowerShell.Commands.Management 程序集、Microsoft.PowerShell.Commands.GetChildItemCommand 类。

我使用了 ILSpy .NET 反编译器并通过“powershell”字符串过滤了 GAC 程序集。 据我了解,Microsoft.PowerShell.Commands.* 程序集包含内置 cmdlet。

PowerShell cmdlets' assemblies are located in GAC. You can find "Get-ChildItem" cmdlet in:

Microsoft.PowerShell.Commands.Management assembly, Microsoft.PowerShell.Commands.GetChildItemCommand class.

I've used ILSpy .NET decompiler and filtered GAC assemblies by "powershell" string. As I understand, Microsoft.PowerShell.Commands.* assemblies contain built-in cmdlets.

属性 2024-07-14 17:34:36

对于某些使用 Install-Module 安装且包含源代码而不是二进制文件的 cmdlet(例如 PSnmap):

Install-Module -Name PSnmap

...您可以通过查看 Definition 来查看其代码property from:

Get-Command Invoke-PSnmap | Format-List

如果没有,很可能您需要反编译一些二进制文件(例如 DLL 属性中指定的文件)。

For some cmdlets that are installed with Install-Module and contain source code and not binaries, (e.g. PSnmap):

Install-Module -Name PSnmap

...you can view their code by looking at the Definition property from:

Get-Command Invoke-PSnmap | Format-List

If not, most likely, you will need to decompile some binary (e.g. the file specified in the DLL property).

铜锣湾横着走 2024-07-14 17:34:36

一些代码可以在参考资源站点上找到:
http://referencesource.microsoft.com/ #System.Management.Automation/System/Management/Automation/ChildItemCmdletProviderIntrinsics.cs,c6eed9f6a5417c19

但这仅给出了概要; 不是代码的详细信息。

Some code can be found on the Reference Resource Site:
http://referencesource.microsoft.com/#System.Management.Automation/System/Management/Automation/ChildItemCmdletProviderIntrinsics.cs,c6eed9f6a5417c19

This only gives the outline though; not the code's detail.

回心转意 2024-07-14 17:34:36

我不相信 PowerShell 的源代码曾经被发布过。

I do not believe that the source code for PowerShell has ever been released.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文