Foreach输出整个数组


早上好 我可以得到一些帮助,我可能做错了什么?不管出于什么原因,返回的输出是打印整个数组,而不是遍历它。假设该函数允许用户在命令行语法中输入,如果没有提供任何内容,它将对可供选择的内容进行选择。

好的,到目前为止,选择和提供参数给函数工作,关于它抓取正确的数组。它不做的是遍历数组。

见下文:

$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', 'ParameterValue', 'this is Group 2'
}
Microsoft.PowerShell.CoreRegister-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:WINDOWSsystem32> Test-Group -GrpSelec Group1
1st Group 2nd Group
PS C:WINDOWSsystem32> Test-Group
0: Group1
1: Group2
Select Group(s): 0
1st Group 2nd Group

输出应该是什么样子:

1st Group
2nd Group

希望这对比我聪明的人有意义!我还试图将我的脚本与这个脚本的回答联系起来,但是,无法让它工作:

继续我的评论

对你的代码进行小更新,允许在- grpselect参数上进行智能感知/tab补全,并保留你的参数竞争对手/帮助消息。

$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', 'ParameterValue', 'this is Group 2'
}
Microsoft.PowerShell.CoreRegister-ArgumentCompleter -CommandName Test-Group -ParameterName GrpSelec -ScriptBlock $Resultss
Function Test-Group
{
param
(
[ValidateSet(
'0', 
'1', 
'Group1', 
'Group2', 
'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
#>

根据我们关于动态补全器/validatesset示例的评论交换进行更新。

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}
}

最新更新