在PowerShell中返回单个元素的数组

mum43rcc  于 2023-06-29  发布在  Shell
关注(0)|答案(3)|浏览(125)

这应该非常简单-我需要从函数返回一个哈希表数组。当有多个哈希表时,这是有效的,但是当只有一个哈希表时,结果不是数组。我宁愿不测试结果是否是数组。

function GetArrayWith1Hashtable()
{
    $array = @()

    $hashtable = @{}
    $hashtable["a"] = "a" 
    $hashtable["b"] = "b" 
    $hashtable["c"] = "c"
    $array += $hashtable 

    Write-Host "GetArrayWith1Hashtable array.Length =" $array.Length
    Write-Host "GetArrayWith1Hashtable array.Count" $array.Count
    Write-Host "GetArrayWith1Hashtable array[0].Keys" $array[0].Keys

    $array
}

function GetArrayWith2Hashtables()
{
    $array = @()

    $hashtable = @{}
    $hashtable["a"] = "a" 
    $hashtable["b"] = "b" 
    $hashtable["c"] = "c"
    $array += $hashtable 

    $hashtable2 = @{}
    $hashtable2["d"] = "d" 
    $hashtable2["e"] = "e"
    $hashtable2["f"] = "f"
    $array += $hashtable2 

    Write-Host "GetArrayWith2Hashtables array.Length = " $array.Length
    Write-Host "GetArrayWith2Hashtables array.Count = " $array.Count
    Write-Host "GetArrayWith2Hashtables array[0].Keys =" $array[0].Keys
    Write-Host "GetArrayWith2Hashtables array.Count = "$array[1].Keys

    $array
}

$result1 = GetArrayWith1Hashtable
# $result1.Length - not available
Write-Host "Result of GetArrayWith1Hashtable result1.Count = " $result1.Count                      # Count = 2 (would expect this to be 1)
# $result1[0] not available - not an array

$result2 = GetArrayWith2Hashtables
Write-Host "Result of GetArrayWith2Hashtables result2.Length = "    $result2.Length # Length = 2
Write-Host "Result of GetArrayWith2Hashtables result2.Count = "     $result2.Count # Count = 2
Write-Host "Result of GetArrayWith2Hashtables result2[0].Keys = "     $result2[0].Keys # Keys = c a b
Write-Host "Result of GetArrayWith2Hashtables result2[1].Keys = "     $result2[1].Keys # Keys = d e f

 <#
  FULL OUTPUT:

 GetArrayWith1Hashtable array.Length = 1
 GetArrayWith1Hashtable array.Count 1
 GetArrayWith1Hashtable array[0].Keys c a b

 Result of GetArrayWith1Hashtable result1.Count =  2

 GetArrayWith2Hashtables array.Length =  2
 GetArrayWith2Hashtables array.Count =  2
 GetArrayWith2Hashtables array[0].Keys = c a b
 GetArrayWith2Hashtables array.Count =  d e f

 Result of GetArrayWith2Hashtables result2.Length =  2
 Result of GetArrayWith2Hashtables result2.Count =  2
 Result of GetArrayWith2Hashtables result2[0].Keys =  c a b
 Result of GetArrayWith2Hashtables result2[1].Keys =  d e f

 #>
qeeaahzv

qeeaahzv1#

实际上,你只需要在返回的项目前面加一个逗号就可以了。这告诉PowerShell按原样返回它,而不试图聪明地处理它。这也允许您在一个地方修复它,而不是在所有调用它的地方。

function GetArrayWith1Hashtable()
{
    $array = @()

    $hashtable = @{}
    $hashtable["a"] = "a" 
    $hashtable["b"] = "b" 
    $hashtable["c"] = "c"
    $array += $hashtable 

    Write-Host "GetArrayWith1Hashtable array.Length =" $array.Length
    Write-Host "GetArrayWith1Hashtable array.Count" $array.Count
    Write-Host "GetArrayWith1Hashtable array[0].Keys" $array[0].Keys

    # The only edit was to add the comma below
    ,$array
}

**更新:**虽然,我不认为你想要这个功能,但我会提到它,以便其他人能够理解其中的区别,并为他们的方法做出明智的选择。

为了让你的函数兼容,以便在管道中更有效地使用,更好的方法是为每个项调用Write-Output。

function GetArrayWith1Hashtable()
{
    Write-Output "a" 
    Write-Output "b" 
    Write-Output "c"
}

通过使用Write-Output,您允许PowerShell在管道中为每个对象单独调用下一个命令的Process block。否则,它将调用下一个命令并向其传递单个数组对象。您可以在此处阅读更多有关管道对象的信息

7hiiyaii

7hiiyaii2#

将返回类型转换为数组:

$result1 = @(GetArrayWith1Hashtable)
icnyk63a

icnyk63a3#

为了强制函数返回一个数组(不管集合的大小),我这样做:

return Write-Output $array -NoEnumerate

我认为-NoEnumerate开关有助于使我想要做的事情更加明确。

相关问题