Skip to content

Commit

Permalink
Added new docs and new add-functions for pipeline and progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
darksidemilk committed Aug 3, 2020
1 parent a710225 commit 7d0edbb
Show file tree
Hide file tree
Showing 16 changed files with 685 additions and 80 deletions.
21 changes: 13 additions & 8 deletions EzFirewallMgmt/EzFirewallMgmt.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'EzFirewallMgmt.psm1'

# Version number of this module.
ModuleVersion = '1.0.40.2'
ModuleVersion = '1.0.47.1'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand All @@ -33,20 +33,20 @@ Copyright = '2020'
Description = 'Simplified helper functions to block and unblock ports and programs
Utilizes New-NetFirewallRule and Remove-NetFirewallRule functions and turns them into easier versions'

# Minimum version of the Windows PowerShell engine required by this module
# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '3.0'

# Name of the Windows PowerShell host required by this module
# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# CLRVersion = ''
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
Expand All @@ -70,9 +70,10 @@ PowerShellVersion = '3.0'
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Block-Port', 'Block-Program', 'Get-ExePaths', 'Get-PortRuleName',
'Get-ProgramRuleName', 'Remove-PortRule', 'Remove-ProgramRule',
'Unblock-Port', 'Unblock-Program'
FunctionsToExport = 'Add-PortRule', 'Add-ProgramRule', 'Block-Port', 'Block-Program',
'Get-ExePaths', 'Get-PortRuleName', 'Get-ProgramRuleName',
'Remove-PortRule', 'Remove-ProgramRule', 'Unblock-Port',
'Unblock-Program'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down Expand Up @@ -111,6 +112,10 @@ PrivateData = @{

# ReleaseNotes of this module
ReleaseNotes = '
# 1.0.47.1
added progress bars and modular functions for adding rules to make code a bit cleaner
# 1.0.40.2
Made it so pipeline variables can be used for paths with examples of filtered get-exepaths output being piped in to show how you could select exes for a program name
Expand Down
93 changes: 93 additions & 0 deletions EzFirewallMgmt/Public/Add-PortRule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
function Add-PortRule {
<#
.SYNOPSIS
Adds a port rule for blocking/unblockin
.DESCRIPTION
Mainly meant to be an internal function to avoid repeated code
.PARAMETER port
The port or ports to control
.PARAMETER protocol
TCP,UDP, or BOTH
.PARAMETER type
Block or Unblock
.LINK
Add-PortRule
.LINK
Block-PortRule
.LINK
Unblock-PortRule
.LINK
New-NetfirewallRule
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]
[string[]]$port,
[Parameter()]
[ValidateSet("TCP","UDP","BOTH")]
[string]$protocol,
[Parameter()]
[ValidateSet("Block","Unblock")]
[string]$type
)

begin {
$newRules = New-Object System.Collections.Generic.List[object];
$i = 1;
if ($type -eq "Unblock") {
$action = "Allow";
} else {
$action = "Block";
}
switch ($protocol) {
BOTH {
$count = 4;
}
Default {
$count = 2;
}
}
}

process {
if ($protocol -eq "BOTH" -OR $protocol -eq "TCP") {
$TCPRule = Get-PortRuleName -type $type -port $port -protocol "TCP";
if ($null -eq (Get-NetFirewallRule -Name "$TCPRule*") ) {
Write-Progress -Activity "Creating Port Rules" -id 1 -Status "$i of $count" -CurrentOperation "Creating $TCPRule inBound Rule";$i++;
$newRules.add((New-NetFirewallRule -DisplayName "$TCPRule inbound" -Name "$TCPRule inbound" -Action $action -Profile Any -Direction Inbound -Protocol TCP -LocalPort $port -EA 0))

Write-Progress -Activity "Creating Port Rules" -id 1 -Status "$i of $count" -CurrentOperation "Creating $TCPRule outbound Rule";$i++;
$newRules.add((New-NetFirewallRule -DisplayName "$TCPRule outbound" -Name "$TCPRule outbound" -Action $action -Profile Any -Direction Outbound -Protocol TCP -LocalPort $port -EA 0))
} else {
"$TCPRule already exists" | Out-Host; $i+=2;
}
}
if ($protocol -eq "BOTH" -OR $protocol -eq "UDP") {
$UDPRule = Get-PortRuleName -type $type -port $port -protocol "UDP";
if ($null -eq (Get-NetFirewallRule -Name "$UDPRule*") ) {
Write-Progress -Activity "Creating Port Rules" -id 1 -Status "$i of $count" -CurrentOperation "Creating $UDPRule inbound Rule";$i++;
$newRules.add((New-NetFirewallRule -DisplayName "$UDPRule inbound" -Name "$UDPRule inbound" -Action $action -Profile Any -Direction Inbound -Protocol UDP -LocalPort $port -EA 0))

Write-Progress -Activity "Creating Port Rules" -id 1 -Status "$i of $count" -CurrentOperation "Creating $UDPRule outbound Rule";$i++;
$newRules.add((New-NetFirewallRule -DisplayName "$UDPRule outbound" -Name "$UDPRule outbound" -Action $action -Profile Any -Direction Outbound -Protocol UDP -LocalPort $port -EA 0))
} else {
"$UDPRule already exists" | Out-Host; $i+=2;
}
}
Write-Progress -Activity "Creating Port Rules" -id 1 -Status "$i of $count" -Completed

}

end {
return $newRules;
}
}
90 changes: 90 additions & 0 deletions EzFirewallMgmt/Public/Add-ProgramRule.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
function Add-ProgramRule {
<#
.SYNOPSIS
Adds program rules via pipe
.DESCRIPTION
Runs through the list of paths and makes the rules
Mainly meant for internal use to avoid repeated code
.PARAMETER paths
The list of paths generated during Block/Unblock-Program
.PARAMETER type
Block or Unblock, used to determin rule name and rule action
.EXAMPLE
$paths | Add-ProgramRule
.LINK
Add-ProgramRule
.LINK
Block-Program
.LINK
Unblock-Program
.LINK
Remove-ProgramRule
.LINK
Get-ProgramRulename
.LINK
New-NetfirewallRule
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
$paths,
[Parameter()]
[ValidateSet("Block","Unblock")]
[string]$type,
$count
)

begin {
$newRules = New-Object System.Collections.Generic.List[object];
$i = 1;
# $count = $paths.count;
}

process {
Write-Debug "paths list is $($paths | out-string)";
if ($type -eq "Unblock") {
$action = "Allow";
} else {
$action = "Block";
}
$paths | Foreach-Object {
$ProgramRule = Get-ProgramRuleName -type $type -program $name -exe "$($_.Name)";
Write-Progress -Activity "Creating Firewall Rules" -Status "$i of $count" -Id 1 -PercentComplete (($i/$count)*100) -CurrentOperation "Creating $ProgramRule rules";

if ($null -eq (Get-NetFirewallRule -Name "$ProgramRule*") ) {

Write-Debug "Creating '$($programRule) inbound'";
Write-Progress -Activity "Creating $ProgramRule" -Status "creating inbound/outbound rules" -Id 2 -parentid 1 -CurrentOperation "Creating inbound rule";
$newRules.add((New-NetFirewallRule -DisplayName "$ProgramRule inbound" -Name "$ProgramRule inbound" -Action $action -Profile Any -Direction Inbound -Program "$($_.Fullname)"))

Write-Debug "Creating '$($programRule) outbound'";
Write-Progress -Activity "Creating $ProgramRule" -Status "creating inbound/outbound rules" -Id 2 -parentid 1 -CurrentOperation "Creating outbound rule";
$newRules.add((New-NetFirewallRule -DisplayName "$ProgramRule outbound" -Name "$ProgramRule Outbound" -Action $action -Profile Any -Direction Outbound -Program "$($_.Fullname)"))

Write-Progress -Activity "Creating $ProgramRule" -Status "creating inbound/outbound rules" -Id 2 -parentid 1 -Completed
} else {
"$ProgramRule already exists" | Out-Host;
}
$i++;
}
}

end {
Write-Progress -Activity "Creating Firewall Rules" -Status "$i of $count" -Id 1 -Completed;
if ($null -eq $newRules) {
"Some or all Rules already existed" | Out-Host
}
return $newRules;
}
}
21 changes: 2 additions & 19 deletions EzFirewallMgmt/Public/Block-Port.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,11 @@ function Block-Port {
if ([string]::IsNullOrEmpty($protocol)) {
$protocol = "BOTH";
}
$newRules = New-Object System.Collections.Generic.List[object];
# $newRules = New-Object System.Collections.Generic.List[object];
}

process {
if ($protocol -eq "BOTH" -OR $protocol -eq "TCP") {
$TCPRule = Get-PortRuleName -type "Block" -port $port -protocol "TCP";
if ($null -eq (Get-NetFirewallRule -Name "$TCPRule*") ) {
$newRules.add((New-NetFirewallRule -DisplayName "$TCPRule inbound" -Name "$TCPRule inbound" -Action "Block" -Profile Any -Direction Inbound -Protocol TCP -LocalPort $port -EA 0))
$newRules.add((New-NetFirewallRule -DisplayName "$TCPRule outbound" -Name "$TCPRule outbound" -Action "Block" -Profile Any -Direction Outbound -Protocol TCP -LocalPort $port -EA 0))
} else {
"$TCPRule already exists" | Out-Host;
}
}
if ($protocol -eq "BOTH" -OR $protocol -eq "UDP") {
$UDPRule = Get-PortRuleName -type "Block" -port $port -protocol "UDP";
if ($null -eq (Get-NetFirewallRule -Name "$UDPRule*") ) {
$newRules.add((New-NetFirewallRule -DisplayName "$UDPRule inbound" -Name "$UDPRule inbound" -Action "Block" -Profile Any -Direction Inbound -Protocol UDP -LocalPort $port -EA 0))
$newRules.add((New-NetFirewallRule -DisplayName "$UDPRule outbound" -Name "$UDPRule outbound" -Action "Block" -Profile Any -Direction Outbound -Protocol UDP -LocalPort $port -EA 0))
} else {
"$UDPRule already exists" | Out-Host;
}
}
$newRules = Add-PortRule -port $port -protocol $protocol -type Block;
}

end {
Expand Down
15 changes: 1 addition & 14 deletions EzFirewallMgmt/Public/Block-Program.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ function Block-Program {

begin {
$paths = New-Object System.Collections.Generic.List[Object];
$newRules = New-Object System.Collections.Generic.List[object];
}

process {
Expand Down Expand Up @@ -98,19 +97,7 @@ function Block-Program {
}

end {
Write-Debug "paths list is $($paths | out-string)";
$paths | Foreach-Object {
$ProgramRule = Get-ProgramRuleName -type "Block" -program $name -exe "$($_.Name)";
if ($null -eq (Get-NetFirewallRule -Name "$ProgramRule*") ) {
$newRules.add((New-NetFirewallRule -DisplayName "$ProgramRule inbound" -Name "$ProgramRule inbound" -Action "Block" -Profile Any -Direction Inbound -Program "$($_.Fullname)"))
$newRules.add((New-NetFirewallRule -DisplayName "$ProgramRule Outbound" -Name "$ProgramRule Outbound" -Action "Block" -Profile Any -Direction Outbound -Program "$($_.Fullname)"))
} else {
"$ProgramRule already exists" | Out-Host;
}
}
if ($null -eq $newRules) {
"Some or all Rules already existed" | Out-Host
}
$newRules = $paths | Add-ProgramRule -type Block -count $paths.count;
return $newRules;
}
}
6 changes: 4 additions & 2 deletions EzFirewallMgmt/Public/Remove-PortRule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ function Remove-PortRule {
if ($protocol -eq "BOTH" -OR $protocol -eq "TCP") {
$TCPRule = Get-PortRuleName -type "Unblock" -port $port -protocol "TCP";
"Removing $TCPRule" | Out-Host;
$removedRules.add((Remove-NetFirewallRule -Name "$TCPRule*" -EA 0))
$removedRules.add((Get-NetFirewallRule -Name "$TCPRule*" -EA 0))
Get-NetFirewallRule -Name "$TCPRule*" | Remove-NetFirewallRule -EA 0;
# $removedRules.add((Remove-NetFirewallRule -Name $TCPRule -EA 0))
}
if ($protocol -eq "BOTH" -OR $protocol -eq "UDP") {
$UDPRule = Get-PortRuleName -type "Unblock" -port $port -protocol "UDP";
$removedRules.add((Remove-NetFirewallRule -Name "$UDPRule*" -EA 0))
$removedRules.add((Get-NetFirewallRule -Name "$UDPRule*" -EA 0))
Get-NetFirewallRule -Name "$UDPRule*" | Remove-NetFirewallRule -EA 0;
# $removedRules.add((Remove-NetFirewallRule -Name $UDPRule -EA 0))
}
}
Expand Down
6 changes: 4 additions & 2 deletions EzFirewallMgmt/Public/Remove-ProgramRule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ function Remove-ProgramRule {
)

begin {
$removedRules = New-Object System.Collections.Generic.List[object];
# $removedRules = New-Object System.Collections.Generic.List[object];
}

process {
$programRule = Get-ProgramRuleName -type $type -program $program -exe $exe;
$removedRules.add((Remove-NetFirewallRule -Name "$ProgramRule" -EA 0))

$removedRules = Get-NetFirewallRule -Name "$ProgramRule" -EA 0;
Get-NetFirewallRule -Name "$ProgramRule" | Remove-NetFirewallRule -EA 0;
}

end {
Expand Down
24 changes: 5 additions & 19 deletions EzFirewallMgmt/Public/Unblock-Port.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function Unblock-Port {
.LINK
Remove-PortRule
.LINK
Add-PortRule
.LINK
New-NetfirewallRule
Expand All @@ -49,28 +52,11 @@ function Unblock-Port {
if ([string]::IsNullOrEmpty($protocol)) {
$protocol = "BOTH";
}
$newRules = New-Object System.Collections.Generic.List[object];
# $newRules = New-Object System.Collections.Generic.List[object];
}

process {
if ($protocol -eq "BOTH" -OR $protocol -eq "TCP") {
$TCPRule = Get-PortRuleName -type "Unblock" -port $port -protocol "TCP";
if ($null -eq (Get-NetFirewallRule -Name "$TCPRule*") ) {
$newRules.add((New-NetFirewallRule -DisplayName "$TCPRule inbound" -Name "$TCPRule inbound" -Action "Allow" -Profile Any -Direction Inbound -Protocol TCP -LocalPort $port))
$newRules.add((New-NetFirewallRule -DisplayName "$TCPRule outbound" -Name "$TCPRule outbound" -Action "Allow" -Profile Any -Direction Outbound -Protocol TCP -LocalPort $port))
} else {
"$TCPRule already exists" | Out-Host;
}
}
if ($protocol -eq "BOTH" -OR $protocol -eq "UDP") {
$UDPRule = Get-PortRuleName -type "Unblock" -port $port -protocol "UDP";
if ($null -eq (Get-NetFirewallRule -Name "$UDPRule*") ) {
$newRules.add((New-NetFirewallRule -DisplayName "$UDPRule inbound" -Name "$UDPRule inbound" -Action "Allow" -Profile Any -Direction Inbound -Protocol UDP -LocalPort $port))
$newRules.add((New-NetFirewallRule -DisplayName "$UDPRule outbound" -Name "$UDPRule outbound" -Action "Allow" -Profile Any -Direction Outbound -Protocol UDP -LocalPort $port))
} else {
"$UDPRule already exists" | Out-Host;
}
}
$newRules = Add-PortRule -port $port -protocol $protocol -type Unblock;
}

end {
Expand Down

0 comments on commit 7d0edbb

Please sign in to comment.