Bonjour,
J'ai fait un script permettant de créer une règle pour le Firewall dans Windows.
celle-ci fonctionne bien mais si le l'exécute plusieurs fois, cette règle se crée plusieurs fois aussi.
Je souhaite donc faire un test avant et ne pas la recréer si elle existe déjà.
mon code de création :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2"); INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2); //on crée la règle INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); inboundRule.Enabled = true; //Allow through firewall inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; inboundRule.Protocol = 17; //TCP = 6, UDP = 17 inboundRule.LocalPorts = "80"; //Port 80 inboundRule.Name = "LeNomDeMaRegle"; //Now add the rule INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); firewallPolicy.Rules.Add(inboundRule);
Partager