Last active
July 4, 2017 09:43
-
-
Save indented-automation/85c0d960e689a36cae42ca220d2daa04 to your computer and use it in GitHub Desktop.
Resolve-ParameterSet
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
function Resolve-ParameterSet { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[String]$Command, | |
[String[]]$ParameterName | |
) | |
try { | |
$commandInfo = Get-Command $Command | |
$candidateSets = for ($i = 0; $i -lt $commandInfo.ParameterSets.Count; $i++) { | |
$parameterSet = $commandInfo.ParameterSets[$i] | |
$isCandidateSet = $true | |
foreach ($parameter in $parameterSet.Parameters) { | |
if ($parameter.IsMandatory -and -not ($ParameterName -contains $parameter.Name)) { | |
$isCandidateSet = $false | |
break | |
} | |
} | |
foreach ($name in $ParameterName) { | |
if ($name -notin $parameterSet.Parameters.Name) { | |
$isCandidateSet = $false | |
break | |
} | |
} | |
if ($isCandidateSet) { | |
[PSCustomObject]@{ | |
Name = $parameterSet.Name | |
Index = $i | |
} | |
} | |
} | |
if (@($candidateSets).Count -eq 1) { | |
return $candidateSets.Name | |
} elseif (@($candidateSets).Count -gt 0) { | |
foreach ($parameterSet in $candidateSets) { | |
if ($commandInfo.ParameterSets[$parameterSet.Index].IsDefault) { | |
return $parameterSet.Name | |
} | |
} | |
} else { | |
throw 'Failed to find parameter set' | |
} | |
} catch { | |
throw | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment