0

Here is the C# code to return Firewall rules:

Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");

INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);

List<INetFwRule> RuleList = new List<INetFwRule>();

foreach (INetFwRule rule in fwPolicy2.Rules)
{
    RuleList.Add(rule);
}

With Powershell this returns the same:

Get-NetFirewallRule

However Powershell also has the -PolicyStore param. If we use:

Get-NetFirewallRule -PolicyStore RSOP

or

Get-NetFirewallRule -PolicyStore ActiveStore

It will return all the rules enforced by the domain group policy.

Is there something similar that can be done with C#?

Janis S.
  • 2,526
  • 22
  • 32
flux
  • 1,518
  • 1
  • 17
  • 31
  • `Get-NetFirewallRule` uses the `MSFT_NetFirewallRule` CIM class, so you should be able to use that. – Bill_Stewart Nov 03 '17 at 16:36
  • Just tried this with the scope "\\\\.\\ROOT\\StandardCimv2" and query "SELECT * FROM MSFT_NetFirewallRule" but it only gave me the local rules. Is there a path where the group policy rules are stored locally? – flux Nov 06 '17 at 10:10
  • Strange there is a PolicyStoreSource property. But how can MSFT_NetFirewallRule be populated with different policy stores? – flux Nov 06 '17 at 11:32
  • 1
    I haven't used that CIM class; my comment was intended to get you pointed in the right direction. – Bill_Stewart Nov 06 '17 at 13:31
  • You can't. The API does not expose them. – Alexandru Dicu Jul 13 '18 at 12:57
  • Hi flux. Did you solve this problem? I also have this problem. If you solved, please help me – Jack Lee May 09 '20 at 01:18
  • I think I went a different route using netsh to do what I needed to do (long time ago) – flux May 10 '20 at 06:54

0 Answers0