@@ -123,7 +123,16 @@ function New-PipeScript {
123123 # The script header.
124124 [Parameter (ValueFromPipelineByPropertyName )]
125125 [string ]
126- $Header
126+ $Header ,
127+ # If provided, will automatically create parameters.
128+ # Parameters will be automatically created for any unassigned variables.
129+ [Alias (' AutoParameterize' , ' AutoParameters' )]
130+ [switch ]
131+ $AutoParameter ,
132+ # The type used for automatically generated parameters.
133+ # By default, ```[PSObject]```.
134+ [type ]
135+ $AutoParameterType = [PSObject ]
127136 )
128137 begin {
129138 $ParametersToCreate = [Ordered ]@ {}
@@ -228,6 +237,24 @@ function New-PipeScript {
228237 # accumulate them.
229238 $allEndBlocks += $end
230239 }
240+ if ($AutoParameter ) {
241+ $variableDefinitions = $Begin , $Process , $End |
242+ Where-Object { $_ } |
243+ Search-PipeScript - AstType VariableExpressionAST |
244+ Select-Object - ExpandProperty Result
245+ foreach ($var in $variableDefinitions ) {
246+ $assigned = $var.GetAssignments ()
247+ if ($assigned ) { continue }
248+ $varName = $var.VariablePath.userPath.ToString ()
249+ $ParametersToCreate [$varName ] = @ (
250+ @ (
251+ " [Parameter(ValueFromPipelineByPropertyName)]"
252+ " [$ ( $AutoParameterType.FullName -replace ' ^System\.' ) ]"
253+ " $var "
254+ ) -join [Environment ]::NewLine
255+ )
256+ }
257+ }
231258 }
232259 end {
233260 # Take all of the accumulated parameters and create a parameter block
0 commit comments