我正在使用一個函數調用另一個函數並返回一個PSObject數組或多個數組(我認爲)。每個循環對象之後的返回值是一組值。函數Test2中的$ a中的數據在下面,但它的計數是3,表示三個對象或數組,每個文件夾一個。這似乎很正常,如果我把它寫到一個CSV報表中,我會很好,但我正在尋找操縱每個數組中的數據。當我嘗試操縱數據時,它試圖操縱數組,並且我無法在每一行中搜索或使用項目。我也不知道我有多少個文件夾,所以解決方案需要具有通用性和可擴展性。我不知道如何輕鬆訪問所有數組中的每一行。PSObject數組數組返回Powershell讀取單個項目/行
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)"}
感謝。
謝謝你解釋傑森。太簡單了,我試圖變得太花哨,但是謝謝你讓我直立。我用下面的代碼測試了它,並得到了我的結果。代碼回答。 – Nakious