-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathSelect-ScriptCommands.ps1
More file actions
62 lines (54 loc) · 2.14 KB
/
Select-ScriptCommands.ps1
File metadata and controls
62 lines (54 loc) · 2.14 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
<#
.SYNOPSIS
Returns the commands used by the specified script.
.FUNCTIONALITY
Scripts
.INPUTS
System.String containing the path to a script file to parse.
.OUTPUTS
System.Management.Automation.CommandInfo for each command parsed from the file.
.LINK
https://learn.microsoft.com/dotnet/api/system.management.automation.language.parser.parsefile
.LINK
Get-Command
.EXAMPLE
Select-ScriptCommands.ps1 Select-ScriptCommands.ps1
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Out-Null 7.5.0.500 Microsoft.PowerShell.Core
Cmdlet Where-Object 7.5.0.500 Microsoft.PowerShell.Core
Cmdlet Select-Object 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Get-Command 7.5.0.500 Microsoft.PowerShell.Core
Cmdlet Resolve-Path 7.0.0.0 Microsoft.PowerShell.Management
Filter Get-ScriptCommands
#>
#Requires -Version 7
[CmdletBinding()][OutputType([System.Management.Automation.CommandInfo])] Param(
# A script file path (wildcards are accepted).
[Parameter(Position=0,ValueFromPipeline=$true)][string] $Path,
# Specifies the types of commands that this cmdlet gets.
[Alias('Type')][Management.Automation.CommandTypes] $CommandType
)
Begin
{
$Script:parseErrors = [Management.Automation.Language.ParseError[]]@()
$Script:tokens = [Management.Automation.Language.Token[]]@()
filter Get-ScriptCommands
{
[CmdletBinding()] Param(
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string] $Path
)
[Management.Automation.Language.Parser]::ParseFile($Path,
[ref]$Script:tokens, [ref]$Script:parseErrors) |Out-Null
$commands = $Script:tokens |
Where-Object TokenFlags -eq 'CommandName' -pv token |
Select-Object -Unique -ExpandProperty Value -ErrorAction Ignore |
Get-Command -ErrorAction Ignore |
ForEach-Object {$_ |Add-Member -NotePropertyName ScriptName -NotePropertyValue $token.Extent.File -Force -PassThru}
return !$CommandType ? $commands : ($commands |Where-Object CommandType -eq $CommandType)
}
}
Process
{
Resolve-Path $Path |Get-ScriptCommands
}