我在从特定的参数集中获取参数时遇到了一些问题。我已经通过获取所有参数并使用$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
}
发布于 2018-06-20 05:40:26
使用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
发布于 2018-06-20 04:53:58
下面是一个在特定参数集(或所有参数集)中查找参数的脚本。它应该能让你得到你想要的东西。
$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
https://stackoverflow.com/questions/50936256
复制相似问题