Jump to content

Recommended Posts

Posted (edited)

This simple script to toggle Win 7 Firewall state is simply failing.

Can anyone see why? I think I've been looking to long.

#RequireAdmin

$bFWState = _WinFireWallEnabled()

While 1
    $bAction = MsgBox(4, 0, "Firwall Enabled = " & $bFWState & @CRLF & _
            "Change state of firewall? ")
    Switch $bAction
        Case 6
            _WinFireWallEnDEn(Not $bFWState)
            If @error Then
                MsgBox(0,"Error","Change Firewall State Failed" & @CRLF & _
                "Error: " & @error)
                Exit
            EndIf
        Case Else
    EndSwitch
    $bFWState = _WinFireWallEnabled()
WEnd

Func _WinFireWallEnabled()
    $FwMgr = ObjCreate("HNetCfg.FwMgr")
    $err = @error
    $ext = @extended
    If Not IsObj($FwMgr) Then
        Return SetError($err, $ext, -1)
    EndIf
    $profile = $FwMgr.LocalPolicy.CurrentProfile
    If $profile.FirewallEnabled = False Then
        $FwMgr = ""
        Return False
    Else
        $FwMgr = ""
        Return True
    EndIf
EndFunc   ;==>_WinFireWallEnabled

Func _WinFireWallEnDEn($bState)
    $FwMgr = ObjCreate("HNetCfg.FwMgr")
    $err = @error
    $ext = @extended
    If Not IsObj($FwMgr) Then
        Return SetError($err, $ext, -1)
    EndIf
    $profile = $FwMgr.LocalPolicy.CurrentProfile
    ;MsgBox(0,0,$bState)
    $profile.FirewallEnabled = $bState ; The requested action with this object has failed.
    $FwMgr = ""
EndFunc   ;==>_WinFireWallED
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Posted (edited)

JohnOne, it's time to stop wrecking:

#RequireAdmin
; Profile Type
Global Const $NET_FW_PROFILE2_DOMAIN  = 1
Global Const $NET_FW_PROFILE2_PRIVATE = 2
Global Const $NET_FW_PROFILE2_PUBLIC  = 4

$bFWState = _WinFireWallEnabled()

While 1
 Sleep(10)
    $bAction = MsgBox(4, 0, "Firwall Enabled = " & $bFWState & @CRLF & _
            "Change state of firewall? ")
    Switch $bAction
        Case 6
            _WinFireWallEnDEn(Not $bFWState)
            If @error Then
                MsgBox(0,"Error","Change Firewall State Failed" & @CRLF & _
                "Error: " & @error)
                Exit
            EndIf
        Case Else
   Exit
    EndSwitch
    $bFWState = _WinFireWallEnabled()
WEnd

Func _WinFireWallEnabled()
    $FwMgr = ObjCreate("HNetCfg.FwMgr")
    $err = @error
    $ext = @extended
    If Not IsObj($FwMgr) Then
        Return SetError($err, $ext, -1)
    EndIf
    $profile = $FwMgr.LocalPolicy.CurrentProfile
    If $profile.FirewallEnabled = False Then
        $FwMgr = ""
        Return False
    Else
        $FwMgr = ""
        Return True
    EndIf
EndFunc   ;==>_WinFireWallEnabled

Func _WinFireWallEnDEn($bState)
 $FwPol = ObjCreate("HNetCfg.FwPolicy2")
    $err = @error
    $ext = @extended
    If Not IsObj($FwPol) Then
        Return SetError($err, $ext, -1)
    EndIf
 $FwPol.FirewallEnabled($NET_FW_PROFILE2_PUBLIC) = $bState ;change firewall for the public profile
    $FwPol = ""
EndFunc   ;==>_WinFireWallED

Tested on Windows 7/32 bit.

[EDIT] Deleted $a variable (was just for testing)

Edited by taietel
Posted (edited)

I don't know if it's not working for you, but when I press Yes button, the msgbox turns to "Firewall Enabled=False", and a notification popups from the tray saying to enable firewall. If Yes is pressed again, the firewall turns on again.

For reference, see also this.

[EDIT] Replace Func _WinFireWallEnDEn with this (to disable/enable all)

Func _WinFireWallEnDEn($bState)
 Local $FwPolicy2 = ObjCreate("HNetCfg.FwPolicy2")
    If Not IsObj($FwPolicy2) Then Return SetError(@error, @extended, -1)
 $FwPolicy2.FirewallEnabled($NET_FW_PROFILE2_DOMAIN) = $bState ;change firewall for the domain profile
 $FwPolicy2.FirewallEnabled($NET_FW_PROFILE2_PRIVATE) = $bState ;change firewall for the private profile
 $FwPolicy2.FirewallEnabled($NET_FW_PROFILE2_PUBLIC) = $bState ;change firewall for the public profile
 $FwPolicy2 = ""
EndFunc   ;==>_WinFireWallED
Edited by taietel
Posted

Ok your code works with the 2 const, thanks taietel.

$FwPol.FirewallEnabled(2) = $bState

@MPH: None at all, I was just thinking I was doing something wrong with the code, I think it just cannot be done like that in Win 7, but rather odd that it will enable, but not disable.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...