Powershell:檢查防火牆是否已啟用(而非設定檔)

Powershell:檢查防火牆是否已啟用(而非設定檔)

我對 Windows 10 顯示防火牆狀態的方式感到有點不舒服。我正在嘗試審核我的 Windows 10 和 Server 2016 裝置以取得以下資訊:

  1. Windows 防火牆是否已啟用? [不工作]
  2. 3 個設定檔均已啟用嗎? [在職的]
  3. 是否啟用了第三方防火牆? [在職的]

從這個畫面來看,一切似乎都已啟用並且運作良好: 健康的韌體配置文件

然而,當我上升一級時,這是我看到的訊息(單擊“打開”不會執行任何操作): 由於 BitDefender,WF 被停用

如果我在此處檢查三個設定檔的註冊表項,我可以看到它們都已啟用:"HKLM:\SYSTEM\ControlSet001\Services\SharedAccess\Parameters\FirewallPolicy"但它們實際上並未“啟用”,因為 Windows 防火牆已停用。

這個小片段偵測設備上的第三方防火牆:

$firewalls= @(Get-WmiObject -Namespace $securityCenterNS -class FirewallProduct -ErrorAction Stop)

if($firewalls.Count -eq 0){

    Write-Output "No third party firewall installed."

}else{ 

    $firewalls | Foreach-Object {                       
        [int]$productState=$_.ProductState
        $hexString=[System.Convert]::toString($productState,16).padleft(6,'0')
        $provider=$hexString.substring(0,2)
        $realTimeProtec=$hexString.substring(2,2)
        $definition=$hexString.substring(4,2)

        "Product Name : {0}."     -f $_.displayName
        "Service Type : {0}."     -f $SecurityProvider[[String]$provider]
        "State        : {0}.`n`n" -f $RealTimeBehavior[[String]$realTimeProtec]
    }
}

<# OUTPUT:
Product Name : Bitdefender Firewall
Service Type : AntiVirus
State        : ON
#>

問題: 如何判斷 Windows 防火牆(而不僅僅是其設定檔)是否真正啟用或停用?我需要在註冊表中尋找特定值嗎?下面是否有一個命令列開關可以快速告訴我韌體是否實際上已開啟?

防火牆命令列開關

答案1

Windows 防火牆作為服務安裝到作業系統上。要知道它是全域啟用還是停用,您需要確認其狀態是「正在運行」還是「已停止」。

電源外殼

$FWService = (Get-Service | ?{$_.Name -eq "mpssvc"});
$FWService | %{
    If($_.Status -eq "Running"){
        Write-Host "The $($_.DisplayName) service is running." -Foregroundcolor Green
        }Else{
        Write-Host "The $($_.DisplayName) service is stopped." -Foregroundcolor Red
        }
    };

此外,根據Windows 防火牆設定檔據稱。 。 。

  • Windows 防火牆提供三種防火牆設定檔:網域、專用和公用。網域設定檔適用於主機系統可以向網域控制器進行驗證的網路。專用設定檔是使用者指派的設定文件,用於指定專用或家庭網路。最後,預設設定檔是公共設定文件,用於指定公共網絡,例如咖啡店、機場和其他地點的 Wi-Fi 熱點。

因此,這意味著 Windows 防火牆也可以在這三個設定檔層級停用或啟用,因此要確認它是否在此處啟用或停用,您需要檢查這些設定檔的狀態。

電源外殼

$FWProfiles = (Get-NetFirewallProfile);
Write-Host "Windows Firewall Profile Statuses" -Foregroundcolor Yellow;
$FWProfiles | %{
    If($_.Enabled -eq 1){
        Write-Host "The Windows Firewall $($_.Name) profile is enabled"  -Foregroundcolor Green
        }Else{
        Write-Host "The Windows Firewall $($_.Name) profile is disabled" -Foregroundcolor Red
        } 
    };

更多資源

相關內容