Last active
July 21, 2016 17:18
-
-
Save beatcracker/a7136d41bba5acfbdf2241eb1e38f926 to your computer and use it in GitHub Desktop.
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
# Get-Inventory -Server -IPAddress 1.1.1.1 -Credential (Get-Credential) | |
# Get-Inventory -Storage -WebServiceEndpoint https://a.b.c -OAuthToken XXXXX | |
function Get-Inventory | |
{ | |
[CmdletBinding()] | |
Param() | |
DynamicParam | |
{ | |
$ValidateSet = 'Server', 'Storage', 'Bar', 'Baz' | |
$BaseDP = $ValidateSet | ForEach-Object { | |
@{ | |
Name = $_ | |
Type = [switch] | |
Parameter = @{ | |
Mandatory = $true | |
ParameterSetName = $_ | |
} | |
} | |
} | |
$ExtDP = @( | |
@{ | |
Name = 'IPAddress' | |
Type = [ipaddress] | |
Parameter = @{ | |
ParameterSetName = 'Server' | |
} | |
}, | |
@{ | |
Name = 'Credential' | |
Type = [pscredential] | |
Parameter = @{ | |
ParameterSetName = 'Server' | |
} | |
}, | |
@{ | |
Name = 'WebServiceEndpoint' | |
Type = [uri] | |
Parameter = @{ | |
ParameterSetName = 'Storage' | |
} | |
}, | |
@{ | |
Name = 'OAuthToken' | |
Type = [string] | |
Parameter = @{ | |
ParameterSetName = 'Storage' | |
} | |
} | |
) | |
$DynamicParameters = $BaseDP + $ExtDP | |
# Create the dictionary | |
$RuntimeParameterDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary | |
foreach($dp in $DynamicParameters) | |
{ | |
# Create the collection of attributes | |
$AttributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute] | |
# Create and set the parameters' attributes (Parameter, ValidateSet) | |
# [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] | |
$AttributeCollection.Add( | |
(New-Object -TypeName System.Management.Automation.ParameterAttribute -Property $dp.Parameter) | |
) | |
# [ValidateSet()] | |
if($dp.ValidateSet) | |
{ | |
$AttributeCollection.Add( | |
(New-Object -TypeName System.Management.Automation.ValidateSetAttribute -ArgumentList $dp.ValidateSet) | |
) | |
} | |
# Create and return the dynamic parameter | |
$RuntimeParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter( | |
$dp.Name, | |
$dp.Type, | |
$AttributeCollection | |
) | |
$RuntimeParameterDictionary.Add($dp.Name, $RuntimeParameter) | |
} | |
# Return dicitonary | |
$RuntimeParameterDictionary | |
} | |
Process | |
{ | |
$PSBoundParameters | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment