Repository: microsoft/CSS-Exchange
File: Diagnostics/AVTester/Test-ExchAVExclusions.ps1 (PowerShell)

Code before:
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\FIP-FS').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\GroupMetrics').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\Logging').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\Mailbox').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\Adam').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\IpFilter').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\Queue').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\SenderReputation').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\Temp').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Logs').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Pickup').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Replay').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\UnifiedMessaging\Grammars').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\UnifiedMessaging\Prompts').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\UnifiedMessaging\Temp').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\UnifiedMessaging\Voicemail').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\Working\OleConverter').tolower())
$BaseFolders.Add((Join-Path $env:SystemDrive '\inetpub\temp\IIS Temporary Compressed Files').tolower())
$BaseFolders.Add((Join-Path $env:SystemRoot '\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files').tolower())
$BaseFolders.Add((Join-Path $env:SystemRoot '\System32\Inetsrv').tolower())


$CmdAvailable = Get-Command Get-ExchangeServer -ErrorAction SilentlyContinue
if ( -not $CmdAvailable) {
    try {
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
        # Checking if it is a mailbox server to avoid errors if it is an Edge server.
        if ((Get-ExchangeServer $env:COMPUTERNAME).IsMailboxServer) {
            # Add all database folder paths
            foreach ($Entry in (Get-MailboxDatabase -Server $Env:COMPUTERNAME)) {
                $BaseFolders.Add((Split-Path $Entry.EdbFilePath -Parent).tolower())
                $BaseFolders.Add(($Entry.LogFolderPath.pathname.tolower()))
            }
        }
    } catch {
        Write-Warning "Failed to Load Exchange PowerShell Module..."
        Write-Warning "We could not verify the database folder paths"
    }
}

# Get transport database path
[xml]$TransportConfig = Get-Content (Join-Path $env:ExchangeInstallPath "Bin\EdgeTransport.exe.config")
$BaseFolders.Add(($TransportConfig.configuration.appsettings.Add | Where-Object { $_.key -eq "QueueDatabasePath" }).value.tolower())
$BaseFolders.Add(($TransportConfig.configuration.appsettings.Add | Where-Object { $_.key -eq "QueueDatabaseLoggingPath" }).value.tolower())

# Remove any Duplicates
$BaseFolders = $BaseFolders | Select-Object -Unique

#'$env:SystemRoot\Temp\OICE_<GUID>'
#'$env:SystemDrive\DAGFileShareWitnesses\<DAGFQDN>'


Review comment (suggestion):
We should not be directly adding in the PowerShell SnapIn like this, as we bypass RBAC. 

Use the `Confirm-ExchangeShell` from the Shared location. 

Code after:
}

if ($null -eq $env:ExchangeInstallPath -or (
        -not (Test-Path $env:ExchangeInstallPath))) {
    Write-Warning "Failed to find Exchange Install Path"
    exit
}

# Create the Array List
$BaseFolders = New-Object Collections.Generic.List[string]

# List of base Folders
$BaseFolders.Add((Join-Path $env:SystemRoot '\Cluster').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\ClientAccess\OAB').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\FIP-FS').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\GroupMetrics').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\Logging').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\Mailbox').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\Adam').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\IpFilter').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\Queue').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\SenderReputation').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Data\Temp').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Logs').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Pickup').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\TransportRoles\Replay').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\UnifiedMessaging\Grammars').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\UnifiedMessaging\Prompts').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\UnifiedMessaging\Temp').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\UnifiedMessaging\Voicemail').tolower())
$BaseFolders.Add((Join-Path $env:ExchangeInstallPath '\Working\OleConverter').tolower())
$BaseFolders.Add((Join-Path $env:SystemDrive '\inetpub\temp\IIS Temporary Compressed Files').tolower())
$BaseFolders.Add((Join-Path $env:SystemRoot '\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files').tolower())
$BaseFolders.Add((Join-Path $env:SystemRoot '\System32\Inetsrv').tolower())

# Checking if it is a mailbox server to avoid errors if it is an Edge server.
if ((Get-ExchangeServer $env:COMPUTERNAME).IsMailboxServer) {
    # Add all database folder paths
    foreach ($Entry in (Get-MailboxDatabase -Server $Env:COMPUTERNAME)) {
        $BaseFolders.Add((Split-Path $Entry.EdbFilePath -Parent).tolower())
        $BaseFolders.Add(($Entry.LogFolderPath.pathname.tolower()))
    }
}

# Get transport database path
[xml]$TransportConfig = Get-Content (Join-Path $env:ExchangeInstallPath "Bin\EdgeTransport.exe.config")
$BaseFolders.Add(($TransportConfig.configuration.appsettings.Add | Where-Object { $_.key -eq "QueueDatabasePath" }).value.tolower())
$BaseFolders.Add(($TransportConfig.configuration.appsettings.Add | Where-Object { $_.key -eq "QueueDatabaseLoggingPath" }).value.tolower())

# Remove any Duplicates
$BaseFolders = $BaseFolders | Select-Object -Unique