5

I would like to know how to open a firewall port in Windows by using Power Shell.Could anyone write a script for opening a firewall port.I saw a similar post on https://stackoverflow.com/questions/24760821/changing-windows-firewall-rules-with-powershell-open-close-a-specific-port but couldn't understand how to do it.

I would just want to open a port:8983 in windows because when I execute the application(stack dump) it says pysolr.SolrError: Failed to connect to server at 'http://localhost:8983/solr/stackdump/admin/ping', are you sure that URL is correct?.Atlast it says:No connection could be made because the target machine actively refused it.

15

You can refer to the guide here.

The command to open port 80 is:

netsh advfirewall firewall add rule name="Open Port 80" dir=in action=allow protocol=TCP localport=80

You need to specify:

  • name for the rule
  • direction
  • whether to allow the connection
  • protocol used
  • port number

You can use this command from the Powershell level.

If you absolutely must use Powershell, you can use something like the script below(for the port 80 as well):

#==============================================================
# Creates a rule to open an incomming port in the firewall.
#==============================================================

#$numberAsString = read-host "type an port number"
#$mynumber = [int]$numberAsString


$port1 = New-Object -ComObject HNetCfg.FWOpenPort

$port1.Port = 80

$port1.Name = 'MyTestPort' # name of Port

$port1.Enabled = $true

$fwMgr = New-Object -ComObject HNetCfg.FwMgr

$profiledomain=$fwMgr.LocalPolicy.GetProfileByType(0)

$profiledomain.GloballyOpenPorts.Add($port1)

Taken from here.

  • :Thanks.I tried it and it worked but fortunately it says:The requested operation requires elevation (Run as administrator).I'm using this computer from college library so may be that might be the problem. – justin Nov 20 '14 at 13:31
  • Well, I think You might be right. The college library computers, or pretty much any publicly available computers are known to have limitations implemented that will prohibit any changes to the computer configuration. I'm pretty sure that above scripts will work on normal computers. – mnmnc Nov 20 '14 at 13:39
  • :I would also like to ask can we execute the script on linux?Becuase while I'm running the application XOWA it says that the `connection is refused' in firefox while using an XOWA addon for firefox.Do these type of errors happen because a port is not opened? – justin Nov 20 '14 at 13:45
  • Connection is refused looks more like the response from the destination server or from the network device that is between you and destination server. Linux is a different operating system. Pretty much totally different. You cannot execute above comments in Linux. There are Linux commands what will do the samem but this is entirely different question. – mnmnc Nov 20 '14 at 14:32
  • :Have you heard about XOWA?It's really an application which can allow you to have wikipedia offline.XOWA addon in firefox helps us to run the application from firefox.But still as you said the 'destination server' here that's really my computer itself.So I think the problem would be with the port beause the response should go through a port,isn't it? – justin Nov 21 '14 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.