1

I have an advanced rule setup in Windows firewall that I want to be able to turn on and off via an AutoHotkey script.

RunWaitOne(command) {
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec(ComSpec " /C " command)
    return exec.StdOut.ReadAll()
}

!d::
    if WinActive("ahk_exe Hearthstone.exe")
        RunWaitOne("netsh advfirewall set rule name=hearthstone new enable=yes")
        Sleep, 1000  ; 1000ms of delay between disconnecting and reconnecting
        RunWaitOne("netsh advfirewall set rule name=hearthstone new enable=no") 
    return

The rule works fine manually when I turn it on and off via the pannel however when testing the script and the action manually I get an error: The following command was not found: advfirewall set rule name=hearthstone new enable=yes.

This question suggested it was a missing helper however when I ran netsh show helper the .DLL for advfirewall is present so I'm rather at a loss on how to further debug this.

EDIT: Yes I have AHK running with elevated privileges.

2 Answers 2

1
+50

It's not a missing helper - you are executing the whole command-string as the command-name. There is no executable whose name is this entire string "netsh advfirewall set rule...".

I suggest to simplify the script by using the RunWait command instead of ComObjCreate.

Your command may then look like this:

!d::
if WinActive("ahk_exe Hearthstone.exe")
    RunWait, %comspec% /c netsh advfirewall set rule name=hearthstone new enable=yes,,hide
    Sleep, 1000  ; 1000ms of delay between disconnecting and reconnecting
    RunWait, %comspec% /c netsh advfirewall set rule name=hearthstone new enable=no,,hide
return
4
  • Can you be more specific about how this would fit into the code above? Maybe add it to your answer? I tried replacing the top part with your code but nothing happens when I try and execute the script. Commented Jan 15, 2021 at 19:39
  • I've fleshed out the AHK code, replacing the RunWaitOne part. The rest of the code is yours without change.
    – harrymc
    Commented Jan 15, 2021 at 20:10
  • 1
    Unfortunately this is incorrect. Commented Jan 16, 2021 at 23:41
  • What is incorrect here? And why use ComObjCreate when AHK does it for you?
    – harrymc
    Commented Jan 17, 2021 at 8:19
1

The issue wasn't with the need to simplify the script. There was a missing command in the chain.

It was missing a firewall in the RunWait command.

The correct code that works is:

RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
return exec.StdOut.ReadAll()
}

!d::
    if WinActive("ahk_exe Hearthstone.exe")
        RunWaitOne("netsh advfirewall firewall set rule name=hearthstone new enable=yes")
        Sleep, 1000  ; 1000ms of delay between disconnecting and reconnecting
        RunWaitOne("netsh advfirewall firewall set rule name=hearthstone new enable=no") 
    return

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .