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

Code before:
        $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
    $completed = 0
    $results = New-Object System.Collections.ArrayList
    $jobs | ForEach-Object {

Review comment (suggestion):
Not sure how common it is, but possible since this script doesn't require to be run on the server itself you can have different time zones from the execution computer and the Exchange Servers or possibly have different time zones within all the servers themselves. Best to use UTC. 

Code after:
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($_) }
                        }
                    }
                }
            }