forked from jdhitsolutions/ISEScriptingGeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ScriptComments.ps1
55 lines (45 loc) · 1.42 KB
/
Get-ScriptComments.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
#requires -version 4.0
#parse a script file for comments only
Function Get-ScriptComments {
<#
.Synopsis
Get comments from a PowerShell script file.
.Description
This command will use the AST parser to go through a PowerShell script, either a .ps1 or .psm1 file, and display only the comments.
.Example
PS C:\> get-scriptcomments c:\scripts\MyScript.ps1
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter the path of a PS1 file",
ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath","Name")]
[ValidateScript({Test-Path $_})]
[ValidatePattern("\.ps(1|m1)$")]
[string]$Path
)
Begin {
#Begin scriptblock
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
#initialization commands
#explicitly define some AST variables
New-Variable astTokens -force
New-Variable astErr -force
} #close begin
Process {
#Process scriptblock
#convert each path to a nice filesystem path
$Path= Convert-Path -Path $Path
Write-Verbose -Message "Parsing $Path"
#Parse the file
$ast = [System.Management.Automation.Language.Parser]::ParseFile($Path,[ref]$astTokens,[ref]$astErr)
#filter tokens for comments and display text
$asttokens.where({$_.kind -eq 'comment'}) |
Select-Object -ExpandProperty Text
} #close process
End {
#end scriptblock
#ending the function
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #close end
} #close function