1

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?

4

1 回答 1

1

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"]
}
于 2012-08-22T08:00:58.780 回答