Fixed firewall rules check and improved the script
the check shall be like ~ Get-NetFirewallRule -DisplayName eShopOnContainers-* -ErrorAction Stop
This commit is contained in:
parent
84411b067d
commit
6c3ab903ed
@ -1,26 +1,53 @@
|
|||||||
param([switch]$Elevated)
|
param(
|
||||||
|
[string]$Name = "eShopOnContainers",
|
||||||
|
[string]$InboundDisplayName = "eShopOnContainers-Inbound",
|
||||||
|
[string]$OutboundDisplayName = "eShopOnContainers-Outbound",
|
||||||
|
[switch]$Elevated
|
||||||
|
)
|
||||||
|
|
||||||
function Check-Admin {
|
function Check-Admin {
|
||||||
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
|
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
|
||||||
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
|
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
|
||||||
}
|
}
|
||||||
if ((Check-Admin) -eq $false) {
|
function Add-InboundRule {
|
||||||
if ($elevated)
|
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
|
||||||
{
|
|
||||||
# could not elevate, quit
|
|
||||||
}
|
}
|
||||||
|
function Add-OutboundRule {
|
||||||
else {
|
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
|
||||||
|
|
||||||
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
|
|
||||||
}
|
|
||||||
exit
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if ((Check-Admin) -eq $false) {
|
||||||
Get-NetFirewallRule -DisplayName EshopDocker -ErrorAction Stop
|
if ($elevated)
|
||||||
Write-Host "Rule found"
|
{
|
||||||
|
# could not elevate, quit
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
|
||||||
|
}
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
$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
|
||||||
}
|
}
|
||||||
catch [Exception] {
|
|
||||||
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
|
|
||||||
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
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user