Created
January 17, 2014 16:49
-
-
Save mwrock/8476811 to your computer and use it in GitHub Desktop.
A stab at a Powershell Plugin Pattern. In working out how different VM providers can plugin to Boxstarter, I have come up with a pattern for dynamicaly resolving a plugin function. I want to expose a Enable-BoxstarterVM and based on its -Provider argument call the same named functioned available in any same named function loaded. This pattern cr…
This file contains 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
function Resolve-VMPlugin { | |
[CmdletBinding()] | |
[OutputType([BoxstarterConnectionConfig])] | |
param( | |
$Provider | |
) | |
DynamicParam { | |
if(!$provider){$provider="HyperV"} | |
$module=Get-Module "Boxstarter.$provider" | |
$command = Get-Command $module\Enable-BoxstarterVM | |
$metadata=New-Object System.Management.Automation.CommandMetaData($command) | |
$paramDictionary = new-object ` | |
-Type System.Management.Automation.RuntimeDefinedParameterDictionary | |
$metadata.Parameters.Keys | % { | |
$param=$metadata.Parameters[$_] | |
$dynParam = new-object ` | |
-Type System.Management.Automation.RuntimeDefinedParameter($param.Name, $param.ParameterType, $param.Attributes[1]) | |
$paramDictionary.Add($param.Name, $dynParam) | |
} | |
return $paramDictionary | |
} | |
Begin{ | |
$module=Get-Module "Boxstarter.$provider" | |
$command = Get-Command $module\Enable-BoxstarterVM | |
$PSBoundParameters.Remove("Provider") | Out-Null | |
&($command) @PSBoundParameters | |
} | |
} | |
new-alias Enable-BoxstarterVM Resolve-VMPlugin -force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment