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.

kryptic
  • 3
  • 1

1 Answers1

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.
Jawad
  • 11,028
  • 3
  • 24
  • 37
  • 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. – kryptic Feb 12 '20 at 22:10
  • Right.. needed to remove the first `,` before the array you were inserting. Remove the `,` after `+=` and before `($laptop.name)` – Jawad Feb 12 '20 at 22:13
  • 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? – kryptic Feb 12 '20 at 22:19
  • Sorry if it wasn't clear enough in my description but that was my intended goal – kryptic Feb 12 '20 at 22:19
  • @samb see updated answer. You will not be able to create an array of an array – Jawad Feb 12 '20 at 22:27
  • Thanks for the reply. I understand what you are saying here. I adjusted my code. I'm still have a problem getting the 4 input variables to be added to their own array within an array during each iteration of the loop. One of the things I tried when troubleshooting before posting here was defining the array type as a string array when I declared it on the third line. That didn't change anything. I need to figure out how to get my 4 variables to be appended to the $data_array as an array each iteration. – kryptic Feb 12 '20 at 22:31
  • Use the hashtable as an element in the array like the example at the bottom of the post – Jawad Feb 12 '20 at 22:32
  • I think that using [PSCustomObject] is significantly better and more flexible than the HT suggestion here. See my answer to [this question](https://stackoverflow.com/questions/60217000/iterate-through-multiple-arrays-in-powershell-script/60219341#60219341) for a detailed explanation of the differences. – Matthew Feb 14 '20 at 02:57
  • @Matthew I would differ to [this question](https://stackoverflow.com/questions/14012773/difference-between-psobject-hashtable-and-pscustomobject) to read about the differences in psObject, hashtable and psCustomObject. Its a choice and for the simplicity of using three variables, I personally prefer hashtables. – Jawad Feb 14 '20 at 03:29
  • 1
    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. – Matthew Feb 14 '20 at 04:18