如何通过 C# 检查远程服务器上的 windows 防火墙状态

How can I check windows firewall status on remote server via C#

如何通过 C# 检查 windows 远程服务器上的防火墙状态?

我已经使用 WMI 调用进行了调查,但 Root/SecurityCenter2 仅包含第三方防火墙信息,我还没有找不到 Windows 防火墙信息。

看来搜索注册表可能是下一个选择,但在我走这条路之前,我想看看是否还有其他人有任何其他 ides/suggestions 或代码示例?

我所做的是使用 PSexec 来执行防火墙的命令:

Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = path; //the path of PsExec.exe
        p.StartInfo.Arguments = @"\" + ip + " netsh advfirewall show domain state "; //you can change domain for allprofile in order to look for all profile firewall information
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        Console.WriteLine(output)
        if (output.Contains("ON"))
        {
           "Domain Firewall enabled";
        }else{
           "Domain Firewall disabled";
        }

PsExec : https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx 只需获取 PsExec.exe

string path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\Resources\PsExec.exe";
        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = path;
        p.StartInfo.Arguments = @"\" + ip + " netsh advfirewall show domain state ";
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        if (output.Contains("ON"))
        {
            object[] rowResult = { "0", "Server Info", "Domain Firewall enabled, it should be disabled." };
            resultList.Add(rowResult);
        }
        else
        {
            object[] rowResult = { "1", "Server Info", "Domain Firewall disabled." };
            resultList.Add(rowResult);
        }
        return resultList;