You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.6 KiB

  1. param(
  2. [string]$Name = "eShopOnContainers",
  3. [string]$InboundDisplayName = "eShopOnContainers-Inbound",
  4. [string]$OutboundDisplayName = "eShopOnContainers-Outbound",
  5. [switch]$Elevated
  6. )
  7. function Check-Admin {
  8. $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  9. $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
  10. }
  11. function Add-InboundRule {
  12. New-NetFirewallRule -DisplayName $InboundDisplayName -Confirm -Description "$Name Inbound Rule for port range 5100-5205" -LocalAddress Any -LocalPort 5100-5205 -Protocol tcp -RemoteAddress Any -RemotePort Any -Direction Inbound
  13. }
  14. function Add-OutboundRule {
  15. New-NetFirewallRule -DisplayName $OutboundDisplayName -Confirm -Description "$Name Outbound Rule for port range 5100-5205" -LocalAddress Any -LocalPort 5100-5205 -Protocol tcp -RemoteAddress Any -RemotePort Any -Direction Outbound
  16. }
  17. if ((Check-Admin) -eq $false) {
  18. if ($elevated)
  19. {
  20. # could not elevate, quit
  21. }
  22. else {
  23. Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
  24. }
  25. exit
  26. }
  27. try {
  28. $rules = $(Get-NetFirewallRule -DisplayName $Name-* -ErrorAction Stop | Out-String)
  29. if (!$rules.Contains($InboundDisplayName) -and !$rules.Contains($OutboundDisplayName))
  30. {
  31. Add-InboundRule
  32. Add-OutboundRule
  33. }
  34. elseif (!$rules.Contains($InboundDisplayName))
  35. {
  36. Add-InboundRule
  37. }
  38. elseif (!$rules.Contains($OutboundDisplayName))
  39. {
  40. Add-OutboundRule
  41. }
  42. else{
  43. Write-Host "Rules found!"
  44. }
  45. }
  46. catch [Exception] {
  47. Add-InboundRule
  48. Add-OutboundRule
  49. }