mercredi 20 octobre 2021

Powershell windows defender is anti virus enabled script when importing from csv if and else statement not working

$servers=Import-Csv C:\Users\Downloads\Defender\Servers.csv
foreach($server in $servers){
$avstatus = get-MpComputerStatus | select *AntiVirusEnabled 
       $avstatus | %{
    if($_.AntivirusEnabled -eq "True"){
        Write-Host "It is $($_.AntivirusEnabled) $server, Anti virus is enabled." -`Foregroundcolor Green #}`
        }Elseif($_.AntivirusEnabled -eq "False") {
        Write-Host "It is $($_.AntivirusEnabled) $server, anti service is stopped." -

Foregroundcolor Red
            }
        };
    Write-Host $server, $avstatus}

This is simple powershell script to check the status of anti virus defender on a list of servers. The output I get is always true, and it seems the if / else part is not working. I am not sure where I am going wrong. The output looks like the below. Servers that are offline are returning anti virus is enabled. Sample output below. It is True @{names=laptop-user; OS=Windows 10}, Anti virus is enabled.

Aucun commentaire:

Enregistrer un commentaire