Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit c8e8073

Browse files
author
Sumit Ghosh
authored
Merge pull request #1390 from EdmondShtogu/patch-2
Fixed firewall rules check and improved the script
2 parents 844348c + 6c3ab90 commit c8e8073

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

deploy/windows/add-firewall-rules-for-sts-auth-thru-docker.ps1

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
1-
param([switch]$Elevated)
1+
param(
2+
[string]$Name = "eShopOnContainers",
3+
[string]$InboundDisplayName = "eShopOnContainers-Inbound",
4+
[string]$OutboundDisplayName = "eShopOnContainers-Outbound",
5+
[switch]$Elevated
6+
)
7+
28
function Check-Admin {
3-
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
4-
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
9+
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
10+
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
511
}
6-
if ((Check-Admin) -eq $false) {
7-
if ($elevated)
8-
{
9-
# could not elevate, quit
12+
function Add-InboundRule {
13+
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
1014
}
11-
12-
else {
13-
14-
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
15+
function Add-OutboundRule {
16+
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
1517
}
16-
exit
18+
19+
if ((Check-Admin) -eq $false) {
20+
if ($elevated)
21+
{
22+
# could not elevate, quit
23+
}
24+
else {
25+
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
26+
}
27+
exit
1728
}
1829

30+
1931
try {
20-
Get-NetFirewallRule -DisplayName EshopDocker -ErrorAction Stop
21-
Write-Host "Rule found"
32+
$rules = $(Get-NetFirewallRule -DisplayName $Name-* -ErrorAction Stop | Out-String)
33+
if (!$rules.Contains($InboundDisplayName) -and !$rules.Contains($OutboundDisplayName))
34+
{
35+
Add-InboundRule
36+
Add-OutboundRule
37+
}
38+
elseif (!$rules.Contains($InboundDisplayName))
39+
{
40+
Add-InboundRule
41+
}
42+
elseif (!$rules.Contains($OutboundDisplayName))
43+
{
44+
Add-OutboundRule
45+
}
46+
else{
47+
Write-Host "Rules found!"
48+
}
49+
}
50+
catch [Exception] {
51+
Add-InboundRule
52+
Add-OutboundRule
2253
}
23-
catch [Exception] {
24-
New-NetFirewallRule -DisplayName eShopOnContainers-Inbound -Confirm -Description "eShopOnContainers Inbound Rule for port range 5100-5150" -LocalAddress Any -LocalPort 5100-5150 -Protocol tcp -RemoteAddress Any -RemotePort Any -Direction Inbound
25-
New-NetFirewallRule -DisplayName eShopOnContainers-Outbound -Confirm -Description "eShopOnContainers Outbound Rule for port range 5100-5150" -LocalAddress Any -LocalPort 5100-5150 -Protocol tcp -RemoteAddress Any -RemotePort Any -Direction Outbound
26-
}

0 commit comments

Comments
 (0)