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

时间:2015-07-23 18:16:37

标签: c# windows firewall

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

我调查过使用WMI调用,但Root / SecurityCenter2只保存第三方防火墙信息,我一直无法找到Windows防火墙信息。

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

2 个答案:

答案 0 :(得分:0)

我所做的是使用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

答案 1 :(得分:0)

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;