1

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 :)

1 Answer 1

3

Try this:

$compArray = $ds3 | select -Property DeviceName, IP_Address
$DeviceHashtable = @{}
$compArray | %  {  $DeviceHashtable.add( $_.DeviceName, $_.IP_Address )}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank-you, that worked perfectly. The % sign is saying do X as many times as you can correct?
% is the alias for foreach-object cmdlet. Try: get-help foreach-obeject -full to read about it! Glad to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.