I want to pass sequence counts for Try/Catch error handling purposes. If my code catches an error, I'd like to be able to return something like "File 2 of 4 failed" without incriminating a sequence variable. I have the following so far:
($hash = @(Get-ChildItem C:\Dir -Recurse -Include *.txt) | Where {$_.length -gt 0}) | ForEach-Object { Write-Host $_.BaseName Write-Host $hash.IndexOf($_)
}
"Array Count = $($hash.Count)"This will output:
File1
0
File2
1
Array Count = 2I'm not sure how to get the Index number outside the loop.
1 Answer
Answer: Use a global variable.
Add inside the ForEach-Object a statement such as:
$x = $hash.IndexOf($_);When the loop stops, the variable $x will contain the index number.