当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Test-FirewallAllServer:查询所有Windows服务器上的防火墙状态

作者:精品下载站 日期:2024-12-14 07:23:54 浏览:15 分类:玩电脑

Test-FirewallAllServer:查询所有Windows服务器上的防火墙状态


我经历过很多人故意禁用 Windows 防火墙。如果事情没有按预期进行,他们不想对此感到恼火。但没有什么可担心的,因为它按其应该的方式工作。我怀疑有些管理员缺乏一些知识。无论如何,让我们获取所有运行 Windows Server 的域计算机的列表,并查明它们是否启用了防火墙。这是一个小函数,用绿色显示好的函数。红色的是坏的?

在我之前的文章使用 PowerShell 配置 Windows 防火墙中,我描述了一些基本的防火墙待办事项。现在,我们更进一步,使用一个功能来测试所有加入域的 Windows 服务器的所有防火墙设置……

[玩转系统] Test-FirewallAllServer:查询所有Windows服务器上的防火墙状态

目标

一个简单的命令。无参数。快速检查所有加入域的 Windows Server。这就是这篇博文的目标。开始了:

[玩转系统] Test-FirewallAllServer:查询所有Windows服务器上的防火墙状态

[玩转系统] Test-FirewallAllServer:查询所有Windows服务器上的防火墙状态

工作中也有特别勇敢的人。你买不到勇气。 ?

[玩转系统] Test-FirewallAllServer:查询所有Windows服务器上的防火墙状态

功能测试-FirewallAllServer

如果您喜欢以上内容,请继续阅读。下面你可以找到代码。

注意事项:

  • 这是在 Windows Server 2012 及更高版本上测试的
  • 确保在域控制器或安装了 RSAT (ADDS) 的计算机上运行该命令
  • 确保您拥有清理后的 Active Directory 环境(没有孤立的 Windows Server 计算机帐户)。该脚本包含 erroraction 参数,该参数会忽略已关闭的服务器。
  • 确保在所有服务器上启用远程管理(自 Windows Server 2012 起默认启用)

打开 PowerShell ISE。将代码复制到您的会话中。

function Test-FirewallAllServer {
$servers=(Get-ADComputer -Filter * -Properties Operatingsystem | Where-Object {$_.operatingsystem -like "*server*"}).Name
$check=Invoke-Command -ComputerName $servers {Get-NetFirewallProfile -Profile Domain | Select-Object -ExpandProperty Enabled} -ErrorAction SilentlyContinue
$line="__________________________________________________________"
$line2="=========================================================="
$en=$check | ? value -EQ "true"
$di=$check | ? value -EQ "false"
If ($en -ne $null) {
Write-Host ""; Write-Host "The following Windows Server have their firewall enabled:" -ForegroundColor Green; $line; Write-Output ""$en.PSComputerName"";Write-Host ""
}
If ($di -ne $null) {
Write-Host ""; Write-Host "The following Windows Server have their firewall disabled:" -ForegroundColor Red ; $line; Write-Output ""$di.PSComputerName""; Write-Host ""
}
If ($di -eq $null) {
Write-Host $line2; Write-Host "All Windows Servers have it's firewall enabled" -ForegroundColor Green; Write-Host ""
}
If ($en -eq $null) {
Write-Host $line2; Write-Host "All Windows Servers have it's firewall disabled" -ForegroundColor Red; Write-Host ""
}
}

使其永久化

在 C:\Program Files\Windows PowerShell\Modules 中创建一个文件夹。将文件夹命名为 Test-FirewallAllServer。将脚本保存为 psm1 文件。 确保目录名和文件名相同。

[玩转系统] Test-FirewallAllServer:查询所有Windows服务器上的防火墙状态

关闭 Windows PowerShell。再次打开 PowerShell。该命令现在可供所有用户使用。

今天就这样。享受探索 Windows 防火墙设置的乐趣……

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯