|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Returns the commands used by the specified script. |
| 4 | +
|
| 5 | +.FUNCTIONALITY |
| 6 | +Scripts |
| 7 | +
|
| 8 | +.INPUTS |
| 9 | +System.String containing the path to a script file to parse. |
| 10 | +
|
| 11 | +.OUTPUTS |
| 12 | +System.Management.Automation.CommandInfo for each command parsed from the file. |
| 13 | +
|
| 14 | +.LINK |
| 15 | +https://learn.microsoft.com/dotnet/api/system.management.automation.language.parser.parsefile |
| 16 | +
|
| 17 | +.LINK |
| 18 | +Get-Command |
| 19 | +
|
| 20 | +.EXAMPLE |
| 21 | +Select-ScriptCommands.ps1 Select-ScriptCommands.ps1 |
| 22 | +
|
| 23 | +CommandType Name Version Source |
| 24 | +----------- ---- ------- ------ |
| 25 | +Cmdlet Out-Null 7.5.0.500 Microsoft.PowerShell.Core |
| 26 | +Cmdlet Where-Object 7.5.0.500 Microsoft.PowerShell.Core |
| 27 | +Cmdlet Select-Object 7.0.0.0 Microsoft.PowerShell.Utility |
| 28 | +Cmdlet Get-Command 7.5.0.500 Microsoft.PowerShell.Core |
| 29 | +Cmdlet Resolve-Path 7.0.0.0 Microsoft.PowerShell.Management |
| 30 | +Filter Get-ScriptCommands |
| 31 | +#> |
| 32 | + |
| 33 | +#Requires -Version 7 |
| 34 | +[CmdletBinding()][OutputType([System.Management.Automation.CommandInfo])] Param( |
| 35 | +# A script file path (wildcards are accepted). |
| 36 | +[Parameter(Position=0,ValueFromPipeline=$true)][string] $Path |
| 37 | +) |
| 38 | +Begin |
| 39 | +{ |
| 40 | + $Script:parseErrors = [Management.Automation.Language.ParseError[]]@() |
| 41 | + $Script:tokens = [Management.Automation.Language.Token[]]@() |
| 42 | + filter Get-ScriptCommands |
| 43 | + { |
| 44 | + [CmdletBinding()] Param( |
| 45 | + [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][string] $Path |
| 46 | + ) |
| 47 | + [Management.Automation.Language.Parser]::ParseFile($Path, |
| 48 | + [ref]$Script:tokens, [ref]$Script:parseErrors) |Out-Null |
| 49 | + $Script:tokens | |
| 50 | + Where-Object TokenFlags -eq 'CommandName' | |
| 51 | + Select-Object -Unique -ExpandProperty Value | |
| 52 | + Get-Command |
| 53 | + } |
| 54 | +} |
| 55 | +Process |
| 56 | +{ |
| 57 | + Resolve-Path $Path |Get-ScriptCommands |
| 58 | +} |
0 commit comments