Tuesday, 23 March 2010

Get-FirewallStatus.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script gets the status of the host firewall 
  4.     and ensures the firewall IS running! 
  5. .DESCRIPTION 
  6.     This script gets the status and displays it to the 
  7.     console. The script also turns on the firewall if it's 
  8.     currently off. It's a simpler script than in MSDN for VBScript! 
  9. .NOTES 
  10.     File Name  : Get-FirewallStatus.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2010/03/get-firewallstatusps1.html 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/aa366442%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Get-FirewallStatus.ps1 
  20.     Firewall Enabled               : True 
  21.     Firewall Exceptions Not Allowed: False 
  22. #> 
  23.  
  24. ## 
  25. # Start Script 
  26. ## 
  27.   
  28. # Create the firewall manager object. 
  29. $fwMgr = New-Object -com HNetCfg.FwMgr 
  30.   
  31. # Get the current profile for the local firewall policy. 
  32. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  33.   
  34. # Verify that the Firewall is enabled. If it isn't, then enable it. 
  35. if (!$profile.FirewallEnabled)  
  36.     {$profile.FirewallEnabled = $TRUE
  37.  
  38. # Display details 
  39. "Firewall Enabled               : {0}" -f $profile.FirewallEnabled 
  40. "Firewall Exceptions Not Allowed: {0}" -f $profile.ExceptionsNotAllowed 
  41. # End Script 

Monday, 22 March 2010

Get-FWAuthorisedApplications.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays each Firewall Authorised Application 
  4. .DESCRIPTION 
  5.     This script gets the list of authorised applications, then 
  6.     displays them. This is a re-write of a MSDN Script written in 
  7.     VBScript. 
  8. .NOTES 
  9.     File Name  : Get-FWAuthorisedApplications.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/aa366181%28VS.85%29.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-FWAuthorisedApplications.ps1 
  19.     2 Authorised Applications: 
  20.       Name:          : Delivery Manager Service 
  21.       Image Filename : C:\Program Files (x86)\Kontiki\KService.exe 
  22.       IP Version     : ANY 
  23.       Scope          : All subnets 
  24.       RemoteAddresses: * 
  25.       Enabled        : True 
  26.      
  27.       Name:          : BitTorrent 
  28.       Image Filename : C:\Program Files (x86)\BitTorrent\bittorrent.ex 
  29.       IP Version     : ANY 
  30.       Scope          : All subnets 
  31.       RemoteAddresses: * 
  32.       Enabled        : True 
  33. #> 
  34.  
  35. ## 
  36. # Start of script 
  37. ## 
  38.  
  39. # IP Version Constants 
  40. $NET_FW_IP_VERSION_V4 = 0 
  41. $NET_FW_IP_VERSION_V4_NAME = "IPv4" 
  42. $NET_FW_IP_VERSION_V6 = 1 
  43. $NET_FW_IP_VERSION_V6_NAME = "IPv6" 
  44. $NET_FW_IP_VERSION_ANY = 2 
  45. $NET_FW_IP_VERSION_ANY_NAME = "ANY" 
  46.  
  47. # Scope constants 
  48. $NET_FW_SCOPE_ALL = 0 
  49. $NET_FW_SCOPE_ALL_NAME = "All subnets" 
  50. $NET_FW_SCOPE_LOCAL_SUBNET = 1 
  51. $NET_FW_SCOPE_LOCAL_SUBNET_NAME = "Local subnet only" 
  52. $NET_FW_SCOPE_CUSTOM = 2 
  53. $NET_FW_SCOPE_CUSTOM_NAME = "Custom Scope (see RemoteAddresses)" 
  54.  
  55. # Create the firewall manager object 
  56. $fwMgr = new-object -com HNetCfg.FwMgr 
  57.  
  58. # Get the current profile for the local firewall policy 
  59. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  60.  
  61. #Display authorised applications 
  62.  
  63. "{0} Authorised Applications:" -f $profile.AuthorizedApplications.Count 
  64. foreach ($app in $profile.AuthorizedApplications) { 
  65.  
  66.     "  Name:          : {0}" -f $app.Name 
  67.     "  Image Filename : {0}" -f $app.ProcessImageFileName 
  68.  
  69.     switch ($app.IpVersion) { 
  70.         $NET_FW_IP_VERSION_V4  {"  IP Version     : {0}" -f $NET_FW_IP_VERSION_V4_NAME
  71.         $NET_FW_IP_VERSION_V6  {"  IP Version     : {0}" -f $NET_FW_IP_VERSION_V6_NAME
  72.         $NET_FW_IP_VERSION_ANY {"  IP Version     : {0}" -f $NET_FW_IP_VERSION_ANY_NAME
  73.     } 
  74.     switch ($app.Scope) { 
  75.         $NET_FW_SCOPE_ALL          {"  Scope          : {0}" -f $NET_FW_SCOPE_ALL_NAME
  76.         $NET_FW_SCOPE_LOCAL_SUBNET {"  Scope          : {0}" -f $NET_FW_SCOPE_LOCAL_SUBNET_NAME
  77.         $NET_FW_SCOPE_CUSTOM       {"  Scope          : {0}" -f $NET_FW_SCOPE_CUSTOM_NAME
  78.     } 
  79.     "  RemoteAddresses: {0}" -f $app.RemoteAddresses 
  80.     "  Enabled        : {0}" -f $app.Enabled 
  81.     "" 
  82. }  

Sunday, 21 March 2010

Add-FireWallApplication.ps1

  1.    <# 
  2. .SYNOPSIS 
  3.     This script adds a program to the firewall. 
  4. .DESCRIPTION 
  5.     This script used the firewall com object to add 
  6.     a new application to the firewall.   
  7. .NOTES 
  8.     File Name  : Add-FirewallApplication.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/add-firewallapplicationps1.html
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/aa366421%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     At start of script, authorised applications are: 
  18.  
  19.     Name                            Enabled 
  20.     ----                            ------- 
  21.     Delivery Manager Service           True 
  22.     BitTornado                         True 
  23.     driver                             True 
  24.     driver                             True 
  25.     BitTorrent                         True 
  26.     DNA                                True 
  27.     Microsoft Office OneNote           True 
  28.  
  29.     After adding Notepad - here are authorised applications 
  30.  
  31.    Name                            Enabled 
  32.    ----                            ------- 
  33.     Notepad                           True 
  34.     Delivery Manager Service          True 
  35.     BitTornado                        True 
  36.     driver                            True 
  37.     driver                            True 
  38.     BitTorrent                        True 
  39.     DNA                               True 
  40.     Microsoft Office OneNote          True 
  41. #> 
  42.  
  43. ## 
  44. # Start of script 
  45. ## 
  46.  
  47. # Set constants 
  48. $NET_FW_PROFILE_DOMAIN = 0 
  49. $NET_FW_PROFILE_STANDARD = 1 
  50.  
  51. # Scope 
  52. $NET_FW_SCOPE_ALL = 0 
  53.  
  54. # IP Version - ANY is the only allowable setting for now 
  55. $NET_FW_IP_VERSION_ANY = 2 
  56.  
  57. # Create the firewall manager object. 
  58. $fwMgr = new-object -com HNetCfg.FwMgr 
  59.  
  60. # Get the current profile for the local firewall policy. 
  61. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  62.  
  63. # Display applications available 
  64. "At start of script, authorised applications are:" 
  65. $profile.AuthorizedApplications | ft name, enabled -AutoSize 
  66.  
  67. # Create application to add to firewall 
  68. $app = New-Object -com HNetCfg.FwAuthorizedApplication 
  69. $app.ProcessImageFileName = "C:\windows\notepad.exe" 
  70. $app.Name = "Notepad" 
  71. $app.Scope = $NET_FW_SCOPE_ALL 
  72.  
  73. # Use either Scope or RemoteAddresses, but not both 
  74. # $app.RemoteAddresses = "*" 
  75. $app.IpVersion = $NET_FW_IP_VERSION_ANY 
  76. $app.Enabled = $TRUE 
  77.  
  78. # Use this line if you want to add the app, but disabled. 
  79. # $app.Enabled = FALSE 
  80. $profile.AuthorizedApplications.Add($app
  81.  
  82. # Show applications after addition 
  83. "After adding Notepad - here are authorised applications" 
  84. $profile.AuthorizedApplications | ft name, enabled -AutoSize 
  85. # End of script 

Saturday, 20 March 2010

Get-OutlookStores.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses the Outlook COM object to  
  4.     display the data stores in the current profile 
  5. .DESCRIPTION 
  6.     This script creates an Outlook object, displays 
  7.     user information, and the stores currently 
  8.     attached to the profile. 
  9. .NOTES 
  10.     File Name  : Get-OutlookStores.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. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-OutlookStores.ps1' 
  18.     Current profile has the following configured accounts: 
  19.  
  20.     Account Type           User Name        SMTP Address 
  21.     ------------           ---------        ------------ 
  22.     Microsoft Exchange     Thomas.Lee       Thomas.Lee@cookham.net 
  23.     thomas_lee@hotmail.com Thomas Lee (MSN) thomas_lee@hotmail.com 
  24.  
  25.     Exchange Offile Folder Store: 
  26.     C:\Users\tfl\AppData\Local\Microsoft\Outlook\outlook0.ost 
  27.     
  28.     PST Files 
  29.     Display Name    File Path  
  30.     ------------    --------- 
  31.     Archive Folders C:\Users\tfl\AppData\Local\Microsoft\Outlook\archive.pst 
  32. #> 
  33.  
  34. ## 
  35. # Begin Script 
  36. ## 
  37.  
  38. # Create Outlook object 
  39. $Outlook = New-Object -ComObject Outlook.Application 
  40. $stores = $Outlook.Session.Stores 
  41. $accounts = $outlook.session.accounts 
  42.  
  43. # Basic information 
  44. "Current profile has the following configured accounts:" 
  45. $dn = @{label = "Account Type"; expression={$_.displayname}} 
  46. $un = @{label = "User Name"; expression = {$_.username}} 
  47. $sm = @{label = "SMTP Address"; expression = {$_.smtpaddress}} 
  48. $accounts | Format-Table -AutoSize $dn,$un,$sm 
  49. # Check number of stores > 0 
  50. if ($stores.Count -le 0) {"No stores found"; return
  51.  
  52. # Outlook Off-Line folder store 
  53. $ost = $stores | where{$_.filepath -match ".ost$"
  54. if (!$ost
  55.    "No Outlook Offline Folder store found" 
  56. else 
  57.    "Exchange Offile Folder Store:" 
  58.    $ost | ft filepath -HideTableHeaders 
  59.   } 
  60.  
  61. # PST Files 
  62. $pst = $stores | where {$_.filepath -match ".pst$"
  63. if (!$pst
  64.   { 
  65.     "No PST files found" 
  66.   } 
  67. else 
  68.   { 
  69.     "PST Files" 
  70.     $dn = @{label = "Display Name"; expression={$_.displayname}} 
  71.     $fn = @{label = "File Path"; expression={$_.filepath}} 
  72.     $pst | ft $dn,$fn        
  73.   } 
  74. # End Script

Thursday, 18 March 2010

Enable-ICMP.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script Enables ICMP on the Standard Firewall profile. 
  4. .DESCRIPTION 
  5.     This script creates a Firewall object then configures it. 
  6. .NOTES 
  7.     File Name  : Enable-ICMP.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN Sample posted at: 
  14.         http:// 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\COM\HNetCfg.FwMgr\Enable-ICMP.ps1' 
  17.  
  18.     AllowOutboundDestinationUnreachable : False 
  19.     AllowRedirect                       : False 
  20.     AllowInboundEchoRequest             : False 
  21.     AllowOutboundTimeExceeded           : False 
  22.     AllowOutboundParameterProblem       : False 
  23.     AllowOutboundSourceQuench           : False 
  24.     AllowInboundRouterRequest           : False 
  25.     AllowInboundTimestampRequest        : False 
  26.     AllowInboundMaskRequest             : False 
  27.     AllowOutboundPacketTooBig           : True 
  28.  
  29.     After Script ran: 
  30.     AllowOutboundDestinationUnreachable : False 
  31.     AllowRedirect                       : False 
  32.     AllowInboundEchoRequest             : True 
  33.     AllowOutboundTimeExceeded           : False 
  34.     AllowOutboundParameterProblem       : False 
  35.     AllowOutboundSourceQuench           : False 
  36.     AllowInboundRouterRequest           : False 
  37.     AllowInboundTimestampRequest        : False 
  38.     AllowInboundMaskRequest             : False 
  39.     AllowOutboundPacketTooBig           : True 
  40. #> 
  41.  
  42. ## 
  43. # Start of script 
  44. ## 
  45.  
  46. # Set strict mode 
  47. Set-StrictMode -Version 2.0 
  48.  
  49. # Set Constants 
  50. $NET_FW_PROFILE_DOMAIN   = 0 
  51. $NET_FW_PROFILE_STANDARD = 1 
  52.  
  53. # Create the firewall manager object. 
  54. $fwMgr = New-Object -com HNetCfg.FwMgr 
  55.  
  56. # Get the current profile for the local firewall policy. 
  57. $profile = $fwMgr.LocalPolicy.GetProfileByType($NET_FW_PROFILE_STANDARD
  58.  
  59. # Display current ICMP settings 
  60. $Profile.IcmpSettings 
  61.  
  62. # Now set it to True 
  63. $profile.IcmpSettings.AllowInboundEchoRequest = $True 
  64.  
  65. # Use this line if you want to disable the setting. 
  66. #profile.IcmpSettings.AllowInboundEchoRequest = $FALSE 
  67.  
  68. # Display it again 
  69. "After Script ran: " 
  70. $Profile.IcmpSettings 
  71.  
  72. # End Script 

New-ExcelWorkbook.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates an Excel workbook using PowerShell 
  4. .DESCRIPTION 
  5.     This script demonstrates manipulating Excell with PowerShell 
  6.     and the Excel.Application COM object. 
  7. .NOTES 
  8.     File Name  : New-ExcelWorkbook.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/new-excelworkbookps1.html
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/bb211359.aspx  
  16. .EXAMPLE
  17.     Run it and see one! 
  18. #> 
  19. ##  
  20. # Start of Script 
  21. ## 
  22. # Then we create and save a sample worksheet 
  23. # Create Excel object 
  24. $excel = new-object -comobject Excel.Application 
  25.     
  26. # Make Excel visible 
  27. $excel.visible = $true 
  28.    
  29. # Create a new workbook 
  30. $workbook = $excel.workbooks.add() 
  31.  
  32. # The default workbook has three sheets, remove 2 
  33. $S2 = $workbook.sheets | where {$_.name -eq "Sheet2"
  34. $s3 = $workbook.sheets | where {$_.name -eq "Sheet3"
  35. $s2.delete() 
  36. $s3.delete() 
  37. # Get sheet and update sheet name 
  38. $s1 = $workbook.sheets | where {$_.name -eq 'Sheet1'
  39. $s1.name = "PowerShell Sample" 
  40.    
  41. # Update workook properties 
  42. $workbook.author = "Thomas Lee - tfl@psp.co.uk" 
  43. $workbook.title = "Excel and PowerShell rock!" 
  44. $workbook.subject = "Demonstrating the Power of PowerShell with Excel" 
  45.    
  46. # Next update some cells in the worksheet 'PowerShell Sample' 
  47. $s1.range("A1:A1").cells="Cell a1" 
  48. $s1.range("A2:A2").cells="A2" 
  49. $s1.range("b1:b1").cells="Cell B1" 
  50. $s1.range("b2:b2").cells="b2" 
  51.  
  52. # now make a sum 
  53. $s1.range("E1:E2").cells="Widgets" 
  54. $s1.range("E2:E2").cells=2 
  55. $s1.range("E3:E3").cells=2 
  56. $s1.range("E4:E4").cells=38 
  57. $s1.range("D5:D5").cells="Total" 
  58. $s1.range("E5:E5").cells.formula = "=sum(e2,E4)" 
  59.    
  60. # And save it away: 
  61. $s1.saveas("c:\foo\xlsx3.xlsx"
  62. # end of script 

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 

Tuesday, 16 March 2010

Set-ProgrammerAlias.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a function to set aliases for all Cmdlets which omit the '-' 
  4. .DESCRIPTION 
  5.     This script defines a function which uses Get-Command to find all
  6.     cmdlets. For each of them, it then creates an alias which omits
  7.     the "-". This function  was oritinally written by Jeffrey Snover for
  8.     Monad back in the day, but I updated it slightly for PowerShell V2. 
  9.     Updated after a comment to be a tad shorter. 
  10. .NOTES 
  11.     File Name  : Set-ProgrammerAlias.ps1 
  12.     Author     : Jeffrey Snover - jsnover@microsoft.com 
  13.     Updated by : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell Version 2.0 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18. #> 
  19. ## 
  20. # Start of Script 
  21.  
  22. function Set-ProgrammerAlias { 
  23.  get-command -Com Cmdlet | % {set-alias $($_.Verb + $_.Noun) $_.Name}  
Technorati Tags: ,,,

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: ,,,