PSObject Array of Arrays Return Powershell Read Individual Items/Rows

1.5k views Asked by At

I am using one Function to call another and returning a PSObject array or multiple arrays (I think). The return after each looped object is a set of values. The data in $a in Function Test2 is below but it's count is 3, meaning three objects or arrays, one for each folder. This seems pretty normal and if I wrote it to a CSV for a report I would be fine, but I am looking to manipulate the data in each array. When I try to manipulate the data it's trying to manipulate the arrays and I can't search or use items in each row. I also don't know how many folders I have so the solution needs to be universal and expandable. I don't know how to gain access to each row in all the arrays easily.

Function Test1 {
 [cmdletbinding()]
param(
    [Parameter(Position = 0, Mandatory = $true)]
    [string]$Folder
)

$array1 = @("Folder1_Test1","Folder1_Test2","Folder1_Test3","Folder1_Test4","Folder1_Test5    ","Folder2_Test6","Folder2_Test7","Folder2_Test8","Folder2_Test9","Folder3_Test1    0")
$array2 = @("Folder1_Test1","Folder1_Test4","Folder1_Test5","Folder2_Test9")


$data = @()
Foreach ($item in $array1) {
    If ($item -match $Folder -and $array2 -notcontains $item) {
        $Obj = New-Object PSObject -Property @{
            Folder = $Folder;
            SubFolder = $item;
            Message = "$item not found.";
        }
        $data += $Obj
}
}
Return ,$data
}

Function Test2 {
$Folders = @("Folder1", "Folder2", "Folder3")
$a = $Folders | ForEach-Object {Test1 $_}
$a.Count

foreach ($item in $a)
        { 
          $item.Folder
          $item.SubFolder
          $item.Message
        }
}

The output of $a is however the count is 3.

SubFolder      Message                   Folder 
---------      -------                   ------ 
Folder1_Test2  Folder1_Test2 not found.  Folder1
Folder1_Test3  Folder1_Test3 not found.  Folder1
Folder2_Test6  Folder2_Test6 not found.  Folder2
Folder2_Test7  Folder2_Test7 not found.  Folder2
Folder2_Test8  Folder2_Test8 not found.  Folder2
Folder3_Test10 Folder3_Test10 not found. Folder3

How can I get access to each row inside each object? I want to be able to search through a subfolder and then identify the folder it's on and write the message, something like this:

$a | ForEach-Object | Write-Host {"Subfolder $($_.Subfolder) is in $($_.Folder) and error message is $($_.Message)"}

Thanks in advance.

2

There are 2 answers

1
Jason Snell On BEST ANSWER

What you are creating is an array with three elements. Each element in the array is displaying information. When you write it out to the conosole you're seeing all the elements crammed together:

SubFolder          Message                       Folder 
---------          -------                       ------ 
Folder1_Test2      Folder1_Test2 not found.      Folder1
Folder1_Test3      Folder1_Test3 not found.      Folder1
Folder1_Test5      Folder1_Test5     not found.  Folder1
Folder2_Test6      Folder2_Test6 not found.      Folder2
Folder2_Test7      Folder2_Test7 not found.      Folder2
Folder2_Test8      Folder2_Test8 not found.      Folder2
Folder3_Test1    0 Folder3_Test1    0 not found. Folder3

If you look at $a[0] you will see this:

PS C:\WINDOWS\system32> $a[0]

SubFolder         Message                      Folder 
---------         -------                      ------ 
Folder1_Test2     Folder1_Test2 not found.     Folder1
Folder1_Test3     Folder1_Test3 not found.     Folder1
Folder1_Test5     Folder1_Test5     not found. Folder1

That is why the count returns a 3. If you use $a[0][0] you will see a single line since it is accessing the first element of $a, which is an array, and then accessing the first element of that array. You would have to use a nesting loop to access each element within the nested arrays.

0
Nakious On

Thanks to Jason I was able to get the code to work. I added this at the bottom of Test2 and the output is below, line by line.

Foreach ($Element in $a) {
   ForEach ($item in $Element) {
       Write-Host "Subfolder $($item.Subfolder) is in $($item.Folder) and 
error message is $($item.FolderMessage)"
   }


Subfolder Folder1_Test2 is in Folder1 and error message is Folder1_Test2 not found.
Subfolder Folder1_Test3 is in Folder1 and error message is Folder1_Test3 not found.
Subfolder Folder2_Test6 is in Folder2 and error message is Folder2_Test6 not found.
Subfolder Folder2_Test7 is in Folder2 and error message is Folder2_Test7 not found.
Subfolder Folder2_Test8 is in Folder2 and error message is Folder2_Test8 not found.
Subfolder Folder3_Test10 is in Folder3 and error message is Folder3_Test10 not found.