windows 我们可以看到 PowerShell cmdlet 的源代码吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/266250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we see the source code for PowerShell cmdlets?
提问by
I'm learning some PowerShell. Is it possible to see the source code for a built-in cmdlet like Get-ChildItem?
我正在学习一些 PowerShell。是否可以查看像Get-ChildItem这样的内置 cmdlet 的源代码?
回答by halr9000
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.
实际上,最好的办法是查看PowerShell Community Extensions。这个开源软件社区项目“旨在提供一组广泛有用的附加 cmdlet……”。该项目的开发人员是 PowerShell MVP,了解他们的东西。
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:
至于在现有的 PowerShell cmdlet 上使用反射,PowerShell MVP Oisin Grehan 制作了一个名为“ Reflect-Cmdlet”的方便的函数。我不会窃取他的代码并放在这里,但基本上你要做的是:
Get-Command Get-ChildItem | Reflect-Cmdlet
And then .NET Reflectorpops up with the right assembly opened up and expanded and everything. It's really pretty cool. Here's a screenshot:
然后.NET Reflector弹出,打开和扩展正确的程序集以及所有内容。这真的很酷。这是一个屏幕截图:
Alt text http://halr9000.com/images/screenshots/reflector.png
回答by Zev Spitz
The source for Powershell is now available on Github.
The source for Get-ChildItem
can be found here.
Powershell 的源代码现在可以在 Github 上找到。
的来源Get-ChildItem
可以在这里找到。
回答by ImpossibleSqui
I think if you were just starting PowerShell, this is what you'd be looking for:
我想如果您刚刚开始使用 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
This will create a script which shows how Get-Processruns. 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.
这将创建一个脚本,显示Get-Process 的运行方式。输入要替换 Get-Process 的任何 cmdlet。如果您想在谷歌上搜索更多相关信息,这就是您创建代理功能的方式。
回答by Michael Kropat
For compiledCmdlets, you can get the path to the .dll
with:
对于已编译的Cmdlet,您可以通过以下方式获取路径.dll
:
(Get-Command Get-ChildItem).DLL
(Replace Get-ChildItem
with the cmdlet you are interested in)
(替换Get-ChildItem
为您感兴趣的 cmdlet)
Once you know the path to the .dll
, you can open it with a .NET disassembler like dotPeek:
一旦知道.dll
了 .NET的路径,就可以使用dotPeek 之类的 .NET 反汇编程序打开它:
& dotPeek64.exe (Get-Command Get-ChildItem).DLL
回答by dalle
You should be able to use .NET Reflectorto "see" the source code. You need to know the assembly though, but it should also accessible using the GetType method or similar.
您应该能够使用.NET Reflector来“查看”源代码。虽然您需要了解程序集,但也应该使用 GetType 方法或类似方法访问它。
This PowerShellLanguage .NET Reflector Add-Incan perhaps be useful.
回答by Stephen Harrison
You might also like to take a look at Windows Installer PowerShell Snap-Inon CodePlex. It's a smaller project than the community extensions, so it is easier to get your head around what's going on.
您可能还想查看CodePlex上的Windows Installer PowerShell 管理单元。这是一个比社区扩展更小的项目,因此更容易了解正在发生的事情。
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.
查看专业 Windows PowerShell 编程:管理单元、Cmdlets、主机和提供程序(Wrox 专业指南),ISBN:0470173939 - 这是我发现的编写 cmdlet 和提供程序的最有用的书籍之一。
回答by ssvinarenko
PowerShell cmdlets' assemblies are located in GAC. You can find "Get-ChildItem" cmdlet in:
PowerShell cmdlet 的程序集位于 GAC 中。您可以在以下位置找到“Get-ChildItem”cmdlet:
Microsoft.PowerShell.Commands.Management assembly, Microsoft.PowerShell.Commands.GetChildItemCommandclass.
Microsoft.PowerShell.Commands.Management 程序集、Microsoft.PowerShell.Commands.GetChildItemCommand类。
I've used ILSpy .NET decompiler and filtered GAC assemblies by "powershell" string. As I understand, Microsoft.PowerShell.Commands.*assemblies contain built-in cmdlets.
我使用了 ILSpy .NET 反编译器并通过“powershell”字符串过滤了 GAC 程序集。据我了解,Microsoft.PowerShell.Commands.*程序集包含内置的 cmdlet。
回答by EBGreen
I do not believe that the source code for PowerShell has ever been released.
我不相信 PowerShell 的源代码曾经被发布过。
回答by JohnLBevan
Some code can be found on the Reference Resource Site: http://referencesource.microsoft.com/#System.Management.Automation/System/Management/Automation/ChildItemCmdletProviderIntrinsics.cs,c6eed9f6a5417c19
一些代码可以在参考资源站点上找到:http: //referencesource.microsoft.com/#System.Management.Automation/System/Management/Automation/ChildItemCmdletProviderIntrinsics.cs,c6eed9f6a5417c19
This only gives the outline though; not the code's detail.
不过,这只给出了大纲;不是代码的细节。