2020-08-07 13:20:34 +02:00
param (
[ string ] $Name = " eShopOnContainers " ,
[ string ] $InboundDisplayName = " eShopOnContainers-Inbound " ,
[ string ] $OutboundDisplayName = " eShopOnContainers-Outbound " ,
[ switch ] $Elevated
)
2016-12-21 11:30:11 +01:00
function Check-Admin {
2020-08-07 13:20:34 +02:00
$currentUser = New-Object Security . Principal . WindowsPrincipal $ ( [ Security.Principal.WindowsIdentity ] :: GetCurrent ( ) )
$currentUser . IsInRole ( [ Security.Principal.WindowsBuiltinRole ] :: Administrator )
2016-12-21 11:30:11 +01:00
}
2020-08-07 13:20:34 +02:00
function Add-InboundRule {
New-NetFirewallRule -DisplayName $InboundDisplayName -Confirm -Description " $Name Inbound Rule for port range 5100-5150 " -LocalAddress Any -LocalPort 5100 - 5150 -Protocol tcp -RemoteAddress Any -RemotePort Any -Direction Inbound
2016-12-21 11:30:11 +01:00
}
2020-08-07 13:20:34 +02:00
function Add-OutboundRule {
New-NetFirewallRule -DisplayName $OutboundDisplayName -Confirm -Description " $Name Outbound Rule for port range 5100-5150 " -LocalAddress Any -LocalPort 5100 - 5150 -Protocol tcp -RemoteAddress Any -RemotePort Any -Direction Outbound
2016-12-21 11:30:11 +01:00
}
2020-08-07 13:20:34 +02:00
if ( ( Check-Admin ) -eq $false ) {
if ( $elevated )
{
# could not elevate, quit
}
else {
Start-Process powershell . exe -Verb RunAs -ArgumentList ( '-noprofile -noexit -file "{0}" -elevated' -f ( $myinvocation . MyCommand . Definition ) )
}
exit
2016-12-21 11:30:11 +01:00
}
2017-01-04 10:43:55 +01:00
2020-08-07 13:20:34 +02:00
2017-01-04 10:43:55 +01:00
try {
2020-08-07 13:20:34 +02:00
$rules = $ ( Get-NetFirewallRule -DisplayName $Name - * -ErrorAction Stop | Out-String )
if ( ! $rules . Contains ( $InboundDisplayName ) -and ! $rules . Contains ( $OutboundDisplayName ) )
{
Add-InboundRule
Add-OutboundRule
}
elseif ( ! $rules . Contains ( $InboundDisplayName ) )
{
Add-InboundRule
}
elseif ( ! $rules . Contains ( $OutboundDisplayName ) )
{
Add-OutboundRule
}
else {
Write-Host " Rules found! "
}
}
catch [ Exception ] {
Add-InboundRule
Add-OutboundRule
2017-01-04 10:43:55 +01:00
}