@@ -76,8 +76,30 @@ process {
7676 $DotChainPart = ' '
7777 $DotChainPattern = ' ^\.\p{L}'
7878
79+ $targetCommandAst = $CommandAst
80+ $commandSequence = @ ()
81+ $lastCommandAst = $null
7982 # Then, walk over each element of the commands
80- $CommandElements = $CommandAst.CommandElements
83+ $CommandElements = @ (do {
84+ $lastCommandAst = $targetCommandAst
85+ $commandSequence += $targetCommandAst
86+ $targetCommandAst.CommandElements
87+ $nextStatementIndex = $commandAst.Parent.Parent.Statements.IndexOf ($targetCommandAst.Parent ) + 1
88+ $nextStatement = $CommandAst.Parent.Parent.Statements [$nextStatementIndex ]
89+ if ($nextStatement -isnot [Management.Automation.Language.PipelineAst ]) {
90+ break
91+ }
92+ if ($nextStatement.PipelineElements [0 ] -isnot
93+ [Management.Automation.Language.CommandAst ]) {
94+ break
95+ }
96+ if ($nextStatement.PipelineElements [0 ].CommandElements[0 ].Value -notmatch
97+ $DotChainPattern ) {
98+ break
99+ }
100+ $targetCommandAst = $CommandAst.Parent.Parent.Statements [$nextStatementIndex ].PipelineElements[0 ]
101+ } while ($targetCommandAst ))
102+
81103 for ( $elementIndex = 0 ; $elementIndex -lt $CommandElements.Count ; $elementIndex ++ ) {
82104 # If we are on the first element, or the command element starts with the DotChainPattern
83105 if ($elementIndex -eq 0 -or $CommandElements [$elementIndex ].Value -match $DotChainPattern ) {
@@ -124,11 +146,7 @@ process {
124146
125147 $NewScript = @ ()
126148 $indent = 0
127- $WasPipedTo =
128- $CommandAst.Parent -and
129- $CommandAst.Parent.PipelineElements -and
130- $CommandAst.Parent.PipelineElements.IndexOf ($CommandAst ) -gt 0
131-
149+ $WasPipedTo = $CommandAst.IsPipedTo
132150
133151 # By default, we are not creating a property bag.
134152 # This default will change if:
@@ -137,7 +155,7 @@ process {
137155 $isPropertyBag = $false
138156
139157 # If we were piped to, adjust indent (for now)
140- if ($WasPipedTo ) {
158+ if ($WasPipedTo ) {
141159 $indent += 4
142160 }
143161
@@ -286,5 +304,10 @@ $(' ' * ($indent + 4))$thisSegement
286304 $NewScript = $NewScript -join [Environment ]::Newline
287305
288306 # Return the created script.
289- [scriptblock ]::Create($NewScript )
307+ $newScriptBlock = [scriptblock ]::Create($NewScript )
308+
309+ if ($targetCommandAst -ne $CommandAst ) {
310+ $newScriptBlock | Add-Member NoteProperty SkipUntil $commandSequence
311+ }
312+ $newScriptBlock
290313}
0 commit comments