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

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

Review comment (suggestion):
This does require the script to be run as Administrator otherwise, you will get an error back.

![image](https://user-images.githubusercontent.com/22776718/218496853-0f9eb76b-7eff-4fad-806c-96267194140c.png)


Code after:

    if (-not (Confirm-Administrator)) {
        Write-Host "This script must be run as an Administrator."
        exit
    }

    $serverNames = New-Object System.Collections.ArrayList
    $SamAccountName = $SamAccountName | ForEach-Object { [Regex]::Escape($_) }
    $samAccountRegexForFileMatch = [string]::Join('|', ($SamAccountName | ForEach-Object { "\\$_," }))
    $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 {