PowerShell CmdLet reflecten

Meistens muss ich alles ganz genau wissen: Z.B. wie ein spezielles PowerShell-CmdLet implementiert ist.
Mittels des folgenden kleinen Skriptes​ kann man ganz schnell die implementierende DLL zu einem CmdLet finden und notfalls auch schon mal einen decompiler starten.​
Die Inspiration kommt von OISIN GREHAN​.

<#
.SYNOPSIS
Run reflection on a given commandlet
.DESCRIPTION
Run reflection on any CmdLet
.NOTES
This code was heavily inspired from OISIN GREHAN, see http://www.nivot.org/post/2008/10/30/ATrickToJumpDirectlyToACmdletsImplementationInReflector
.Example
Get-Command Get-ChildItem | Reflect-Cmdlet -Reflect ShowDllOnly
.Example
Reflect-Cmdlet -CmdLet (Get-Command Get-ChildItem) -Reflect ShowDllOnly
#>
[CmdletBinding()]
param(
[Parameter(Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage = "The CmdLet to reflect")]
[Management.Automation.CommandInfo]$CmdLet,
[Parameter(HelpMessage = "Which reflector to use.. ")]
[ValidateSet("ShowDllOnly", "Reflector", "JustDecompile", "ILSpy")]
[string]$Reflect = "ShowDllOnly"
)
# resolve to command if this is an alias
while ($CmdLet.CommandType -eq "Alias") {
$def = $CmdLet.definition;
Write-Verbose "$CmdLet is an alias. Using Definition: $def";
$CmdLet = Get-Command $def
}
Write-Verbose "Reflecting $CmdLet.";
$name = $CmdLet.ImplementingType
$DLL = $CmdLet.DLL
if($DLL -eq $null) {
Write-Warning "$CmdLet is not implemented in any DLL. Possibly a script?";
if($CmdLet.Path -ne $null) {
Write-Warning "Have a look at: $($CmdLet.Path)"
}
#$CmdLet | gm
Exit;
}
Write-Verbose "Type:$name, DLL:$DLL";
switch ($Reflect) {
"ShowDllOnly" {
Write-Output "$CmdLet is implemented in $name in the dll:$DLL";
}
"Reflector" {
if (-not (Get-Command reflector.exe -ErrorAction SilentlyContinue)) {
throw "Reflector.exe is not in your path."
}
Write-Verbose "Starting Reflector.";
reflector /select:$name $DLL
}
"JustDecompile" {
$regKey = Get-Item HKCU:\Software\Telerik\JustDecompile -ErrorAction SilentlyContinue
if($regKey -eq $null) {
throw "It seems JustDecompile is not installed."
}
$exe = $regKey.GetValue("ExecutablePath") ;
Write-Verbose "invoking $exe";
&$exe """$DLL""" ; #TODO: select the right type...
}
"ILSpy" {
if (-not (Get-Command ilspy.exe -ErrorAction SilentlyContinue)) {
throw "ilspy.exe is not in your path."
}
Write-Verbose "Starting ILSpy.";
ilspy $DLL /navigateTo:T:$name
}
}
view raw Relect-Cmdlet.ps1 hosted with ❤ by GitHub