Problem overview
Many organizations struggle to ensure that essential security features, like antivirus and firewall protections, remain consistently enabled and up-to-date on all Windows endpoints. This script addresses that challenge by systematically detecting issues at the system level, allowing IT professionals and MSPs to fix vulnerabilities before they result in security incidents.
<#
This resource is provided as a convenience for Level users. We cannot
guarantee it will work in all environments. Please test before deploying
to your production environment. We welcome contributions to our community
library
Level Library
https://level.io/library/script-windows-monitor-av
#>
# Initialize a variable to track the overall security health
$securityHealthOk = $true
# Function to check the security health status from the Security Center
function Check-SecurityHealth {
try {
# Check Antivirus status
$antivirusProducts = Get-WmiObject -Namespace "ROOT\SecurityCenter2" -Class "AntiVirusProduct"
if ($antivirusProducts) {
foreach ($product in $antivirusProducts) {
Write-Host "Antivirus Name: $($product.displayName)"
# Interpret productState for demonstration; you may need specific checks here
if ($product.productState -match "262144" -or $product.productState -match "266240") {
Write-Host "Antivirus Status: Enabled and up to date"
}
else {
Write-Host "Antivirus Status: Disabled or out of date"
$global:securityHealthOk = $false
}
}
}
else {
Write-Host "No Antivirus product detected."
$global:securityHealthOk = $false
}
# Check Firewall status
$firewallProducts = Get-WmiObject -Namespace "ROOT\SecurityCenter2" -Class "FirewallProduct"
if ($firewallProducts) {
foreach ($product in $firewallProducts) {
Write-Host "Firewall Name: $($product.displayName)"
# Example check; adjust based on actual requirements
if ($product.productState -match "262144") {
Write-Host "Firewall Status: Enabled"
}
else {
Write-Host "Firewall Status: Disabled"
$global:securityHealthOk = $false
}
}
}
else {
Write-Host "No Firewall product detected."
$global:securityHealthOk = $false
}
# Check for other security products as needed...
}
catch {
Write-Host "An error occurred querying the Security Center."
$global:securityHealthOk = $false
}
}
# Execute the security health check
Check-SecurityHealth
# Determine script exit based on overall security health
if ($securityHealthOk) {
Write-Host "SUCCESS: All security features are active and in good standing."
exit 0
}
else {
Write-Host "ERROR: One or more security features are disabled or in a bad state."
exit 1
}
The script queries the Windows Security Center for antivirus and firewall status, verifying if these protections are active and up to date. If an issue is detected—for example, an out-of-date antivirus or a disabled firewall—it sets a flag that triggers a non-zero exit code, ideal for generating alerts in Level. This output can then prompt a remediation workflow to re-enable or update these critical security products. By offering a simple “all-clear” or “problem-found” exit state, the script seamlessly integrates with your broader security monitoring strategy.
Use cases
- Ensuring antivirus and firewall remain consistently enabled
- Automating security checks on remote endpoints
- Triggering an alert or remediation workflow in Level if a vulnerability is detected
- Periodically auditing security status to uphold compliance
Recommendations
- Configure a script-based monitor in Level to run this script regularly, ensuring prompt detection of any disabled or outdated security features
- Pair with an automation remediation workflow that attempts to re-enable antivirus or firewall if issues are discovered
- Test in a non-production environment to ensure the checks align with your antivirus product’s specific productState values
- Adjust productState checks in the script for any custom antivirus or firewall product that might report different codes