Advertisement
PowerShell_PC_Aide

Invoke-Commande + Foreach.ps1

Aug 20th, 2019
232
0
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.     version: 20-08-2019
  7.     Dependents: WinRM (Enable-PSRemoting)
  8.     Elevated: Y
  9.  ----------------------------------------------------------------------------------------------------------
  10. #>
  11. #cmdlet ActiveDirectory
  12. Import-Module ActiveDirectory
  13.  
  14. #Errors
  15. $ErrorActionPreference = "SilentlyContinue"
  16.  
  17. #File txt sur dossier courant avec script
  18. #Format: FQDN (E.g.: VM003.golova.com | VM001.brat.com)
  19. $PCs = Get-Content ".\List PCs.txt"
  20.  
  21. #List PCs
  22. foreach ($PC in $PCs)
  23. {
  24.     #Online
  25.     $Ping = Test-Connection $PC -Quiet -Count 1
  26.    
  27.     #True
  28.     if ($Ping)
  29.     {
  30.         #Test-wsMan (WinRM service is running)
  31.         $WSManRunning = Test-WSMan $PC
  32.         #True
  33.         if ($WSManRunning)
  34.         {
  35.              Invoke-Command -ComputerName $PC -ScriptBlock `
  36.             {
  37.                 #Variables
  38.                 $Seprator = ","
  39.                 #Problem (Enabled:True + Action:Block)
  40.                 #Sample [*hummingbird exceed 2007*]
  41.                 $Condition = Get-NetFirewallRule | Where-Object {$_.Enabled -eq "True" -and $_.Action -eq "Block"}
  42.                 #True
  43.                 if ($Condition -ne $null)
  44.                 {
  45.                     #FQDN + SdtOutPut: 1 line by PC
  46.                     "FQDN," `
  47.                     + "DisplayName," `
  48.                     + "Name"
  49.                     "$env:COMPUTERNAME.$env:USERDNSDOMAIN," `
  50.                     + $Condition.DisplayName + "," `
  51.                     + $Condition.Name
  52.                      "`n"
  53.                 }
  54.             }
  55.         }
  56.         #WinRM not running
  57.         Else
  58.         {
  59.             $PC + ": " +  "WinRM is not running"
  60.         }
  61.     }
  62.     #Offline ($Ping = False)
  63.     Else
  64.     {
  65.         (Write-Host $PC -ForegroundColor Red ": " -NoNewline) + "Offline"
  66.         "`n"
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement