我在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
好的,我找到了。它错过了“+ =”
$name = $row["name"]
$sampletab[$y] += @{
$name = $row["value"]
}
谢谢 !