Can we see the source code for PowerShell cmdlets?

邮差的信 提交于 2019-11-27 12:47:16

问题


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


回答1:


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. Here's a screenshot:

Alt text http://halr9000.com/images/screenshots/reflector.png




回答2:


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




回答3:


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.




回答4:


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)

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



回答5:


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




回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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.



来源:https://stackoverflow.com/questions/266250/can-we-see-the-source-code-for-powershell-cmdlets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!