Virus Scanner Gives False Positive with tests.ps1 File

I’m trying to run Pester code that I wrote and have run many times over the past year. However, it just started to give me this warning about being caught by the virus scanner. This is all my own code that I have written. Any idea how to fix this?

Can’t really advise on why your code got flagged without actually seeing your code. You could set AV exclusion if you’re confident it’s a false positive.

I began removing the tests to figure out which one might be the problem and figured out it was this one:

It "Just Get Directory Path" {
    Get-HSCDepartmentMapPath -DirectoryOnly |
        Should -BeLike "*$DepartmentMapPath*"
}

The part that I don’t understand now is that I was able to fix it simply by changing a variable name with this code:

It "Just Get Directory Path" {
    Get-HSCDepartmentMapPath -DirectoryOnly |
        Should -BeLike "*$DepartmentMapDirectory*"
}

This same antivirus alert happened to me recently with a script that was working perfectly before and I found out that the responsible for this blocking is something called AMSI (Anti-Malware Scan Interface).

Apparently, AMSI uses “string-based” detection measures to determine if some PowerShell code is malicious or not, so it can be easily bypassed if you just don’t use a banned string literally, as in your case.