1
+ function Expand-ScriptAlias
2
+ {
3
+ <#
4
+ . SYNOPSIS
5
+ Function to replace Aliases used in a script by their fullname
6
+
7
+ . DESCRIPTION
8
+ Function to replace Aliases used in a script by their fullname.
9
+ Using PowerShell AST we are able to retrieve the functions and cmdlets used in a script.
10
+
11
+ . PARAMETER Path
12
+ Specifies the Path to the file.
13
+ Alias: FullName
14
+
15
+ . EXAMPLE
16
+ "C:\LazyWinAdmin\testscript.ps1", "C:\LazyWinAdmin\testscript2.ps1" | Expand-ScriptAlias
17
+
18
+ . EXAMPLE
19
+ gci C:\LazyWinAdmin -File | Expand-ScriptAlias
20
+
21
+ . EXAMPLE
22
+ Expand-ScriptAlias -Path "C:\LazyWinAdmin\testscript.ps1"
23
+
24
+ . EXAMPLE
25
+ "C:\LazyWinAdmin\testscript.ps1", "C:\LazyWinAdmin\testscript2.ps1" | Expand-ScriptAlias -Confirm
26
+
27
+ . EXAMPLE
28
+ "C:\LazyWinAdmin\testscript.ps1", "C:\LazyWinAdmin\testscript2.ps1" | Expand-ScriptAlias -WhatIf
29
+
30
+ What if: Performing the operation "Expand Alias: select to Select-Object (startoffset: 15)" on target "C:\LazyWinAdmin\testscript2.ps1".
31
+ What if: Performing the operation "Expand Alias: sort to Sort-Object (startoffset: 10)" on target "C:\LazyWinAdmin\testscript2.ps1".
32
+ What if: Performing the operation "Expand Alias: group to Group-Object (startoffset: 4)" on target "C:\LazyWinAdmin\testscript2.ps1".
33
+ What if: Performing the operation "Expand Alias: gci to Get-ChildItem (startoffset: 0)" on target "C:\LazyWinAdmin\testscript2.ps1".
34
+
35
+ . NOTES
36
+ Francois-Xavier Cat
37
+ www.lazywinadmin.com
38
+ @lazywinadm
39
+ #>
40
+ [CmdletBinding (SupportsShouldProcess , ConfirmImpact = ' Low' )]
41
+ PARAM (
42
+ [Parameter (ValueFromPipeline , ValueFromPipelineByPropertyName )]
43
+ [ValidateScript ({ Test-Path - Path $_ })]
44
+ [Alias (' FullName' )]
45
+ [System.String ]$Path
46
+ )
47
+ PROCESS
48
+ {
49
+ FOREACH ($File in $Path )
50
+ {
51
+ Write-Verbose - Message ' [PROCESS] $File'
52
+
53
+ TRY
54
+ {
55
+ # Retrieve file content
56
+ $ScriptContent = (Get-Content $File - Delimiter $ ([char ]0 ))
57
+
58
+ # AST Parsing
59
+ $AbstractSyntaxTree = [System.Management.Automation.Language.Parser ]::
60
+ ParseInput($ScriptContent , [ref ]$null , [ref ]$null )
61
+
62
+ # Find Aliases
63
+ $Aliases = $AbstractSyntaxTree.FindAll ({ $args [0 ] -is [System.Management.Automation.Language.CommandAst ] }, $true ) |
64
+ ForEach-Object - Process {
65
+ $Command = $_.CommandElements [0 ]
66
+ if ($Alias = Get-Alias | Where-Object { $_.Name -eq $Command })
67
+ {
68
+
69
+ # Output information
70
+ [PSCustomObject ]@ {
71
+ File = $File
72
+ Alias = $Alias.Name
73
+ Definition = $Alias.Definition
74
+ StartLineNumber = $Command.Extent.StartLineNumber
75
+ EndLineNumber = $Command.Extent.EndLineNumber
76
+ StartColumnNumber = $Command.Extent.StartColumnNumber
77
+ EndColumnNumber = $Command.Extent.EndColumnNumber
78
+ StartOffset = $Command.Extent.StartOffset
79
+ EndOffset = $Command.Extent.EndOffset
80
+
81
+ }# [PSCustomObject]
82
+ }# if ($Alias)
83
+ } | Sort-Object - Property EndOffset - Descending
84
+
85
+ # The sort-object is important, we change the values from the end first to not lose the positions of every aliases.
86
+ Foreach ($Alias in $Aliases )
87
+ {
88
+ # whatif and confirm support
89
+ if ($psCmdlet.ShouldProcess ($file , " Expand Alias: $ ( $Alias.alias ) to $ ( $Alias.definition ) (startoffset: $ ( $alias.StartOffset ) )" ))
90
+ {
91
+ # Remove alias and insert full cmldet name
92
+ $ScriptContent = $ScriptContent.Remove ($Alias.StartOffset , ($Alias.EndOffset - $Alias.StartOffset )).Insert($Alias.StartOffset , $Alias.Definition )
93
+ # Apply to the file
94
+ Set-Content - Path $File - Value $ScriptContent - Confirm:$false
95
+ }
96
+ }# ForEach Alias in Aliases
97
+
98
+ }# TRY
99
+ CATCH
100
+ {
101
+ Write-Error - Message $ ($Error [0 ].Exception.Message)
102
+ }
103
+ }# FOREACH File in Path
104
+ }# PROCESS
105
+ }# Expand-ScriptAlias
0 commit comments