Last active
November 18, 2018 17:11
-
-
Save ChrisLGardner/b94c5149a458067de58b15d4c511bb9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Namespace System.Management.Automation.Language | |
Function Get-AliasTarget { | |
[cmdletbinding()] | |
param ( | |
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] | |
[Alias('PSPath', 'FullName')] | |
[string[]]$Path | |
) | |
process { | |
foreach ($File in $Path) { | |
$FileAst = [Parser]::ParseFile($File, [ref]$null, [ref]$null) | |
$FunctionName = $FileAst.FindAll( { | |
param ($ast) | |
$ast -is [FunctionDefinitionAst] | |
}, $true).Name | |
$AliasDefinitions = $FileAst.FindAll( { | |
param ($ast) | |
$ast -is [StringConstantExpressionAst] -And $ast.Value -match '(New|Set)-Alias' | |
}, $true) | |
$AliasTarget = $AliasDefinitions.Parent.CommandElements.Where( { | |
$_.StringConstantType -eq 'BareWord' -and | |
$_.Value -notin ('New-Alias', 'Set-Alias', $FunctionName) | |
}).Value | |
$Attributes = $FileAst.FindAll( { | |
param ($ast) | |
$ast -is [AttributeAst] | |
}, $true) | |
$AliasDefinitions = $Attributes.Where( {$_.TypeName.Name -eq 'Alias' -and $_.Parent -is [ParamBlockAst]}) | |
$AliasTarget += $AliasDefinitions.PositionalArguments.Value | |
[PsCustomObject]@{ | |
Function = $FunctionName | |
Alias = $AliasTarget | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment