Forum Discussion
PowerShell Script TCP Ports
Hey, Paul.
Your firewall team seems abnormally out-of-touch since there's very few protocols (comparatively) that use the same source and destination port. Apart from being rare, taking this approach also means that each time you run the script, you run the risk that the chosen source port has since been taken by another process (unless it's an RFC-defined port below 1024 - which would be bad practice conceptually anyway - and you're not running any aligned processes that could otherwise lock you out from using it.)
To answer your question though, you can achieve this but it's not for the feint-of-heart.
You'd have to tap directly into .NET, and the most likely class would be the Socket class (I've linked the Bind method which speaks to your requirement more directly than the overview does):
It's not that it would take a lot of code to achieve (though graceful exception handling could blow it out a bit), it's just not something most people would find particularly easy to navigate.
If you have a look around the PowerShell Gallery, you might find a third-party PowerShell module that does what you want, but since I don't use third-party modules, I can't give you an off-hand reference to one. Perhaps someone else can.
Cheers,
Lain