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.
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:
If you look at $a[0] you will see this:
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.