@@ -16,6 +16,19 @@ function New-PipeScript {
1616 # * As an ```[Object[]]```.
1717 # * As a ```[ScriptBlock]```
1818 [Parameter (ValueFromPipelineByPropertyName )]
19+ [ValidateScript ({
20+ if ($_ -isnot [ScriptBlock ]) { return $true }
21+ $statementCount = 0
22+ $statementCount += $_.Ast.DynamicParamBlock.Statements.Count
23+ $statementCount += $_.Ast.BeginBlock.Statements.Count
24+ $statementCount += $_.Ast.ProcessBlock.Statements.Count
25+ $statementCount += $_.Ast.EndBlock.Statements.Count
26+ if ($statementCount ) {
27+ throw " ScriptBlock should have no statements"
28+ } else {
29+ return $true
30+ }
31+ })]
1932 [ValidateScript ({
2033 $validTypeList = [System.Collections.IDictionary ], [System.String ], [System.Object []], [System.Management.Automation.ScriptBlock ]
2134 $thisType = $_.GetType ()
@@ -30,18 +43,19 @@ function New-PipeScript {
3043 }
3144 return $true
3245 })]
33-
34- $Parameter ,
3546
47+ $Parameter ,
3648 # The dynamic parameter block.
3749 [Parameter (ValueFromPipelineByPropertyName )]
3850 [ValidateScript ({
51+ if ($_ -isnot [ScriptBlock ]) { return $true }
3952 if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock ) {
4053 throw " ScriptBlock should not have any named blocks"
4154 }
4255 return $true
4356 })]
4457 [ValidateScript ({
58+ if ($_ -isnot [ScriptBlock ]) { return $true }
4559 if ($_.Ast.ParamBlock.Parameters.Count ) {
4660 throw " ScriptBlock should not have parameters"
4761 }
@@ -53,12 +67,14 @@ function New-PipeScript {
5367 # The begin block.
5468 [Parameter (ValueFromPipelineByPropertyName )]
5569 [ValidateScript ({
70+ if ($_ -isnot [ScriptBlock ]) { return $true }
5671 if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock ) {
5772 throw " ScriptBlock should not have any named blocks"
5873 }
5974 return $true
6075 })]
6176 [ValidateScript ({
77+ if ($_ -isnot [ScriptBlock ]) { return $true }
6278 if ($_.Ast.ParamBlock.Parameters.Count ) {
6379 throw " ScriptBlock should not have parameters"
6480 }
@@ -70,12 +86,14 @@ function New-PipeScript {
7086 # The process block.
7187 [Parameter (ValueFromPipelineByPropertyName )]
7288 [ValidateScript ({
89+ if ($_ -isnot [ScriptBlock ]) { return $true }
7390 if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock ) {
7491 throw " ScriptBlock should not have any named blocks"
7592 }
7693 return $true
7794 })]
7895 [ValidateScript ({
96+ if ($_ -isnot [ScriptBlock ]) { return $true }
7997 if ($_.Ast.ParamBlock.Parameters.Count ) {
8098 throw " ScriptBlock should not have parameters"
8199 }
@@ -87,12 +105,14 @@ function New-PipeScript {
87105 # The end block.
88106 [Parameter (ValueFromPipelineByPropertyName )]
89107 [ValidateScript ({
108+ if ($_ -isnot [ScriptBlock ]) { return $true }
90109 if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock ) {
91110 throw " ScriptBlock should not have any named blocks"
92111 }
93112 return $true
94113 })]
95114 [ValidateScript ({
115+ if ($_ -isnot [ScriptBlock ]) { return $true }
96116 if ($_.Ast.ParamBlock.Parameters.Count ) {
97117 throw " ScriptBlock should not have parameters"
98118 }
@@ -101,19 +121,19 @@ function New-PipeScript {
101121 [Alias (' EndBlock' )]
102122 [ScriptBlock ]
103123 $End ,
104-
105124 # The script header.
106125 [Parameter (ValueFromPipelineByPropertyName )]
107126 [string ]
108127 $Header
109128 )
110129 begin {
111- $ParametersToCreate = [Ordered ]@ {}
112- $allDynamicParameters = @ ()
113- $allBeginBlocks = @ ()
114- $allEndBlocks = @ ()
115- $allProcessBlocks = @ ()
116- $allHeaders = @ ()
130+ $ParametersToCreate = [Ordered ]@ {}
131+ $parameterScriptBlocks = @ ()
132+ $allDynamicParameters = @ ()
133+ $allBeginBlocks = @ ()
134+ $allEndBlocks = @ ()
135+ $allProcessBlocks = @ ()
136+ $allHeaders = @ ()
117137 }
118138 process {
119139 if ($parameter ) {
@@ -123,7 +143,7 @@ function New-PipeScript {
123143 # If it is, walk thur each parameter in the dictionary
124144 foreach ($EachParameter in $Parameter.GetEnumerator ()) {
125145 # Continue past any parameters we already have
126- if ($ParametersToCreate.Contains ($EachParameter.Key )) {
146+ if ($ParametersToCreate.Contains ($EachParameter.Key )) {
127147 continue
128148 }
129149 # If the parameter is a string and the value is not a variable
@@ -148,13 +168,15 @@ function New-PipeScript {
148168 # If there was a param block on the script block
149169 if ($EachParameter.Value.Ast.ParamBlock ) {
150170 # embed the parameter block (except for the param keyword)
151- $EachParameter.Value.Ast.ParamBlock.Extent.ToString () -replace
152- ' ^[\s\r\n]param(' -replace ' )[\s\r\n]$'
171+ $EachParameter.Value.Ast.ParamBlock.Extent.ToString () -replace
172+ ' ^[\s\r\n]{0,} param\ (' -replace ' \ )[\s\r\n]{0,} $'
153173 } else {
154174 # Otherwise
155- $EachParameter.Value.ToString () -replace
175+ ' [Parameter(ValueFromPipelineByPropertyName)]' + (
176+ $EachParameter.Value.ToString () -replace
156177 " \`$ $ ( $eachParameter.Key ) [\s\r\n]$" -replace # Replace any trailing variables
157- ' param()[\s\r\n]{0,}$' # then replace any empty param blocks.
178+ ' param\(\)[\s\r\n]{0,}$' # then replace any empty param blocks.
179+ )
158180 }
159181 }
160182 elseif ($EachParameter.Value -is [Object []]) {
@@ -167,6 +189,13 @@ function New-PipeScript {
167189 " [Parameter(ValueFromPipelineByPropertyName)]"
168190 " `$ $Parameter "
169191 )
192+ } elseif ($parameter -is [scriptblock ]) {
193+ $parameterScriptBlocks +=
194+ if ($parameter.Ast.ParamBlock ) {
195+ # embed the parameter block (except for the param keyword)
196+ $parameter
197+ } else {
198+ }
170199 } elseif ($Parameter -is [Object []]) {
171200 $currentParam = @ ()
172201 $currentParamName = ' '
@@ -211,16 +240,19 @@ function New-PipeScript {
211240 if ($end ) {
212241 $allEndBlocks += $end
213242 }
214-
215243 }
216244 end {
217- $newParamBlock =
218- " param(" + [Environment ]::newLine +
245+ $newParamBlock =
246+ " param(" + [Environment ]::newLine +
219247 $ (@ (foreach ($toCreate in $ParametersToCreate.GetEnumerator ()) {
220248 $toCreate.Value -join [Environment ]::NewLine
221249 }) -join (' ,' + [Environment ]::NewLine)) +
222250 [Environment ]::NewLine +
223251 ' )'
252+ if ($parameterScriptBlocks ) {
253+ $parameterScriptBlocks += [ScriptBlock ]::new($newParamBlock )
254+ $newParamBlock = $parameterScriptBlocks | Join-PipeScript
255+ }
224256 $createdScriptBlock = [scriptblock ]::Create("
225257$ ( $allHeaders -join [Environment ]::Newline)
226258$newParamBlock
0 commit comments