PoshCode Archive  Artifact [c91a14346d]

Artifact c91a14346d1ffb39f0d35bc22f67fbfa6b60cb17bf751f56c65c8b75125e6405:

  • File Get-Comment.ps1 — part of check-in [c3cc7ad912] at 2018-06-10 13:36:39 on branch trunk — Show all the comments from a script, and ONLY the comments. (user: Joel Bennett size: 1219)

# encoding: ascii
# api: powershell
# title: Get-Comment
# description: Show all the comments from a script, and ONLY the comments.
# version: 3.0
# type: script
# author: Joel Bennett
# license: CC0
# x-poshcode-id: 4109
# x-archived: 2013-05-09T10:01:17
# x-published: 2013-04-16T05:34:00
#
# Inspired by a Scripting Guys article which does not work (especially on this script, but even on itself).
#
#Requires -version 3.0

<#
  .Synopsis
    Gets all of the comments from a script
  .Description
    Uses the PowerShell 3 Parser to figure out what's a comment
#>
[CmdletBinding()]
param( # The script, or the path to a script file
  [String]$Script 
)

# Convert paths to script contents (otherwise, assume they passed the contents)
if(Test-Path $Script) { 
   $Script = Get-Content $Script -Raw  # Raw saves having to stitch the lines back together.
}
# You have to initialize these to something for the ParseInput call
$ParseError = $null
$Tokens = $null
$null = [System.Management.Automation.Language.Parser]::ParseInput($Script, [ref]$Tokens, [ref]$ParseError)
# All tokens have a "Kind" and "Text" but not all tokens are comments ;)
$Tokens | ? Kind -eq "Comment" | % Text