Get-ScriptToken.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
function Get-ScriptToken {
    <#
    .Synopsis
        Gets the tokens within a script block or file
    .Description
        Gets the PowerShell script tokens from a script block or file.
    .Example
        Get-ScriptToken {
            function foo() {
                "foo"
            }
            function bar() {
                "bar"
            }
        }
    .Link
        Get-FunctionFromScript
    #>

    param(
    # The ScriptBlock that will be parsed.
    [Parameter(Mandatory=$true,
        ParameterSetName="ScriptBlock",
        ValueFromPipelineByPropertyName=$true)]    
    [ScriptBlock]
    $ScriptBlock,
    
    # The file that will be parsed.
    [Parameter(Mandatory=$true,
        ParameterSetName="File",
        ValueFromPipelineByPropertyName=$true)]           
    [Alias('FullName')]
    [String]
    $File
    )
    
    process {
        if ($psCmdlet.ParameterSetName -eq "File") {
            $realFile = Get-Item $File
            if (-not $realFile) {
                $realFile = Get-Item -LiteralPath $File -ErrorAction SilentlyContinue
                if (-not $realFile) { 
                    return
                }
            }
            $text = [IO.File]::ReadAllText($realFile.Fullname)
            $scriptBlock = [ScriptBlock]::Create($text)
            Get-ScriptToken -ScriptBlock $scriptBlock
        } elseif ($psCmdlet.ParameterSetName -eq "ScriptBlock") {            
            $text = $scriptBlock.ToString()
            $tokens = [Management.Automation.PSParser]::Tokenize($scriptBlock, [ref]$null)            
            $tokens
        }        
    }
}