diff --git a/scripts/lib/powershell-destructive-command.js b/scripts/lib/powershell-destructive-command.js index 8fad906a5..c5b90d048 100644 --- a/scripts/lib/powershell-destructive-command.js +++ b/scripts/lib/powershell-destructive-command.js @@ -428,6 +428,7 @@ function invokesContainerResult(prefix, options = {}) { const pipelineStart = clause.lastIndexOf('|'); const pipelineCommand = clause.slice(pipelineStart + 1).trim(); const isForeachLoopHeader = Boolean(options.groupingExpression) && + pipelineStart === -1 && /^foreach$/i.test(pipelineCommand); return /(?:^|\s)(?:&|\.)\s*$/.test(clause) || /\.\s*(?:foreach|where)\s*$/i.test(clause) || diff --git a/tests/lib/powershell-destructive-command.test.js b/tests/lib/powershell-destructive-command.test.js index cd023a3c1..e4f7210ba 100644 --- a/tests/lib/powershell-destructive-command.test.js +++ b/tests/lib/powershell-destructive-command.test.js @@ -488,6 +488,9 @@ test('distinguishes foreach statements from the pipeline alias', () => { expectRules('1 | foreach { Remove-Item -Force C:/tmp/demo }', [ RULES.REMOVE_FORCE, ]); + expectRules('1 | foreach ({ Remove-Item -Force C:/tmp/demo })', [ + RULES.REMOVE_FORCE, + ]); }); test('classifies invoked static script-block variables but leaves assignments inert', () => {