问题描述
早上好。
我可以就我在这里做错的地方获得一些帮助吗?无论出于何种原因,返回输出正在打印整个数组而不是遍历它。该函数假设允许用户在命令行语法中输入,如果没有提供任何内容,它会选择可供选择的内容。
好的,到目前为止,选择选择以及为函数提供参数是有效的,关于它获取正确的数组。它没有做的是遍历数组。
见下文:
$Group1 = @("1st Group","2nd Group")
$Group2 = @("3rd Group","4th Group")
$Resultss = {
New-Object System.Management.Automation.CompletionResult 'Group1','Group1','ParameterValue','this is Group 1'
New-Object System.Management.Automation.CompletionResult 'Group2','Group2','this is Group 2'
}
Microsoft.PowerShell.Core\Register-ArgumentCompleter -CommandName Test-Group -ParameterName GrpSelec -ScriptBlock $Resultss
Function Test-Group{
param(
[string[]]$GrpSelec)
if(!$PSBoundParameters.ContainsKey('GrpSelec')){
$AllGroups = @("Group1","Group2")
for($i=0; $i -lt $AllGroups.Count; $i++){
Write-Host "$($i): $($AllGroups[$i])"}
$GrpSelec = Read-Host -Prompt "Select Group(s)"
$GrpSelec = $GrpSelec -split " "}
$swap = Switch -exact ($GrpSelec){
{$_ -eq 0 -or $_ -eq "Group1"} {"$Group1"}
{$_ -eq 1 -or $_ -eq "Group2"} {"$Group2"}
}
Foreach($Group in $swap){
"$Group"}
}
输出:
PS C:\WINDOWS\system32> Test-Group -GrpSelec Group1
1st Group 2nd Group
PS C:\WINDOWS\system32> Test-Group
0: Group1
1: Group2
Select Group(s): 0
1st Group 2nd Group
输出应该是什么样子:
1st Group
2nd Group
希望这对比我更聪明的人有意义!我还尝试将我的脚本与该脚本的回答方式相关联,但无法使其正常工作: Here
解决方法
继续我的评论。
对您的代码进行小幅更新,以允许在 -GrpSelec 参数上使用 IntelliSense/Tab-Completion 并保留您的参数竞争对手/帮助消息。
$Group1 = @("1st Group","2nd Group")
$Group2 = @("3rd Group","4th Group")
$Resultss = {
New-Object System.Management.Automation.CompletionResult 'Group1','Group1','ParameterValue','this is Group 1'
New-Object System.Management.Automation.CompletionResult 'Group2','Group2','this is Group 2'
}
Microsoft.PowerShell.Core\Register-ArgumentCompleter -CommandName Test-Group -ParameterName GrpSelec -ScriptBlock $Resultss
Function Test-Group
{
param
(
[ValidateSet(
'0','1','0: Group1','1: Group2'
)]
[string[]]$GrpSelec
)
if(!$PSBoundParameters.ContainsKey('GrpSelec'))
{
$AllGroups = @('Group1','Group2')
for($i=0; $i -lt $AllGroups.Count; $i++)
{Write-Host "$($i): $($AllGroups[$i])"}
$GrpSelec = Read-Host -Prompt 'Select Group(s)'
$GrpSelec = $GrpSelec -split ' '
}
$swap = Switch -exact ($GrpSelec)
{
{$PSItem -eq 0 -or $PSItem -eq 'Group1' -or $PSItem -eq '0: Group1'} {$Group1}
{$PSItem -eq 1 -or $PSItem -eq 'Group2' -or $PSItem -eq '0: Group2'} {$Group2}
}
Foreach($Group in $swap)
{$Group}
}
Test-Group
# Results
<#
Test-Group
0: Group1
1: Group2
Select Group(s):
Select Group(s): 1
3rd Group
4th Group
#>
Test-Group -GrpSelec 0
<#
a space or tab pops IntelliSense for the selection list.
Test-Group -GrpSelec 0
1st Group
2nd Group
#>
(Get-Command Test-Group).Parameters.Values.Attributes
# Results
<#
Position : 0
ParameterSetName : __AllParameterSets
Mandatory : False
ValueFromPipeline : False
ValueFromPipelineByPropertyName : False
ValueFromRemainingArguments : False
HelpMessage :
HelpMessageBaseName :
HelpMessageResourceId :
DontShow : False
TypeId : System.Management.Automation.ParameterAttribute
IgnoreCase : True
ValidValues : {0,1,Group1,Group2,0: Group1,1: Group2}
TypeId : System.Management.Automation.ValidateSetAttribute
TransformNullOptionalParameters : True
TypeId : System.Management.Automation.ArgumentTypeConverterAttribute
#>
根据我们关于动态完成者/验证集示例的评论交流进行更新。
function Test-DynamicArgumentCompleter
{
[CmdletBinding()]
[Alias('tdac')]
param
(
[Parameter(Mandatory)]
[ArgumentCompleter(
{
Get-ChildItem -Path 'C:\Scripts' -Name |
ForEach-Object {
if ($PSitem -like '* *'){"'$PSitem'"}
else {$PSitem}
}
})][string]$FileName
)
"Chosen file name: $FileName"
}
function Test-DynamicValidateSet
{
[CmdletBinding()]
[Alias('tdvs')]
Param ()
DynamicParam {
$ParameterName = 'Path'
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 1
$AttributeCollection.Add($ParameterAttribute)
$arrSet = Get-ChildItem -Path '.\' -Directory |
Select-Object -ExpandProperty FullName
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
$AttributeCollection.Add($ValidateSetAttribute)
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName,[string],$AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName,$RuntimeParameter)
return $RuntimeParameterDictionary
}
begin
{$Path = $PsBoundParameters[$ParameterName]}
process
{dir -Path $Path}
}