From 1ea974e3abe2d5f80d519b54ecc64b158c300424 Mon Sep 17 00:00:00 2001 From: Harsh Arora <64034129+harsharora94@users.noreply.github.com> Date: Sat, 25 Apr 2020 19:27:06 +0530 Subject: [PATCH] Remove-Firewall-Group-Rule-Name Firewall Group Rules will be deleted based on command-line arguments --- Remove-Firewall-Group-Rules.ps1 | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Remove-Firewall-Group-Rules.ps1 diff --git a/Remove-Firewall-Group-Rules.ps1 b/Remove-Firewall-Group-Rules.ps1 new file mode 100644 index 0000000..4d3f57b --- /dev/null +++ b/Remove-Firewall-Group-Rules.ps1 @@ -0,0 +1,37 @@ +# --------------------------------------------------------------------------------------------------------------------------------- +# Name : Remove-Firewall-Group-Rules.ps1 +# Author : Harsh Arora +# Description : This powershell script removes either Windows Firewall Inbound or Outbound Group Rules which will be passed +# as a command-line argument using powershell cmdlets +# Arguments : $GroupName, $Direction +# --------------------------------------------------------------------------------------------------------------------------------- + +$GroupName = $args[0] +$Direction = $args[1] +$Logfile = "$env:SystemDrive\RemoveFirewallGroupRule.log" +$ExportFirewallGroupRules = "$env:SystemDrive\ListFirewallGroupRules.csv" +$PSFirewallGroupRule = Get-NetFirewallRule -DisplayGroup "$GroupName" -Direction $Direction | Export-Csv -NoTypeInformation $ExportFirewallGroupRules + +function LogWrite([string]$logstring) +{ + $DateTime = Get-Date -Format g + Add-content $Logfile -value "$DateTime $logstring" +} + +LogWrite "Searching Windows Firewall Group Rule Name : '$GroupName' in direction: '$Direction'" + +#Delete all the matching Firewall Group Rules +If($GroupName -ne $null) +{ + $PSFirewallGroupRule + $Return = Get-NetFirewallRule -DisplayGroup "$GroupName" -Direction $Direction | Remove-NetFirewallRule + + If ($? -eq $false) + { + LogWrite "Not found, so can't delete all the matching Firewall Group Rule Name: '$GroupName' from Direction: '$Direction' and returned: $?" + } + else + { + LogWrite "Found, so deleting all the matching Firewall Group Rule Name: '$GroupName' from Direction: '$Direction' and returned: $?" + } +} \ No newline at end of file