Skip to content

Commit a027938

Browse files
StartAutomatingStartAutomating
authored andcommitted
New-PipeScript: Allowing -Parameter to be supplied via reflection (Fixes #171)
1 parent 6b05f7e commit a027938

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

New-PipeScript.ps1

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function New-PipeScript {
2929
}
3030
})]
3131
[ValidateScript({
32-
$validTypeList = [System.Collections.IDictionary],[System.String],[System.Object[]],[System.Management.Automation.ScriptBlock]
32+
$validTypeList = [System.Collections.IDictionary],[System.String],[System.Object[]],[System.Management.Automation.ScriptBlock],[System.Reflection.PropertyInfo],[System.Reflection.PropertyInfo[]],[System.Reflection.ParameterInfo],[System.Reflection.ParameterInfo[]],[System.Reflection.MethodInfo],[System.Reflection.MethodInfo[]]
3333
$thisType = $_.GetType()
3434
$IsTypeOk =
3535
$(@( foreach ($validType in $validTypeList) {
@@ -38,7 +38,7 @@ function New-PipeScript {
3838
}
3939
}))
4040
if (-not $isTypeOk) {
41-
throw "Unexpected type '$(@($thisType)[0])'. Must be 'System.Collections.IDictionary','string','System.Object[]','scriptblock'."
41+
throw "Unexpected type '$(@($thisType)[0])'. Must be 'System.Collections.IDictionary','string','System.Object[]','scriptblock','System.Reflection.PropertyInfo','System.Reflection.PropertyInfo[]','System.Reflection.ParameterInfo','System.Reflection.ParameterInfo[]','System.Reflection.MethodInfo','System.Reflection.MethodInfo[]'."
4242
}
4343
return $true
4444
})]
@@ -215,6 +215,42 @@ function New-PipeScript {
215215
$parameter
216216
}
217217
}
218+
elseif ($parameter -is [Reflection.PropertyInfo] -or
219+
$parameter -as [Reflection.PropertyInfo[]] -or
220+
$parameter -is [Reflection.ParameterInfo] -or
221+
$parameter -as [Reflection.ParameterInfo[]] -or
222+
$parameter -is [Reflection.MethodInfo] -or
223+
$parameter -as [Reflection.MethodInfo[]]
224+
) {
225+
if ($parameter -is [Reflection.MethodInfo] -or
226+
$parameter -as [Reflection.MethodInfo[]]) {
227+
$parameter = @(foreach ($methodInfo in $parameter) {
228+
$methodInfo.GetParameters()
229+
})
230+
}
231+
foreach ($prop in $Parameter) {
232+
if ($prop -is [Reflection.PropertyInfo] -and -not $prop.CanWrite) { continue }
233+
$paramType =
234+
if ($prop.ParameterType) {
235+
$prop.ParameterType
236+
} elseif ($prop.PropertyType) {
237+
$prop.PropertyType
238+
} else {
239+
[PSObject]
240+
}
241+
$ParametersToCreate[$prop.Name] =
242+
@(
243+
$parameterAttribute = "[Parameter(ValueFromPipelineByPropertyName)]"
244+
$parameterAttribute
245+
if ($paramType -eq [boolean]) {
246+
"[switch]"
247+
} else {
248+
"[$($paramType -replace '^System\.')]"
249+
}
250+
'$' + $prop.Name
251+
) -ne ''
252+
}
253+
}
218254
}
219255
# If there is header content,
220256
if ($header) {

0 commit comments

Comments
 (0)