0

When I output elements inside an array of an array (example: $data_array[0][0], I only get a char. Why is this? I was expecting a String of LAP-150 for the [0][0] position of this array.

import-module activedirectory
$domain_laptops = get-adcomputer -filter 'Name -like "LAP-150"' -properties operatingsystem, description | select name, description, operatingsystem
$data_array = @()

foreach ($laptop in $domain_laptops){
        $bde = manage-bde -computername $laptop.name -status
        $encryptionstatus=(manage-bde -status -computername $laptop.name | where {$_ -match 'Conversion Status'})
        if ($encryptionstatus){
            $encryptionStatus=$encryptionstatus.split(":")[1].trim()
        }
        else{
            $EncryptionStatus="Not Found..."
        }
        $data_array += ,($laptop.name,$laptop.description,$laptop.operatingsystem,$encryptionstatus)
    }


write-output $data_array[0][0]

The output of the above script is just the character "L" which is the first character in the $laptop.name variable. Where am I going wrong? I assume it's something to do with how I'm appending to the array but I've tried different combinations of parenthesis, commas, no parenthesis, etc to no avail.

1 Answer 1

0

When you run the following command,

$data_array += ($laptop.name,$laptop.description,$laptop.operatingsystem,$encryptionstatus)

Remove the , after += sign.

Tests performed to show you how it works

$array = @()
$array = 1, 2, 3, 4
$array.Length   //-> 4

$array2 = @()
$array2 = , 1, 2
$array2.Length  //-> 2

$array3 = @()
$array3 = , (1, 2)
$array3.Length  //-> 1

$array4 = @()
$array4= @(), (1, 2)
$array4.Length  //-> 2

When you use ,, you have to define the same type of element before and after. During your iterations, you are using += , (something). Left of , doesnt have any data so all the text after it is considered a string seperated by commas.

For 2D arrays, i would recommend using the hash in the mix,

$data_array += @{name=$laptop.name;description=$laptop.description;os=$laptop.operatingsystem;encryption=$encryptionstatus}

$data_array[0]["name"] // Prints the name of first laptop in array.
Sign up to request clarification or add additional context in comments.

10 Comments

I don't think this is correct. When I check the output of $data_array[0], I get more than just the $laptop.name. I get the $laptop.name, $laptop.description, $laptop.operatingsystem, and $encryption status in ONE line. I believe these are all considered one element in the array but are somehow being considered a string instead of an array.
Right.. needed to remove the first , before the array you were inserting. Remove the , after += and before ($laptop.name)
That helped!! The only issue is that I still need to have arrays within an array. I'll be running this with a much larger amount of LAP-### than just one. How can I get $laptop.name, $laptop.description, $laptop.operatingsystem, and $encryptionstatus to be their own array and then with each laptop add a new array to the array in a 2D array?
Sorry if it wasn't clear enough in my description but that was my intended goal
Agreed that it is a personal preference. I apologize if I was not making it clear that I was stating an opinion. I use the PSCustomObjects because of interoperability and how they play nice when piping them elsewhere.
|

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.