Repository navigation
Expand file tree
/
Copy pathRead-PSMDScript.ps1
More file actions
82 lines (71 loc) · 1.97 KB
/
Copy pathRead-PSMDScript.ps1
File metadata and controls
82 lines (71 loc) · 1.97 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function Read-PSMDScript
{
<#
.SYNOPSIS
Parse the content of a script
.DESCRIPTION
Uses the powershell parser to parse the content of a script or scriptfile.
.PARAMETER ScriptCode
The scriptblock to parse.
.PARAMETER Path
Path to the scriptfile to parse.
Silently ignores folder objects.
.EXAMPLE
PS C:\> Read-PSMDScript -ScriptCode $ScriptCode
Parses the code in $ScriptCode
.EXAMPLE
PS C:\> Get-ChildItem | Read-PSMDScript
Parses all script files in the current directory
#>
[Alias('parse')]
[CmdletBinding()]
param (
[Parameter(Position = 0, ParameterSetName = 'Script', Mandatory = $true)]
[System.Management.Automation.ScriptBlock]
$ScriptCode,
[Parameter(Mandatory = $true, ParameterSetName = 'File', ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('FullName')]
[string[]]
$Path
)
begin
{
Write-PSFMessage -Level InternalComment -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")" -Tag 'debug', 'start', 'param'
}
process
{
foreach ($file in $Path)
{
Write-PSFMessage -Level Verbose -Message "Processing $file" -Target $file
$item = Get-Item $file
if ($item.PSIsContainer)
{
Write-PSFMessage -Level Verbose -Message "is folder, skipping $file" -Target $file
continue
}
$tokens = $null
$errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($item.FullName, [ref]$tokens, [ref]$errors)
[pscustomobject]@{
PSTypeName = 'PSModuleDevelopment.Meta.ParseResult'
Ast = $ast
Tokens = $tokens
Errors = $errors
File = $item.FullName
}
}
if ($ScriptCode)
{
$tokens = $null
$errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseInput($ScriptCode, [ref]$tokens, [ref]$errors)
[pscustomobject]@{
PSTypeName = 'PSModuleDevelopment.Meta.ParseResult'
Ast = $ast
Tokens = $tokens
Errors = $errors
Source = $ScriptCode
}
}
}
}