Repository: microsoft/CSS-Exchange
File: Setup/SetExchAVExclusions/Set-ExchAVExclusions.ps1 (PowerShell)

Code before:

. $PSScriptRoot\..\..\Shared\Confirm-Administrator.ps1
. $PSScriptRoot\..\..\Shared\Confirm-ExchangeShell.ps1
. $PSScriptRoot\..\..\Shared\Get-ExchAVExclusions.ps1
. $PSScriptRoot\..\..\Diagnostics\AVTester\Write-SimpleLogFile.ps1

# Log file name
$LogFile = "SetExchAvExclusions.log"

# Confirm that we are an administrator
if (-not (Confirm-Administrator)) {
    Write-Error "Please run as Administrator"
    exit
}

if (-not $ListRecommendedExclusions) {
    if ( $($host.Version.Major) -lt 5 -or ( $($host.Version.Major) -eq 5 -and $($host.Version.Minor) -lt 1) ) {
        Write-Error "This version of Windows do not have Microsoft Defender"
        exit
    }

    $checkCmdLet = $null
    $checkCmdLet = Get-Command Get-MpComputerStatus -ErrorAction SilentlyContinue
    if ($null -eq $checkCmdLet) {
        Write-Error "Get-MpComputerStatus cmdLet is not available"
        Write-Error "This script only sets Exclusions on Microsoft Defender"
        Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
        exit
    } else {
        $mpStatus = $null
        $mpStatus = Get-MpComputerStatus -ErrorAction SilentlyContinue
        if ($null -eq $mpStatus) {
            Write-Error "We cannot get Microsoft Defender information"
            Write-Error "This script only sets Exclusions on Microsoft Defender"
            Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
            exit
        } else {
            if (-not $mpStatus.AntivirusEnabled ) {
                Write-Warning "Microsoft Defender is not enabled."
                Write-Warning "We will apply the exclusions but they do not take effect until you Enabled Microsoft Defender."
                Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
            }
        }
    }
}

$serverExchangeInstallDirectory = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\Setup -ErrorAction SilentlyContinue

# Check Exchange registry key
if (-not  $serverExchangeInstallDirectory ) {
    Write-Warning "Failed to find the Exchange installation Path registry key"

Review comment (suggestion):
We should change all the Write-Error calls to either Write-Warning or Write-Host -ForegroundColor Red. Write-Error is awkward because it produces multiple lines of output. This means writing one error is noisy and writing two is even worse:

![image](https://github.com/microsoft/CSS-Exchange/assets/4518572/7aa415e6-7864-45ce-8abb-185b3954c7b6)

We should only Write-Error if this is really a fatal error where we're going to need a call stack. line number, etc. For this output, we don't need that. 

Code after:

. $PSScriptRoot\..\..\Shared\Confirm-Administrator.ps1
. $PSScriptRoot\..\..\Shared\Confirm-ExchangeShell.ps1
. $PSScriptRoot\..\..\Shared\Get-ExchAVExclusions.ps1
. $PSScriptRoot\..\..\Diagnostics\AVTester\Write-SimpleLogFile.ps1

# Log file name
$LogFile = "SetExchAvExclusions.log"

# Confirm that we are an administrator
if (-not (Confirm-Administrator)) {
    Write-Error "Please run as Administrator"
    exit
}

if (-not $ListRecommendedExclusions) {
    if ( $($host.Version.Major) -lt 5 -or ( $($host.Version.Major) -eq 5 -and $($host.Version.Minor) -lt 1) ) {
        Write-Error "This version of Windows do not have Microsoft Defender"
        exit
    }

    $checkCmdLet = $null
    $checkCmdLet = Get-Command Get-MpComputerStatus -ErrorAction SilentlyContinue
    if ($null -eq $checkCmdLet) {
        Write-Host "Get-MpComputerStatus cmdLet is not available" -ForegroundColor Red
        Write-Host "This script only sets Exclusions on Microsoft Defender" -ForegroundColor Red
        Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
        exit
    } else {
        $mpStatus = $null
        $mpStatus = Get-MpComputerStatus -ErrorAction SilentlyContinue
        if ($null -eq $mpStatus) {
            Write-Host "We cannot get Microsoft Defender information" -ForegroundColor Red
            Write-Host "This script only sets Exclusions on Microsoft Defender" -ForegroundColor Red
            Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
            exit
        } else {
            if (-not $mpStatus.AntivirusEnabled ) {
                Write-Warning "Microsoft Defender is not enabled."
                Write-Warning "We will apply the exclusions but they do not take effect until you Enabled Microsoft Defender."
                Write-Host "If you have any other Antivirus you can use -ListRecommendedExclusions parameter to get the Recommended Exclusion List"
            }
        }
    }
}

$serverExchangeInstallDirectory = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\ExchangeServer\v15\Setup -ErrorAction SilentlyContinue

# Check Exchange registry key
if (-not  $serverExchangeInstallDirectory ) {
    Write-Warning "Failed to find the Exchange installation Path registry key"