@@ -57,6 +57,41 @@ $NoBlock,
5757[switch ]
5858$NoParameter ,
5959
60+
61+ [ValidateScript ({
62+ $validTypeList = [System.String ], [System.String []], [System.Text.RegularExpressions.Regex ],
63+ [Management.Automation.CommandInfo ], [Management.Automation.CommandInfo []]
64+ $thisType = $_.GetType ()
65+ $IsTypeOk =
66+ $ (@ ( foreach ($validType in $validTypeList ) {
67+ if ($_ -as $validType ) {
68+ $true ;break
69+ }
70+ }))
71+ if (-not $isTypeOk ) {
72+ throw " Unexpected type '$ ( @ ($thisType )[0 ]) '. Must be 'string','string[]','regex','commandinfo','commandinfo[]'."
73+ }
74+ return $true
75+ })]
76+ $IncludeCommand ,
77+
78+ [ValidateScript ({
79+ $validTypeList = [System.String ], [System.String []], [System.Text.RegularExpressions.Regex ],
80+ [Management.Automation.CommandInfo ], [Management.Automation.CommandInfo []]
81+ $thisType = $_.GetType ()
82+ $IsTypeOk =
83+ $ (@ ( foreach ($validType in $validTypeList ) {
84+ if ($_ -as $validType ) {
85+ $true ;break
86+ }
87+ }))
88+ if (-not $isTypeOk ) {
89+ throw " Unexpected type '$ ( @ ($thisType )[0 ]) '. Must be 'string','string[]','regex','commandinfo','commandinfo[]'."
90+ }
91+ return $true
92+ })]
93+ $ExcludeCommand ,
94+
6095# If set, will ensure that the script block contains types in this list.
6196# Passing -IncludeType without -ExcludeType will make -ExcludeType default to *.
6297[ValidateScript ({
@@ -162,6 +197,81 @@ $validateScripts = @(
162197'@
163198 }
164199
200+ # If -IncludeCommand or -ExcludeCommand were provided
201+ # generate an -ASTCondition to check command inclusion.
202+ if ($IncludeCommand -or $ExcludeCommand ) {
203+ if (-not $ExcludeCommand ) {
204+ $ExcludeCommand = ' *'
205+ }
206+
207+ if (-not $IncludeCommand -and $ExcludeCommand -eq ' *' ) {
208+ $AstCondition += {
209+ param ($ast )
210+ if ($ast -is [Management.Automation.Language.CommandAst ]) {
211+ throw " AST cannot contain commands"
212+ }
213+ return $true }
214+ }
215+ else {
216+ $AstCondition += [ScriptBlock ]::Create(@"
217+ param(`$ ast)
218+ `$ included = $ (
219+ if (-not $IncludeCommand ) { ' $null' }
220+ @ ($ (foreach ($inc in $IncludeCommand ) {
221+ if ($inc -is [string ]) {
222+ " '$ ( $inc -replace " '" , " ''" ) '"
223+ }
224+ elseif ($inc -is [Management.Automation.CommandInfo ]) {
225+ " '$ ( $inc.Name -replace " '" , " ''" ) '"
226+ }
227+ elseif ($inc -is [regex ]) {
228+ " [Regex]::new('$ ( $inc.ToString ().Replace(" '" , " ''" )) ','$ ( $inc.Options ) ','$ ( $inc.MatchTimeout ) ')"
229+ }
230+ })) -join ' ,' )
231+ `$ excluded = $ ( @ (
232+ if (-not $ExcludeCommand ) { ' $null' }
233+ $ (foreach ($exc in $ExcludeCommand ) {
234+ if ($exc -is [string ]) {
235+ " '$ ( $exc -replace " '" , " ''" ) '"
236+ }
237+ elseif ($exc -is [Management.Automation.CommandInfo ]) {
238+ " '$ ( $exc.Name -replace " '" , " ''" ) '"
239+ }
240+ elseif ($exc -is [regex ]) {
241+ " [Regex]::new('$ ( $exc.ToString ().Replace(" '" , " ''" )) ','$ ( $exc.Options ) ','$ ( $exc.MatchTimeout ) ')"
242+ }
243+ })) -join ' ,' )
244+ if (`$ ast -is [Management.Automation.Language.CommandAst]) {
245+ `$ astCommandName = `$ ast.CommandElements[0].Value
246+ $ ( if ($IncludeCommand ) {
247+ {
248+ foreach ($inc in $included ) {
249+ if ($inc -is [string ] -and $astCommandName -like $inc ) {
250+ return $true
251+ }
252+ elseif ($inc -is [Regex ] -and $astCommandName -match $inc ) {
253+ return $true
254+ }
255+ }
256+ }})
257+ $ ( {
258+ $throwMessage = " $astCommandName is not allowed"
259+ foreach ($exc in $excluded ) {
260+ if ($exc -is [string ] -and $astCommandName -like $exc ) {
261+ throw $throwMessage
262+ }
263+ elseif ($exc -is [regex ] -and $astCommandName -match $exc ) {
264+ throw $throwMessage
265+ }
266+ }
267+ })
268+
269+ }
270+ return `$ true
271+ "@ )
272+ }
273+ }
274+
165275 # If -IncludeType or -ExcludeType were provided
166276 if ($IncludeType -or $ExcludeType ) {
167277 if (-not $ExcludeType ) {
@@ -262,6 +372,10 @@ return `$true
262372"@ )
263373 }
264374 }
375+
376+ if ($IncludeCommand -or $ExcludeCommand ) {
377+
378+ }
265379
266380 if ($AstCondition ) {
267381@"
0 commit comments