PowerShell: Check firewall status on all servers in a domain
A script that remotely queries every server in the domain for the status of the Windows Firewall service, so you can find the machines where it has gone quiet.
To check the status of the firewall on all servers in the domain using PowerShell, the following script can be used. This script will remotely query each server in the domain to check the status of the Windows Firewall service.
Make sure to run the script with appropriate administrative privileges.
The script
# Define the list of servers in your domain
$servers = Get-ADComputer -Filter {OperatingSystem -like "Windows*Server*"} -Property Name -SearchBase "OU=Servers,DC=domain,DC=com"
# Function to check the status of the Windows Firewall service
function Get-FirewallStatus {
param(
[string]$computerName
)
$firewall = Get-Service -Name "MpsSvc" -ComputerName $computerName -ErrorAction SilentlyContinue
if ($firewall) {
return $firewall.Status
} else {
return "Firewall service not found"
}
}
# Loop through each server and check the firewall status
foreach ($server in $servers) {
$computerName = $server.Name
$firewallStatus = Get-FirewallStatus -computerName $computerName
Write-Host "Server: $computerName, Firewall Status: $firewallStatus"
}
How it works
Replace -SearchBase with the name of your actual domain and the organizational unit (OU) you want to search. The script uses the Get-ADComputer cmdlet to fetch all server objects from the "Servers" organizational unit (OU) in the domain. It then loops through each server, queries the status of the "MpsSvc" (Windows Firewall) service using the Get-Service cmdlet, and prints the result on the console.
Please note that for this script to work, you need to have the necessary permissions to access the servers remotely and query the firewall service status. Additionally, make sure that you have the Active Directory PowerShell module installed to run the Get-ADComputer cmdlet.
Har ni ett problem vi borde skriva om?
Hälften av artiklarna här började som en kundfråga. Ställ er fråga — svaret kanske blir nästa guide.
