forked from lazywinadmin/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-ScriptAlias.ps1
74 lines (64 loc) · 2.72 KB
/
Get-ScriptAlias.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function Get-ScriptAlias {
<#
.SYNOPSIS
Function to retrieve the aliases inside a Powershell script file.
.DESCRIPTION
Function to retrieve the aliases inside a Powershell script file.
Using PowerShell AST Parser we are able to retrieve the functions and cmdlets used in the script.
.PARAMETER Path
Specifies the path of the script
.EXAMPLE
Get-ScriptAlias -Path "C:\LazyWinAdmin\testscript.ps1"
.EXAMPLE
"C:\LazyWinAdmin\testscript.ps1" | Get-ScriptAlias
.EXAMPLE
gci C:\LazyWinAdmin -file | Get-ScriptAlias
.NOTES
Francois-Xavier Cat
lazywinadmin.com
@lazywinadmin
.LINK
https://github.com/lazywinadmin/PowerShell
#>
[CmdletBinding()]
PARAM
(
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateScript( { Test-Path -Path $_ })]
[Alias("FullName")]
[System.String[]]$Path
)
PROCESS {
FOREACH ($File in $Path) {
TRY {
# Retrieve file content
$ScriptContent = (Get-Content $File -Delimiter $([char]0))
# AST Parsing
$AbstractSyntaxTree = [System.Management.Automation.Language.Parser]::
ParseInput($ScriptContent, [ref]$null, [ref]$null)
# Find Aliases
$AbstractSyntaxTree.FindAll( { $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true) |
ForEach-Object -Process {
$Command = $_.CommandElements[0]
if ($Alias = Get-Alias | Where-Object -FilterScript { $_.Name -eq $Command }) {
# Output information
[PSCustomObject]@{
File = $File
Alias = $Alias.Name
Definition = $Alias.Definition
StartLineNumber = $Command.Extent.StartLineNumber
EndLineNumber = $Command.Extent.EndLineNumber
StartColumnNumber = $Command.Extent.StartColumnNumber
EndColumnNumber = $Command.Extent.EndColumnNumber
StartOffset = $Command.Extent.StartOffset
EndOffset = $Command.Extent.EndOffset
}#[PSCustomObject]
}#if ($Alias)
}#ForEach-Object
}#TRY
CATCH {
Write-Error -Message $($Error[0].Exception.Message)
} #CATCH
}#FOREACH ($File in $Path)
} #PROCESS
}