Friday 5 March 2010

Enable-FWPort.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script enables then disables the SMTP port on a local system  
  4. .DESCRIPTION 
  5.     This script first creates a FW object, then creates a port. The 
  6.     script then addes that port to the firewall rules. The script 
  7.     finally removes the port. The script also prints before/after 
  8.     results. 
  9. .NOTES 
  10.     File Name  : Enable-FWPort.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/aa366425(VS.85).aspx 
  18. #> 
  19.  
  20. ##  
  21. # Start of Script 
  22. ##  
  23.   
  24. # Define Constants 
  25. $NET_FW_IP_PROTOCOL_UDP = 17 
  26. $NET_FW_IP_PROTOCOL_TCP = 6 
  27. $NET_FW_SCOPE_ALL = 0 
  28.   
  29. # Create FW objecct 
  30. $fwMgr = new-object -com HNetCfg.FwMgr 
  31.   
  32. # Get current profile 
  33. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  34.   
  35. # Display ports open: 
  36. $profile.GloballyOpenPorts | ft name,port,enabled -auto 
  37.   
  38. # Create Port object 
  39. $port = New-Object -com HNetCfg.FWOpenPort 
  40. $port.Name = "SMTP" 
  41. $port.Port = 25 
  42. $port.Protocol = $NET_FW_IP_PROTOCOL_TCP 
  43. $port.Scope = $NET_FW_SCOPE_ALL 
  44.   
  45. # Enable the port 
  46. $port.Enabled = $True 
  47. $profile.GloballyOpenPorts.Add($port
  48.   
  49. # Display results 
  50. $profile.GloballyOpenPorts | ft name,port,enabled -auto 
  51.   
  52. # now remove the port 
  53. $profile.GloballyOpenPorts.remove($port.port,$NET_FW_IP_PROTOCOL_TCP
  54.   
  55. # Display results 
  56. $profile.GloballyOpenPorts | ft name,port,enabled -auto 
  57. # End of script 
Technorati Tags: ,,,

No comments: