forked from jdhitsolutions/ISEScriptingGeek
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertFrom-Alias.ps1
More file actions
59 lines (50 loc) · 1.8 KB
/
ConvertFrom-Alias.ps1
File metadata and controls
59 lines (50 loc) · 1.8 KB
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
#requires -version 2.0
Function ConvertFrom-Alias {
Param (
[Parameter(Position=0)]
[ValidateNotNullorEmpty()]
$Text=$psISE.CurrentFile.Editor.text
)
#make sure we are using the ISE
if ($host.name -match "ISE"){
#Turn the script into syntax tokens
Write-Verbose "Tokenizing"
#verify there are no syntax errors first by Tokenizing the script
$out=$null
$tokens = [System.Management.Automation.PSparser]::Tokenize($text, [ref]$out)
#if there are errors they will be directed to $out
if ($out) {
#enumerate each parsing error in $out
foreach ($problem in $out) {
Write-Warning $problem.message
Write-Warning "Line: $($problem.Token.Startline) at character: $($problem.token.StartColumn)"
}
}
else {
#if no errors then proceed to convert
$tokens | Where-Object { $_.Type -eq 'Command'} |
Sort-Object StartLine, StartColumn -Descending |
ForEach-Object {
#handle the ? by escaping it
if($_.content -eq '?') {
Write-Verbose "Found a ?"
$result = Get-Command -name '`?' -CommandType Alias
}
else {
$result = Get-Command -name $_.Content -CommandType Alias -ErrorAction SilentlyContinue
}
#check and see if Get-Command returned anything
if($result) {
#find each command and insert the corresponding command definition
Write-Verbose "Replacing $($result.name) with $($result.definition)"
$psISE.CurrentFile.Editor.Select($_.StartLine,$_.StartColumn,$_.EndLine,$_.EndColumn)
$psISE.CurrentFile.Editor.InsertText($result.Definition)
}
} #foreach
} #else $tokens exists and there were no parsing errors
} #if ISE
else {
Write-Warning "You must be using the PowerShell ISE"
}
Write-Verbose "Finished"
} #end Function