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

Code before:
foreach ($path in $BaseFolders) {
    try {
        # Resolve path only returns a bool so we have to manually throw to catch
        if (!(Resolve-Path -Path $path -ErrorAction SilentlyContinue)) {
            throw "Failed to resolve"
        }
        # If -recurse then we need to find all subfolders and Add them to the list to be tested
        if ($Recurse) {

            # Add the root folder
            $FolderList.Add($path)

            # Get the Folder and all subFolders and just return the fullname value as a string
            Get-ChildItem $path -Recurse -Directory -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName | ForEach-Object { $FolderList.Add($_) }
        }
        # Just Add the root folder
        else { $FolderList.Add($path) }
    } catch { Write-SimpleLogfile -string ("[ERROR] - Failed to resolve folder " + $path) -Name $LogFile }
}

Write-SimpleLogfile -String "Creating EICAR Files" -name $LogFile -OutHost

# Create the EICAR file in each path
foreach ($Folder in $FolderList) {

    [string] $FilePath = (Join-Path $Folder eicar.bat)
    Write-SimpleLogfile -String ("Creating EICAR file " + $FilePath) -name $LogFile

    #Base64 of Eicar string
    [string] $EncodedEicar = 'WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5EQVJELUFOVElWSVJVUy1URVNULUZJTEUhJEgrSCo='

    if (!(Test-Path -Path $FilePath)) {

        # Try writing the encoded string to a the file
        try {
            [byte[]] $EicarBytes = [System.Convert]::FromBase64String($EncodedEicar)
            [string] $Eicar = [System.Text.Encoding]::UTF8.GetString($EicarBytes)
            [IO.File]::WriteAllText($FilePath, $Eicar)
        }

        catch {
            Write-Warning "$Folder Eicar.bat file couldn't be created. Either permissions or AV prevented file creation."
        }
    }

    else {
        Write-SimpleLogfile -string ("[WARNING] - Eicar.bat already exists!: " + $FilePath) -name $LogFile -OutHost
    }
}

# Try to open each EICAR file to force detection

Review comment (suggestion):
Change to File name being a variable. Do this outside of the loop. 

$EicarFileName = "eicar.com" 


Secondly, why are we changing the file to be .bat vs .com? Most articles I have found do .com or .com.txt not .bat.

Code after:
foreach ($path in $BaseFolders) {
    try {
        # Resolve path only returns a bool so we have to manually throw to catch
        if (!(Resolve-Path -Path $path -ErrorAction SilentlyContinue)) {
            throw "Failed to resolve"
        }
        # If -recurse then we need to find all subfolders and Add them to the list to be tested
        if ($Recurse) {

            # Add the root folder
            $FolderList.Add($path)

            # Get the Folder and all subFolders and just return the fullname value as a string
            Get-ChildItem $path -Recurse -Directory -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName | ForEach-Object { $FolderList.Add($_) }
        }
        # Just Add the root folder
        else { $FolderList.Add($path) }
    } catch { Write-SimpleLogfile -string ("[ERROR] - Failed to resolve folder " + $path) -Name $LogFile }
}

Write-SimpleLogfile -String "Creating EICAR Files" -name $LogFile -OutHost

# Create the EICAR file in each path
$EicarFileName = "eicar.bat"

foreach ($Folder in $FolderList) {

    [string] $FilePath = (Join-Path $Folder $EicarFileName)
    Write-SimpleLogfile -String ("Creating EICAR file " + $FilePath) -name $LogFile

    #Base64 of Eicar string
    [string] $EncodedEicar = 'WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5EQVJELUFOVElWSVJVUy1URVNULUZJTEUhJEgrSCo='

    if (!(Test-Path -Path $FilePath)) {

        # Try writing the encoded string to a the file
        try {
            [byte[]] $EicarBytes = [System.Convert]::FromBase64String($EncodedEicar)
            [string] $Eicar = [System.Text.Encoding]::UTF8.GetString($EicarBytes)
            [IO.File]::WriteAllText($FilePath, $Eicar)
        }

        catch {
            Write-Warning "$Folder $EicarFileName file couldn't be created. Either permissions or AV prevented file creation."
        }
    }

    else {
        Write-SimpleLogfile -string ("[WARNING] - $EicarFileName already exists!: " + $FilePath) -name $LogFile -OutHost
    }
}