Получить все параметры из определенных наборов параметров

У меня возникли проблемы с получением параметров из определенного набора параметров. Я решил это, получив все параметры и используя $parDetails.Name.Contains("FileAttachment") в качестве оператора if.

Вместо этого я хотел бы получить параметры из определенного набора параметров.

Может кто-нибудь, пожалуйста, помогите мне с этим? Ниже приведен код, который я сейчас использую.

$CommandName = $PSCmdlet.MyInvocation.InvocationName
$ParameterList = (Get-Command -Name $CommandName).Parameter

foreach ($key in $ParameterList.keys) {
    Write-Verbose "Starting loop for $key"
    $parDetails = Get-Variable -Name $key
}

person Anders Thyrsson    schedule 19.06.2018    source источник
comment
Почему бы просто не использовать Get-Help для просмотра параметров? Какую проблему вы решаете?   -  person Bill_Stewart    schedule 19.06.2018


Ответы (2)


Используя синтаксис PSv4+:

# Sample cmdlet and parameter set to inspect.
# To determine all parameter-set names for a given cmdlet, use:
#  (Get-Command $commandName).ParameterSets.Name
$cmd = 'Get-Item'
$paramSet = 'Path'

# Get all parameters associated with the specified parameter set.
$paramsInSet = (Get-Command $cmd).ParameterSets.Where({$_.Name -eq $paramSet}).Parameters

# Output the names of all parameters in the set.
$paramsInSet.Name

Вышеизложенное дает:

Path
Filter
Include
Exclude
Force
Credential
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
person mklement0    schedule 19.06.2018
comment
Не видел наборов параметров в команде... это лучше, чем то, что было у меня. - person Mike Shepard; 20.06.2018

Вот скрипт, который ищет параметры в определенном наборе параметров (или во всех наборах параметров). Это должно дать вам то, что вы ищете.

$commandName='Get-ChildItem'
$ParameterSetToMatch='LiteralItems'

$ParameterList = (Get-Command -Name $commandName).Parameters.Values

foreach($parameter in $parameterList){
    $parameterSets=$parameter.ParameterSets.Keys
    if($parameterSets -contains '__AllParameterSets'){
        write-host "$($parameter.Name) is in __AllParameterSets"
    } elseif ($parameterSets -contains $parameterSetToMatch ){
        write-host "$($parameter.Name) is in $parameterSetToMatch"
    }
}

Если вам просто нужны элементы конкретно в наборе параметров, вот более короткая версия:

$commandName='Get-ChildItem'
$ParameterSetToMatch='Items'
$parameterlist |  
    Where-object {$_.ParameterSets.Keys -contains $ParameterSetToMatch} | 
     select-object Name
person Mike Shepard    schedule 19.06.2018