PSObject Array of Arrays Powershell Returns Read Single Elements / Rows

I use one function to call another and return 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 lower, but the number is 3, which means three objects or arrays, one for each folder. This seems pretty normal, and if I wrote it in CSV for a report, everything would be fine, but I'm looking to manipulate the data in each array. When I try to manipulate data that they try to manipulate arrays, and I can not search or use elements in each row. I also don’t know how many folders I have, so the solution should be universal and extensible. I don't know how easy it is to access every row in all arrays.

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, however, 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

? , , , - :

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

.

+4
2

, , . . conosole, , :

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

$a [0], :

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

3. $a[0][0], , $a, , . .

+2

. Test2, - , .

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.
0

Source: https://habr.com/ru/post/1685902/


All Articles