Created
July 24, 2019 17:45
-
-
Save wgross/30f9ab1cc824b1db0191d933b8cfde52 to your computer and use it in GitHub Desktop.
POC: replicate parameters of a called script as parameters of a calling script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Management.Automation | |
using namespace System.Collections.ObjectModel | |
function inner { | |
param( | |
[int]$Value | |
) | |
$Value | |
} | |
filter convert_to_runtime_parameter { | |
$parameterData = [ParameterMetadata]$_ | |
#if($null -ne $parameterData -and(![string.IsNull])) { | |
if(![string]::IsNullOrEmpty($parameterData.Name)) { | |
$attributes = [Collection[System.Attribute]]::new() | |
$attributes.Add([ParameterAttribute]@{ | |
Mandatory = $parameterData.Mandatory | |
Position = $parameterData.Position +1 | |
}) | |
[RuntimeDefinedParameter]::new($parameterData.Name, $parameterData.ParameterType, $attributes) | |
} | |
} | |
function outer { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Position=0, Mandatory)] | |
[string]$Target | |
) | |
dynamicparam | |
{ | |
# Set up the Run-Time Parameter Dictionary | |
$dynamicParameters = [RuntimeDefinedParameterDictionary]::new() | |
# Begin dynamic parameter definition | |
$targetParameters = (Get-Item -Path Function:\$Target).Parameters.Values | |
$targetParameters | convert_to_runtime_parameter | ForEach-Object { | |
$dynamicParameters.Add($_.Name, $_) | |
} | |
return $dynamicParameters | |
} | |
process { | |
$cmd = $Target | |
$PSBoundParameters.Remove("Target")|Out-Null | |
#Write-Host $PSBoundParameters | |
#$PSBoundParameters|Out-String | |
& "$cmd" @PSBoundParameters | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment