1717 [Include('*-*.ps1')]$psScriptRoot
1818 } | .>PipeScript
1919#>
20+ [ValidateScript ({
21+ if ($_ -is [Management.Automation.Language.CommandAst ]) {
22+ return $_.CommandsElements [0 ].Value -in ' include' , ' includes'
23+ }
24+ })]
25+ [Alias (' Includes' )]
2026param (
2127# The File Path to Include
2228[Parameter (Mandatory , Position = 0 )]
@@ -31,11 +37,45 @@ $AsByte,
3137[switch ]
3238$Passthru ,
3339
40+ # The exclusion pattern to use.
41+ [Alias (' ExcludePattern' )]
42+ [string []]
43+ $Exclude = ' \.[^\.]+\.ps1$' ,
44+
3445[Parameter (Mandatory , ParameterSetName = ' VariableAST' , ValueFromPipeline )]
3546[Management.Automation.Language.VariableExpressionast ]
36- $VariableAst
47+ $VariableAst ,
48+
49+
50+ [Parameter (Mandatory , ParameterSetName = ' CommandAst' , ValueFromPipeline )]
51+ [Management.Automation.Language.CommandAst ]
52+ $CommandAst
3753)
3854
55+ process {
56+
57+ if ($psCmdlet.ParameterSetName -eq ' CommandAst' ) {
58+ # Gather some information about our calling context
59+ $myParams = [Ordered ]@ {} + $PSBoundParameters
60+ # and attempt to parse it as a sentance (only allowing it to match this particular command)
61+ $mySentence = $commandAst.AsSentence ($MyInvocation.MyCommand )
62+ $myCmd = $MyInvocation.MyCommand
63+
64+ # Walk thru all mapped parameters in the sentence
65+ foreach ($paramName in $mySentence.Parameters.Keys ) {
66+ if (-not $myParams [$paramName ]) { # If the parameter was not directly supplied
67+ $myParams [$paramName ] = $mySentence.Parameters [$paramName ] # grab it from the sentence.
68+ foreach ($myParam in $myCmd.Parameters.Values ) {
69+ if ($myParam.Aliases -contains $paramName ) { # set any variables that share the name of an alias
70+ $ExecutionContext.SessionState.PSVariable.Set ($myParam.Name , $mySentence.Parameters [$paramName ])
71+ }
72+ }
73+ # and set this variable for this value.
74+ $ExecutionContext.SessionState.PSVariable.Set ($paramName , $mySentence.Parameters [$paramName ])
75+ }
76+ }
77+ }
78+
3979# Determine the command we would be including (relative to the current path)
4080$includingCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand ($FilePath , ' All' )
4181if (-not $includingCommand ) { # if we could not determine the command, we may need to error out.
@@ -140,12 +180,19 @@ if ($psCmdlet.ParameterSetName -eq 'ScriptBlock' -or
140180 [ScriptBlock ]::Create(" $ ( $VariableAst ) = $IncludedScript " )
141181} elseif ($VariableAst.VariablePath -notmatch ' ^null$' ) {
142182 [ScriptBlock ]::Create(@"
143- foreach (`$ file in (Get-ChildItem -Path "$ ( $VariableAst ) " -Filter "$FilePath " -Recurse)) {
183+ :ToIncludeFiles foreach (`$ file in (Get-ChildItem -Path "$ ( $VariableAst ) " -Filter "$FilePath " -Recurse)) {
144184 if (`$ file.Extension -ne '.ps1') { continue } # Skip if the extension is not .ps1
145- if (`$ file.Name -match '\.[^\.]+\.ps1$') { continue } # Skip if the file is an unrelated file.
185+ foreach (`$ exclusion in '$ ( $Exclude -replace " '" , " ''" -join " ','" ) ') {
186+ if (-not `$ exclusion) { continue }
187+ if (`$ file.Name -match `$ exclusion) {
188+ continue ToIncludeFiles # Skip excluded files
189+ }
190+ }
146191 . `$ file.FullName$ (
147192 if ($Passthru ) { [Environment ]::NewLine + (' ' * 4 ) + ' $file' }
148193 )
149194}
150195"@ )
151196}
197+
198+ }
0 commit comments