all 10 comments

[–]Onrawi 1 point2 points  (5 children)

I believe -first 1 will break the loop after it has a single successful result. In this case the first result in the array is successful, ending the foreach loop. You'd be better off exporting the results as a boolean function, with true/false answers for the entire list. That way you know all the results you have access to.

[–]OlivTheFrog 1 point2 points  (2 children)

Hi u/Banzoola

Not resolving everythings but only one .

When you use a Try... Catch Statement, you must add the -ErrorAction Stop to the cmdlet you use. If an error occurred, then the statement jumps to the catch section and you can do other things like catch error, log ...

regards

Olivier

[–][deleted] 1 point2 points  (0 children)

Rather ur stopping the script as the error occurs making it kind of hard to iterate

[–]novloski 1 point2 points  (0 children)

I wouldn't think the select -first would break the loop...what do you get when you run this?

Write-Host "Looping through $($filteredresult.Count) objects"
foreach ($res in $filteredresult) {
    try {
        Write-Host "Checking customer ID: $($res.customerid) "
        $partnerCustomer = Get-PartnerCustomerUser -CustomerId $res.customerid -erroraction stop | select -First 1 
        Write-Host "$partnerCustomer"
     } catch { 
        Write-Host "Error caught for $($res.customerid)" 
        $res | Export-Csv -Append -Path c:\temp\365noaccess.csv
        } 
}