Test-PSOneScript.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89


function Test-PSOneScript
{
  <#
      .SYNOPSIS
      Parses a PowerShell Script (*.ps1, *.psm1, *.psd1)
 
      .DESCRIPTION
      Invokes the simple PSParser and returns tokens and syntax errors
 
      .EXAMPLE
      Test-PSOneScript -Path c:\test.ps1
      Parses the content of c:\test.ps1 and returns tokens and syntax errors
 
      .EXAMPLE
      Get-ChildItem -Path $home -Recurse -Include *.ps1,*.psm1,*.psd1 -File |
         Test-PSOneScript |
         Out-GridView
 
      parses all PowerShell files found anywhere in your user profile
 
      .EXAMPLE
      Get-ChildItem -Path $home -Recurse -Include *.ps1,*.psm1,*.psd1 -File |
         Test-PSOneScript |
         Where-Object Errors
 
      parses all PowerShell files found anywhere in your user profile
      and returns only those files that contain syntax errors
 
      .LINK
      https://powershell.one/powershell-internals/parsing-and-tokenization/simple-tokenizer
      https://github.com/TobiasPSP/Modules.PSOneTools/blob/master/PSOneTools/1.4/Test-PSOneScript.ps1
 
  #>


  [CmdletBinding(DefaultParameterSetName='Path')]
  param
  (
    # Path to PowerShell script file
    # can be a string or any object that has a "Path"
    # or "FullName" property:
    [String]
    [Parameter(Mandatory,ValueFromPipeline,ParameterSetName='Path')]
    [Alias('FullName')]
    $Path,

    # PowerShell Code as String
    # you can also submit a ScriptBlock which will automatically be converted
    # to a string. ScriptBlocks by default cannot contain syntax errors because
    # they are parsed already.
    [String]
    [Parameter(Mandatory,ValueFromPipeline,ParameterSetName='Code')]
    $Code
  )
  
  begin
  {
    $errors = $null
  }
  process
  {
    # create a variable to receive syntax errors:
    $errors = $null
    # tokenize PowerShell code:

    # if a path was submitted, read code from file,
    if ($PSCmdlet.ParameterSetName -eq 'Path')
    {
        $code = Get-Content -Path $Path -Raw -Encoding Default
        $name = Split-Path -Path $Path -Leaf
        $filepath = $Path
    }
    else
    {
        # else the code is already present in $Code
        $name = $Code
        $filepath = ''
    }

    # return the results as a custom object
    [PSCustomObject]@{
      Name = $name
      Path = $filepath
      Tokens = [Management.Automation.PSParser]::Tokenize($code, [ref]$errors)
      Errors = $errors | Select-Object -ExpandProperty Token -Property Message
    }  
  }
}