File tree Expand file tree Collapse file tree 1 file changed +26
-2
lines changed
Expand file tree Collapse file tree 1 file changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -128,6 +128,16 @@ return $true
128128})]
129129$ExcludeType ,
130130
131+ # If set, will ensure that the ScriptBlock does not contain any loops.
132+ [Alias (' NoLoops' )]
133+ [switch ]
134+ $NoLoop ,
135+
136+ # If set, will ensure that the ScriptBlock does not contain any do or while loops.
137+ [Alias (' NoWhileLoops' , ' NoDoLoops' , ' NoDoLoop' )]
138+ [switch ]
139+ $NoWhileLoop ,
140+
131141# One or more AST conditions to validate.
132142# If no results are found or the condition throws, the script block will be considered invalid.
133143[Alias (' AstConditions' , ' IfAst' )]
@@ -373,8 +383,22 @@ return `$true
373383 }
374384 }
375385
376- if ($IncludeCommand -or $ExcludeCommand ) {
377-
386+ if ($NoLoop ) {
387+ $AstCondition += {
388+ param ($ast )
389+ if ($ast -is [Management.Automation.Language.LoopStatementAst ]) {
390+ throw " ScriptBlock cannot contain loops"
391+ }
392+ return $true
393+ }
394+ } elseif ($NoWhileLoop ) {
395+ $AstCondition += {
396+ param ($ast )
397+ if ($ast -is [Management.Automation.Language.LoopStatementAst ] -and
398+ $ast.GetType ().Name -match ' (?>do|while)' ) {
399+ throw " ScriptBlock cannot contain $ ( $ast.GetType ().Name) "
400+ }
401+ }
378402 }
379403
380404 if ($AstCondition ) {
You can’t perform that action at this time.
0 commit comments