Windows Firewall Script

Posted on 2007-10-16
Last Modified: 2010-04-16

Do somebody has a script or could tell me how I could script the WIndows Firewall to do only shut it down on LAN connections but let it switched on on RAS and Wireless ones ?

Question by:Vorenus
    LVL 26

    Accepted Solution

    Greetings Vorenus,
    You can use following statement to Enable or Disable Firewall on specific interface from command line.

    Click Start -> Run -> Cmd.exe -> OK

    NetSh FireWall Set OpMode Mode=ENABLE/ DISABLE Interface=<InterfaceNameHere>

    :: You can make batch file like following to Enable and Disable Firewall for particular Interface .
    NetSh FireWall Set OpMode Mode=ENABLE Interface=WAN
    NetSh FireWall Set OpMode Mode=DISABLE Interface=LAN
    EXIT /B /0

    Hope this helps!
    LVL 4

    Author Comment

    Hi Farhan,

    Thanks : it is a nice command.
    That said, what I am ideally after is something even more generic, which means without knowing the name the of Interface, which may change from Windows' language or setups.

    Ideally, it would even work for devices that the system do not know yet when the script is ran...
    For example, if someday we plug an USB Ethernet Adapter, I would like the firewall to not be active on this device, that has never been plugged before.
    In short, I would like the firewall to be always shutdown for LAN devices, currently plugged or plugged in the future.

    I hope I make sense.
    Thanks in advance.
    LVL 26

    Expert Comment

    Know interface name is not a problem like if you goto to the command line paste following statement it will give you all interface names.

    FOR /F "skip=2 tokens=4" %i IN ('NetSh Interface show interface') DO @ECHO %i

    But the problem is how would system know which interface is a WAN interface??? One way it check default gateway or of all interface or to check routing table!! but it will become too complicated :)
    LVL 40

    Expert Comment

    I use a script to turn off and on my firewalls..  easy to do..

    But, I have yet to find a way to have a script 'know' the difference between LAN and WAN, or any other interface...  just don't think there is a way to do it..

    I even have a small app that I wrote in .NET that installs a toggle on / off for the firewall..  you can have it to by downloading it from my website..

    ============Copy / Paste============

    Set objFirewall = CreateObject("HNetCfg.FwMgr")
    Set objPolicy = objFirewall.LocalPolicy.CurrentProfile

    objPolicy.FirewallEnabled = True
    ============Copy / Paste============
    Set objFirewall = CreateObject("HNetCfg.FwMgr")
    Set objPolicy = objFirewall.LocalPolicy.CurrentProfile

    objPolicy.FirewallEnabled = False

    ============Copy / Paste============


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
    We recently endured a series of broadcast storms that caused our ISP to shut us down for brief periods of time. After going through a multitude of tests, we determined that the issue was related to Intel NIC drivers on some new HP desktop computers …
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now