Wednesday, 17 March 2010

Enable-FirewallPort2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a rule in the Windows Host Firewall. 
  4. .DESCRIPTION 
  5.     This script creates a new firewall rule for 
  6.     port 80 over tcp (i.e. 80). 
  7. .NOTES 
  8.     File Name  : Enable-FirewallPort.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/03/enable-firewallport2ps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/aa366423%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Enable-FirewallPort.ps1
  18.     Before Script Runs: 
  19.  
  20.     Name   IpVersion Protocol Port Scope RemoteAddresses Enabled 
  21.     ----   --------- -------- ---- ----- --------------- ------- 
  22.     HTTPS          2        6  443     0 *                  True 
  23.     driver         2        6 8085     0 *                  True 
  24.     driver         2        6 8085     0 *                  True 
  25.   
  26.  
  27.     After Script Runs: 
  28.  
  29.     Name   IpVersion Protocol Port Scope RemoteAddresses Enabled 
  30.     ----   --------- -------- ---- ----- --------------- ------- 
  31.     HTTP           2        6   80     0 *                  True 
  32.     HTTPS          2        6  443     0 *                  True 
  33.     driver         2        6 8085     0 *                  True 
  34.     driver         2        6 8085     0 *                  True 
  35.  
  36. #> 
  37.  
  38. ## 
  39. # Start Script 
  40. ## 
  41.  
  42. # Set Strict Mode  
  43. Set-Strictmode -Version 2.0 
  44. # Set Constants 
  45. $NET_FW_IP_PROTOCOL_UDP = 17 
  46. $NET_FW_IP_PROTOCOL_TCP = 6 
  47.  
  48. # Create the firewall manager object. 
  49. $fwMgr = New-Object -COM HNetCfg.FwMgr 
  50.  
  51. # Get the current profile for the local firewall policy. 
  52. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  53.  
  54. # Display it 
  55. "Before Script Runs:" 
  56. $profile.GloballyOpenPorts | ` 
  57. ft name, ipversion, protocol, port, scope, remoteaddresses, enabled -auto 
  58.  
  59. # Now add Port 80 
  60.  
  61. $port = New-Object -COM HNetCfg.FWOpenPort 
  62. $port.Name = "HTTP" 
  63. $port.Protocol = $NET_FW_IP_PROTOCOL_TCP 
  64. $port.Port = 80 
  65.  
  66. # If using RemoteAddresses, don't use Scope 
  67. # "*" means Scope of Any. Other entries are ignored if this is specified. 
  68. # "LocalSubnet" means Scope of Local Subnet. Can be used with other addresses as well.  
  69. $port.RemoteAddresses = "*" 
  70.  
  71. # Use this line to scope the port to Local Subnet only 
  72. #$port.RemoteAddresses = "LocalSubnet" 
  73.  
  74. #Use this line to scope the port to the specific IP 10.1.1.1, the specific subnet 12.5.0.0, and Local Subnet. Don't put spaces. 
  75. #port.RemoteAddresses = "LocalSubnet,10.1.1.1/255.255.255.255,12.5.0.0/255.255.0.0" 
  76.  
  77. $port.Enabled = $TRUE 
  78.  
  79. #Use this line instead if you want to add the port, but disabled 
  80. #port.Enabled = FALSE 
  81.  
  82. # Now add the port 
  83. $profile.GloballyOpenPorts.Add($port
  84.  
  85. # Print Results 
  86. " After Script Runs:" 
  87. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  88. $profile.GloballyOpenPorts | ` 
  89. ft name, ipversion, protocol, port, scope, remoteaddresses, enabled -auto 
  90. # End of script 

No comments: