by oising
30. October 2008 18:44
Fellow PowerShell MVP and developer all-round expert administrator Brandon Shell asked me how he could navigate to a Cmdlet’s implementation in Lutz Roeder’s Reflector -- which has now been acquired by Red-Gate Software btw. At first I was going to explain to him how snap-ins work and how to figure out where things are, and then a really simple trick hit me that uses some fairly secret command-line parameter that Reflector can accept, namely the /select parameter:
- function Reflect-Cmdlet {
- param([Management.Automation.CommandInfo]$command)
- if ($input) {
- trap { $_; break }
- $command = $input | select -first 1
- }
-
-
- while ($command.CommandType -eq "Alias") {
- $command = Get-Command ($command.definition)
- }
-
- $name = $command.ImplementingType
- $DLL = $command.DLL
-
- if (-not (gcm reflector.exe -ea silentlycontinue)) {
- throw "I can't find Reflector.exe in your path."
- }
-
- reflector /select:$name $DLL
- }
Just pipe the output of get-command to it, like: gcm dir | reflect-cmdlet and Reflector will open up with the class selected (it takes a few seconds).
Update: Doug pointed out in a comment that the gcm reflector.exe line could benefit from an erroraction to keep it silent on failure, so only the throw message shows. Thanks Doug!