SHARE
TWEET

Foreach-object + NetFirewallRule 22-08-2019.ps1

PowerShell_PC_Aide Aug 22nd, 2019 (edited) 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. -----------------------------------------------------------------------------------------------------------
  3.     Titre: "FixUp NetFirewallRule Windows 8.1 @ 10.ps1"
  4.     Author: Powershell_PC_Aide
  5.     PowerShell ver: minimum v5
  6.     Date: 22-08-2019
  7.     Update:
  8.     Dependents: WinRM (Enable-PSRemoting)
  9.     Elevated: Y
  10.  
  11.     O/P: https://imgur.com/k54hwJE
  12.  ----------------------------------------------------------------------------------------------------------
  13. #>
  14. #Switch - Disabled Firewall Rule
  15. [cmdletbinding()]
  16. Param([Switch] $Disable)
  17.  
  18. #Errors
  19. $ErrorActionPreference = "SilentlyContinue"
  20.  
  21. #File txt (format: FQDN / line)
  22. $PCs = Get-Content ".\List PCs.txt"
  23.  
  24. #Header
  25. "FQDN," + "DisplayName," + "Name"
  26.  
  27. #List PCs
  28. foreach ($PC in $PCs)
  29. {#debut
  30.     #Online
  31.     $Ping = Test-Connection $PC -Quiet -Count 1
  32.     #True
  33.     if ($Ping)
  34.     {
  35.         #Test-wsMan (WinRM service is running)
  36.         $WSManRunning = Test-WSMan $PC
  37.         #True
  38.         if ($WSManRunning)
  39.         {
  40.                                 #True (set)
  41.                                 if($Disable)
  42.                                 {
  43.                                     Invoke-Command -ComputerName $PC -ScriptBlock `
  44.                                     {
  45.                                         $DisplayName = (Get-NetFirewallRule | Where-Object {$_.Enabled -eq $true -and $_.Action -eq "Block"}).DisplayName
  46.                                         Set-NetFirewallRule -DisplayName $DisplayName -Enabled False
  47.                                     }
  48.                                 }
  49.                                 #False (Get)
  50.                                 else
  51.                                 {
  52.                                     Invoke-Command -ComputerName $PC -ScriptBlock `
  53.                                     {
  54.                                         #Firewall
  55. $Firewall = Get-NetFirewallRule
  56.  
  57. #Condition
  58. $Condition = $Firewall | Where-Object {$_.Enabled -eq $true -and $_.Action -eq "Block"}
  59.  
  60. #Rule
  61. $Condition  |
  62.         ForEach-Object{
  63.         $Rule = $_
  64.         $_ | Get-NetFirewallPortFilter |
  65.              ForEach-Object{
  66.                 #StdOut console
  67.                  "$env:computerName.$env:USERDNSDOMAIN" `
  68.                  + "," + $Rule.DisplayName `
  69.                  + "," + $Rule.Name
  70.                
  71.             }
  72.             }
  73.                                     }
  74.                                 }
  75.         }
  76.         #False (WinRM not running)
  77.         else
  78.         {
  79.              $PC + ": " +  "WinRM is not running"
  80.         }
  81.     }
  82.     #False (Offline)
  83.     else
  84.     {
  85.         (Write-Host $PC -ForegroundColor Red ": " -NoNewline) + "Offline"
  86.         "`n"
  87.     }
  88. }#
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top