首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从特定参数集中获取所有参数

从特定参数集中获取所有参数
EN

Stack Overflow用户
提问于 2018-06-20 04:13:26
回答 2查看 567关注 0票数 2

我在从特定的参数集中获取参数时遇到了一些问题。我已经通过获取所有参数并使用$parDetails.Name.Contains("FileAttachment")作为if语句解决了这个问题。

相反,我想要的是从特定的参数集中获取参数。

有没有人能帮我一下?下面是我目前使用的代码。

代码语言:javascript
复制
$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
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-20 05:40:26

使用PSv4+语法:

代码语言:javascript
复制
# 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

以上结果如下:

代码语言:javascript
复制
Path
Filter
Include
Exclude
Force
Credential
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
票数 2
EN

Stack Overflow用户

发布于 2018-06-20 04:53:58

下面是一个在特定参数集(或所有参数集)中查找参数的脚本。它应该能让你得到你想要的东西。

代码语言:javascript
复制
$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"
    }
}

如果您只想要参数集中的特定项,这里有一个简短的版本:

代码语言:javascript
复制
$commandName='Get-ChildItem'
$ParameterSetToMatch='Items'
$parameterlist |  
    Where-object {$_.ParameterSets.Keys -contains $ParameterSetToMatch} | 
     select-object Name
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50936256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档