Reputation: 3967
This is my powershell code to iniltialize and populate the hash of hash
$thash = @{};
$thash.add("10.192.200.35",@{OS="XP";BIT="32"})
$thash.add("10.192.200.36",@{OS="XP";BIT="64"})
$thash.add("10.192.200.37",@{OS="XP";BIT="32"})
I am trying to iterate and access the elements in the hash of hash like this
foreach($index in $thash)
{
echo $thash[$index]["BIT"];
echo $thash[$index]["OS"]
}
But i am getting the error
Cannot index into a null array.
At line:
+ echo $thash[$index][ <<<< "BIT"];
+ CategoryInfo : InvalidOperation: (BIT:String) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
How can i access the underlying keys inside the hash of hash?
Upvotes: 1
Views: 3271
Reputation: 13483
Had to play with it a bit, but here's what you need:
$thash = @{};
$thash.add("10.192.200.35",@{OS="XP";BIT="32"})
$thash.add("10.192.200.36",@{OS="XP";BIT="64"})
$thash.add("10.192.200.37",@{OS="XP";BIT="32"})
foreach ($key in $thash.Keys)
{
$key
$thash[$key]["OS"]
$thash[$key]["BIT"]
}
Upvotes: 1