Powershell哈希表无法在foreach之外访问

问题描述 投票:1回答:1

我在foreach里面有一个foreach功能。在foreach中设置的哈希表在外面无法访问,我不明白为什么。

sampletab的范围在“脚本”中

    function Generate(){
        $script:sampletab=@{}
        $y=0
        $samples = Invoke-Sqlcmd -Query "SELECT * FROM ..."

        foreach ($row in $samples) 
        {
            $paramID = $row["xxx"]
            $query = "SELECT * FROM ... ${paramID};"
            $parameters = Invoke-Sqlcmd -Query $query               
            foreach ($row in $parameters)
            {   
                $name = "coucou"
                $name = $row["name"]
                $sampletab[$y] = @{
                    $name = $row["value"]
                }           
            }               
            $y++    
            break
        }
    }
    Generate
    echo $sampletab[0].Keys  # gives me only 1 key, should be 20 keys
powershell
1个回答
0
投票

好的,我找到了。它错过了“+ =”

        $name = $row["name"]
        $sampletab[$y] += @{
            $name = $row["value"]
        }       

谢谢 !

© www.soinside.com 2019 - 2024. All rights reserved.