powershell - 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.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
希望这对比我聪明的人有意义!我还尝试将我的脚本与该脚本的回答方式相关联,但无法使其正常工作: 这里
解决方案
继续我的评论。
对您的代码进行小幅更新,以允许对 -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', 'ParameterValue', 'this is Group 2'
}
Microsoft.PowerShell.Core\Register-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
#>
根据我们关于动态完成器/验证集示例的评论交流进行更新。
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}
}
推荐阅读
- html - 无论如何要恢复对源代码的提交
- sql - 带有 case when 的 SQL case 语句
- python - PyKDE:从 PyQt4 迁移到 PyQt5
- python - 知道什么时候最好在 Wagtail 网站上使用 Wagtail 页面或 Django 模型
- javascript - 如何仅在移动浏览器上修复完全随机的 CSS 故障问题?
- node.js - REACT.NATIVE__NPM_UnhandledPromiseRejectionWarning: RangeError [ERR_SOCKET_BAD_PORT] options.port 应该 >= 0 并且 < 65536。收到 65536
- image - 如何使用 PDFBox 2.0 从 PDF 中删除特定图像
- kubernetes - 如何停止replicaSet终止请求的pod数量
- hyperledger-sawtooth - Hyperledger Sawtooth docker 容器“设置 tp”不起作用
- cmake - 使用 cmake 构建 LLVM。-D_GLIBCXX_USE_CXX11_ABI=0 是否被覆盖?