Reputation: 311
I am trying to convert an array into a hashtable but I keep getting this error
Unable to index into an object of type System.Management.Automation.PSObject.
I have searched everywhere and could not find others having the same problem.
My code:
[array]$compArray = $ds3 | select -Property DeviceName, IP_Address
$DeviceHashtable = @{}
$compArray[0][0]
for ($i=0;$i -lt $compArray.length;$i++)
{
$1=[string]$compArray[0][$i];
$2=[string]$compArray[1][$i];
$DeviceHashTable.add("$1", "$2")
}
$ds3 is a system.data.datatable object and if I do $compArray | display-table all the data I want is there. Any help is appreciated :)
Upvotes: 1
Views: 3115
Reputation: 60918
Try this:
$compArray = $ds3 | select -Property DeviceName, IP_Address
$DeviceHashtable = @{}
$compArray | % { $DeviceHashtable.add( $_.DeviceName, $_.IP_Address )}
Upvotes: 3