Repository: microsoft/CSS-Exchange
File: Diagnostics/FindFrontEndActivity.ps1 (PowerShell)

Code before:
end {
    $timeThreshold = $null
    if ($PSCmdlet.ParameterSetName -eq 'Recent') {
        $timeThreshold = (Get-Date).Add(-$TimeSpan)
    }

    $jobs = $serverNames | ForEach-Object {
        Invoke-Command -AsJob -ComputerName $_ -ScriptBlock {
            $exchangeInstallPath = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ExchangeServer\v15\Setup -ErrorAction SilentlyContinue).MsiInstallPath
            $httpProxyLogPath = Join-Path $exchangeInstallPath "Logging\HttpProxy"
            $results = New-Object System.Collections.ArrayList
            foreach ($thisProtocol in $using:Protocol) {
                $logPathForThisProtocol = Join-Path $httpProxyLogPath $thisProtocol
                if (-not (Test-Path $logPathForThisProtocol)) {
                    continue
                }

                $files = Get-ChildItem "$logPathForThisProtocol\*.log" | Where-Object {
                    if ($null -ne $using:timeThreshold) {
                        $_.LastWriteTime -gt $using:timeThreshold
                    } else {
                        $_.LastWriteTime -gt $using:StartTime -and $_.LastWriteTime -lt $using:EndTime
                    }
                } | Sort-Object LastWriteTime

                $files = $files | Where-Object { Select-String $using:samAccountRegexForFileMatch $_.FullName -Quiet }

                if ($files.Count -gt 0) {
                    if ($using:Quiet) {
                        return $env:COMPUTERNAME
                    } else {
                        $files | ForEach-Object {
                            $relevantEntries = Import-Csv -Path $_ | Where-Object {
                                $_.AuthenticatedUser -match $using:samAccountRegexForFieldMatch -and [int]::Parse($_.TotalRequestTime) -gt $using:LatencyThreshold
                            }

                            if ($thisProtocol -eq "MAPI" -and -not $using:IncludeNonExecutes) {
                                $relevantEntries = $relevantEntries | Where-Object { $_.ClientRequestId -like "*Execute*" }
                            }

                            $relevantEntries | ForEach-Object { [void]$results.Add($_) }
                        }
                    }
                }
            }

            return $results
        }
    }

    $total = $jobs.Count

Review comment (suggestion):
```suggestion
                $files = @($files | Where-Object { Select-String $using:samAccountRegexForFileMatch $_.FullName -Quiet })
```
To make sure that it is treated as an array

Code after:
    $samAccountRegexForFieldMatch = [string]::Join('|', ($SamAccountName | ForEach-Object { "\\$_$" }))
}

process {
    foreach ($name in $ServerName) {
        [void]$serverNames.Add($name)
    }
}

end {
    $timeThreshold = $null
    if ($PSCmdlet.ParameterSetName -eq 'Recent') {
        $timeThreshold = (Get-Date).Add(-$TimeSpan)
    }

    $jobs = $serverNames | ForEach-Object {
        Invoke-Command -AsJob -ComputerName $_ -ScriptBlock {
            $exchangeInstallPath = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ExchangeServer\v15\Setup -ErrorAction SilentlyContinue).MsiInstallPath
            $httpProxyLogPath = Join-Path $exchangeInstallPath "Logging\HttpProxy"
            $results = New-Object System.Collections.ArrayList
            foreach ($thisProtocol in $using:Protocol) {
                $logPathForThisProtocol = Join-Path $httpProxyLogPath $thisProtocol
                if (-not (Test-Path $logPathForThisProtocol)) {
                    continue
                }

                $files = Get-ChildItem "$logPathForThisProtocol\*.log" | Where-Object {
                    if ($null -ne $using:timeThreshold) {
                        $_.LastWriteTime -gt $using:timeThreshold
                    } else {
                        $_.LastWriteTime -gt $using:StartTime -and $_.LastWriteTime -lt $using:EndTime
                    }
                } | Sort-Object LastWriteTime

                $files = @($files | Where-Object { Select-String $using:samAccountRegexForFileMatch $_.FullName -Quiet })

                if ($files.Count -gt 0) {
                    if ($using:Quiet) {
                        return $env:COMPUTERNAME
                    } else {
                        $files | ForEach-Object {
                            $relevantEntries = Import-Csv -Path $_ | Where-Object {
                                $_.AuthenticatedUser -match $using:samAccountRegexForFieldMatch -and [int]::Parse($_.TotalRequestTime) -gt $using:LatencyThreshold
                            }

                            if ($thisProtocol -eq "MAPI" -and -not $using:IncludeNonExecutes) {
                                $relevantEntries = $relevantEntries | Where-Object { $_.ClientRequestId -like "*Execute*" }
                            }

                            $relevantEntries | ForEach-Object { [void]$results.Add($_) }
                        }