簡體   English   中英

Powershell函數返回數組未哈希表

[英]Powershell function return Array not Hashtable

我有一個我不明白的問題。 我有一個看起來像的功能:

Function hashTable
{
    Param($release)

    $releaseArray = @()
    if (![string]::IsNullOrWhitespace($release))
    {
        $releaseArray = $release.split("{|}", [System.StringSplitOptions]::RemoveEmptyEntries)
        $releaseArray.gettype() | Select-Object Name
        if($releaseArray.Count -gt 0) {
            $namePathArray = @{}
            foreach($namePath in $releaseArray) {
                $splitReleaseArray = $namePath.split("{,}", [System.StringSplitOptions]::RemoveEmptyEntries)
                $namePathArray.add($splitReleaseArray[0], $splitReleaseArray[1])
            }

            #here it echos propper hashtable

            $namePathArray.gettype() | Select-Object Name
            if($namePathArray.Count -gt 0) {
                #here it echos propper hashtable as well
                return $namePathArray
            }
        }
    }
}

但是當我調用此函數時,我得到的是一個數組,而不是如下所示的哈希表:

Name
----
String[]
test
reorder

輸入參數示例: -release "reorder,c:\\Repo\\App|test,test"

我想知道我是否缺少什么?

您可以使用GetType() |Select Name語句有效地污染輸出流。 刪除它們,或使用Write-Host來顯示類型名稱:

Function hashTable
{
    Param($release)

    $releaseArray = @()
    if (![string]::IsNullOrWhitespace($release))
    {
        $releaseArray = $release.split("{|}", [System.StringSplitOptions]::RemoveEmptyEntries)
        Write-Host ($releaseArray.gettype() | Select-Object -Expand Name)
        if($releaseArray.Count -gt 0) {
            $namePathArray = @{}
            foreach($namePath in $releaseArray) {
                $splitReleaseArray = $namePath.split("{,}", [System.StringSplitOptions]::RemoveEmptyEntries)
                $namePathArray.add($splitReleaseArray[0], $splitReleaseArray[1])
            }

            #here it echos propper hashtable

            Write-Host ($namePathArray.gettype() | Select-Object -Expand Name)
            if($namePathArray.Count -gt 0) {
                #here it echos propper hashtable as well
                return $namePathArray
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM