Arrays PSObject数组的数组返回Powershell读取单个项/行

Arrays PSObject数组的数组返回Powershell读取单个项/行,arrays,powershell,psobject,Arrays,Powershell,Psobject,我使用一个函数调用另一个函数并返回一个PSObject数组或多个数组(我想)。每个循环对象之后的返回是一组值。函数Test2中$a中的数据如下,但其计数为3,表示三个对象或数组,每个文件夹一个。这似乎很正常,如果我把它写在CSV中作为报告,我会很好,但我希望操作每个数组中的数据。当我试图操纵数据时,它试图操纵数组,而我无法搜索或使用每行中的项。我也不知道我有多少文件夹,所以解决方案需要通用性和可扩展性。我不知道如何轻松访问所有数组中的每一行 Function Test1 { [cmdletbi

我使用一个函数调用另一个函数并返回一个PSObject数组或多个数组(我想)。每个循环对象之后的返回是一组值。函数Test2中$a中的数据如下,但其计数为3,表示三个对象或数组,每个文件夹一个。这似乎很正常,如果我把它写在CSV中作为报告,我会很好,但我希望操作每个数组中的数据。当我试图操纵数据时,它试图操纵数组,而我无法搜索或使用每行中的项。我也不知道我有多少文件夹,所以解决方案需要通用性和可扩展性。我不知道如何轻松访问所有数组中的每一行

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
        }
}
$a的输出为,但计数为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)"}

提前谢谢

您要创建的是一个包含三个元素的数组。数组中的每个元素都显示信息。当你把它写在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的第一个元素,即数组,然后访问该数组的第一个元素。您必须使用嵌套循环来访问嵌套数组中的每个元素。

多亏了Jason,我才能够让代码正常工作。我在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.

谢谢你解释杰森。这么简单,我想表现得太花哨了,但谢谢你让我直截了当。我用下面的代码对它进行了测试,得到了结果。密码应答。