none
Using Powershell to Open Ports on on a Windows 2008 Server RRS feed

  • Question

  • Hello,

    Was wondering if there was any good sample code or some advice out there on how to open Firewall Ports on a 2008 Server. I had done some coding using C# but the framework was not compatible. It was recommended that Powershell could do this quite easily.

    Sample of C# code I am now trying to convert to powershell:

    INetFwOpenPort port;

    Port.port = "PortNumber";

    Port.Name = "PortName";

    Port.enabled = True;

    // Then add the GloballyOpen Ports Collection

    ports.Add(port);

    Thank You!

     

    Friday, February 4, 2011 4:52 PM

Answers

  • Try something along these lines. I don't have a server to play with at the moment, but this worked on Win7.

    $port = New-Object -ComObject HNetCfg.FWOpenPort
    $port.Port = 9999
    $port.Name = 'MyTestPort'
    $port.Enabled = $true
    
    $fwMgr = New-Object -ComObject HNetCfg.FwMgr
    $profile = $fwMgr.LocalPolicy.CurrentProfile
    $profile.GloballyOpenPorts.Add($port)
    

    • Proposed as answer by Thiyagu14Moderator Monday, February 7, 2011 4:18 PM
    • Marked as answer by IamMred Monday, February 7, 2011 6:59 PM
    Saturday, February 5, 2011 9:34 PM

All replies

  • Try something along these lines. I don't have a server to play with at the moment, but this worked on Win7.

    $port = New-Object -ComObject HNetCfg.FWOpenPort
    $port.Port = 9999
    $port.Name = 'MyTestPort'
    $port.Enabled = $true
    
    $fwMgr = New-Object -ComObject HNetCfg.FwMgr
    $profile = $fwMgr.LocalPolicy.CurrentProfile
    $profile.GloballyOpenPorts.Add($port)
    

    • Proposed as answer by Thiyagu14Moderator Monday, February 7, 2011 4:18 PM
    • Marked as answer by IamMred Monday, February 7, 2011 6:59 PM
    Saturday, February 5, 2011 9:34 PM
  • Great! Thank You so much for the reply.

    New to PowerShell so I will break out a book.

    Look up the Syntax needed and try this script.

    I appreciate it.


    Todd Wozny
    Monday, February 7, 2011 3:51 PM
  • Hi,

    I found that the port that is generated using your script is Public (Profile),

    How can I set the profile to "All" (Public, Private and Domain)?

    Regards,

    Bader

    Wednesday, June 20, 2012 11:19 AM
  • $profile=$fwMgr.LocalPolicy.GetProfileByType(0)

    $profile=$fwMgr.LocalPolicy.GetProfileByType(1)

    $profile=$fwMgr.LocalPolicy.GetProfileByType(2)


    ¯\_(ツ)_/¯

    Wednesday, June 20, 2012 12:29 PM