JohnOne Posted August 12, 2011 Share Posted August 12, 2011 (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. expandcollapse popup#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 August 13, 2011 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
wakillon Posted August 12, 2011 Share Posted August 12, 2011 Is your windows Firewall service started ? AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
JohnOne Posted August 12, 2011 Author Share Posted August 12, 2011 I think so. I get my messagebox saying True, its just when I try to toggle it, when the error occurs. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
taietel Posted August 12, 2011 Share Posted August 12, 2011 JohnOne, for turning on in win7: netsh advfirewall set currentprofile state onand for turning off: netsh advfirewall set currentprofile state off.More here. Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text Link to comment Share on other sites More sharing options...
JohnOne Posted August 12, 2011 Author Share Posted August 12, 2011 Thanks for that. Still looking for an answer to this though, as its wrecking my head now. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
taietel Posted August 12, 2011 Share Posted August 12, 2011 (edited) JohnOne, it's time to stop wrecking: expandcollapse popup#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 August 12, 2011 by taietel Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text Link to comment Share on other sites More sharing options...
JohnOne Posted August 12, 2011 Author Share Posted August 12, 2011 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 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
taietel Posted August 12, 2011 Share Posted August 12, 2011 (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 August 12, 2011 by taietel Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text Link to comment Share on other sites More sharing options...
Bert Posted August 13, 2011 Share Posted August 13, 2011 John, is there a reason you don't wish to use command line to do this? The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
JohnOne Posted August 13, 2011 Author Share Posted August 13, 2011 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now