Jump to content
Sign in to follow this  
JohnOne

Simple script Failing [Solved]

Recommended Posts

JohnOne

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.

Share this post


Link to post
Share on other sites
wakillon

Is your windows Firewall service started ?


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
taietel

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

Share this post


Link to post
Share on other sites
JohnOne

Thanks for looking at this taietel.

I'm not certain what that is doing but it is not turning the firewall off for me: Win 7 32

I have noticed that the script I posted will enable the firewall if it is off, but will not disable it if its on :mellow:


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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
taietel

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

Share this post


Link to post
Share on other sites
JohnOne

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.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×