Skip to content

Instantly share code, notes, and snippets.

@ChrisLGardner
Last active November 18, 2018 17:11
Show Gist options
  • Save ChrisLGardner/b94c5149a458067de58b15d4c511bb9b to your computer and use it in GitHub Desktop.
Save ChrisLGardner/b94c5149a458067de58b15d4c511bb9b to your computer and use it in GitHub Desktop.
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