Skip to content

Instantly share code, notes, and snippets.

@indented-automation
Last active July 4, 2017 09:43
Show Gist options
  • Save indented-automation/85c0d960e689a36cae42ca220d2daa04 to your computer and use it in GitHub Desktop.
Save indented-automation/85c0d960e689a36cae42ca220d2daa04 to your computer and use it in GitHub Desktop.
Resolve-ParameterSet
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