Edit Remot Firewall Ruleses

I use this script to disable firewall rules remotely.

There is something missing but I do not see what part is wrong.

Hopefully after this post I get the right direction

thx.

Blockquote

CSV-bestand met serverinformatie

$csvPath = ‘\\HPOM-FWRuletest.csv’
$Exportpath = ‘\csv\HPOM\HPOM-FWRuletest.csv’

$servers = Import-Csv -Path $csvPath -Delimiter “;”

$domainlist = $servers.domain |Group-Object $_.domain |select -ExpandProperty name

Ask for domain credentials and store for re-use

$credstore = @{}
foreach($domain in $domainlist) {
    # ask credential
 $credential = Get-Credential -Message  "geeft credential voor domain $($domain)"
 if(-not($credential)) {
        # skip
  }
  # store credential
 $credstore[$domain] = $credential
}

Resultaat opslag

$results = @()

$firewallRules = @(
@{
DisplayName = “HP Software HTTP Communication Broker”
Protocol = “TCP”
ProgramPath = “C:\Program Files\HP\HP BTO Software\bin\win64\ovbbccb.exe”
},
@{
DisplayName = “HP Software HTTP Communication Broker”
Protocol = “UDP”
ProgramPath = “C:\Program Files\HP\HP BTO Software\bin\win64\ovbbccb.exe”
},
@{
DisplayName = “HP Software HTTP Reverse Channel Proxy”
Protocol = “TCP”
ProgramPath = “C:\Program Files\HP\HP BTO Software\bin\win64\ovbbccb.exe”
},
@{
DisplayName = “HP Software HTTP Reverse Channel Proxy”
Protocol = “UDP”
ProgramPath = “C:\Program Files\HP\HP BTO Software\bin\win64\ovbbccb.exe”
}
)

Write-verbose -verbose “Processing: $($server.servernaam)”

foreach ($server in $servers) {

$servername = $server.servernaam
$domain = $server.domain
$cred = $credstore[$domain]
#Definieer de firewall regels

$results = @()

Maak een sessie naar de remote server

Invoke-Command -ComputerName $serverName -Credential $cred -ScriptBlock {
    param($firewallRules)

    # Initialize results for this session
    

    foreach ($rule in $firewallRules) {

        $firewallRule = Get-NetFirewallRule -DisplayName $rule.DisplayName -ErrorAction SilentlyContinue
        if ($firewallRule) {
            $ruleStatus = $firewallRule.Enabled
            $protocol = $rule.Protocol
            
            # Controleer of de regel ingeschakeld is
            if ($ruleStatus -eq "True") {
                # Zet de regel uit
                Set-NetFirewallRule -DisplayName $rule.DisplayName -Enabled False

                #nieuwe status
                $New = get-NetFirewallRule -DisplayName $rule.DisplayName

                # Voeg de resultaten toe
                [PSCustomObject]@{
                    ServerName   = $using:serverName
                    FirewallRule = $rule.DisplayName
                    Protocol     = $protocol
                    FWR_Enabled  = $new.enabled
                }
            } else {
                # Voeg de resultaten toe voor een niet-ingeschakelde regel
                [PSCustomObject]@{
                    ServerName   = $using:serverName
                    FirewallRule = $rule.DisplayName
                    Protocol     = $protocol
                    FWR_Enabled  = $ruleStatus
            }
        } else {
            # Voeg toe dat de regel niet bestaat
            [PSCustomObject]@{
                ServerName   = $using:serverName
                FirewallRule = $rule.DisplayName
                Protocol     = $rule.Protocol
                Status       = "Not Found"
            }
        }
    }
    return $results
}  -ArgumentList $firewallRules -ErrorAction SilentlyContinue | ForEach-Object {
    $results += $_
}
 }

Exporteer de resultaten naar een CSV bestand

$results | Export-Csv -Path $Exportpath -NoTypeInformation -Force -Append

Write-Host “Script uitgevoerd en resultaten zijn opgeslagen.”

Blockquote

Hi @pba1211 and welcome back to the forums.

To make sure we have the correct code and nothing was cut off, please fix your post to make sure it adheres to our coding practices on the site, listed below:

Hi Austin_H,

Thanx for reminding me, editing and saving gives me a message saying it was to long ago to make changes to my post.
In a new post I will make sure it will be done correctly.