Created
April 25, 2025 11:04
-
-
Save MrWhitee4/6563097031efb09209b3f847264029ca to your computer and use it in GitHub Desktop.
CheckAndReset-PublicNetworkProfiles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================ | |
# Script: Check-NetworkProfileAndRestart.ps1 | |
# Funktion: Prüft Netzwerkprofil, Neustart bei "Public" | |
# Voraussetzungen: Adminrechte | |
# ============================================ | |
# Konfiguration | |
$logFile = "C:\Logs\NetworkProfileCheck.log" | |
$eventSource = "Network" | |
$eventLogName = "System" | |
$eventId = 1001 | |
$eventMessage = "Netzwerkadapter wegen falschem Firewall-Profil neu gestartet" | |
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
# Sicherstellen, dass der Log-Pfad existiert | |
if (!(Test-Path -Path (Split-Path $logFile))) { | |
New-Item -Path (Split-Path $logFile) -ItemType Directory -Force | |
} | |
# Event Source anlegen, falls nicht vorhanden | |
if (-not [System.Diagnostics.EventLog]::SourceExists($eventSource)) { | |
New-EventLog -LogName $eventLogName -Source $eventSource | |
Write-Output "$timestamp - EventLog-Quelle '$eventSource' wurde erstellt." | Out-File -Append $logFile | |
} | |
# Netzwerkprofile prüfen | |
$publicNetworks = Get-NetConnectionProfile | Where-Object { $_.NetworkCategory -eq "Public" } | |
if ($publicNetworks) { | |
# Adapter neu starten | |
Restart-NetAdapter -Name "*" -Confirm:$false | |
# Logging: EventLog | |
Write-EventLog -LogName $eventLogName -Source $eventSource -EventId $eventId -EntryType Information -Message $eventMessage | |
# Logging: Datei | |
$networkNames = ($publicNetworks | Select-Object -ExpandProperty Name) -join ", " | |
$logEntry = "$timestamp - Öffentliche Netzwerke erkannt ($networkNames). Adapter wurden neu gestartet." | |
Write-Output $logEntry | Out-File -Append $logFile | |
} | |
else { | |
$logEntry = "$timestamp - Keine öffentlichen Netzwerke erkannt. Keine Aktion erforderlich." | |
Write-Output $logEntry | Out-File -Append $logFile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment