PowerShell – Сравнение включённых правил брандмауэра на двух компьютерах

Compare Windows Firewall rules with PowerShellСтолкнулся с ситуацией, когда в разборе полётов у одного из Hyper-V серверов с включенным брандмауэром Windows Firewall потребовалось сравнить  действующие правила с другим сервером. Первое, что пришло в голову - с помощью PowerShell удалённо через вызов Invoke-Command слить правила Windows Firewall с обоих серверов в текстовые файлы и затем сравнить их содержимое.

Вот что получилось:

$PC1 = "MyRemotePC1"
$PC2 = "MyRemotePC2"
$LogDir = "C:\Temp\"

$PC1Log = $LogDir + "FWRules_"+ $PC1 + ".log"
$PC1Cmd = Invoke-Command $PC1 {
$FWObj1 = New-Object -ComObject hnetcfg.fwpolicy2
$FWObj1.rules | Where-Object { $_.enabled } | Sort-Object -Property direction | Format-Table -Property direction, protocol, localports, name -AutoSize -Wrap
} | Out-String | Set-Content $PC1Log

$PC2Log = $LogDir + "FWRules_"+ $PC2 + ".log"
$PC2Cmd = Invoke-Command $PC2 {
$FWObj2 = New-Object -ComObject hnetcfg.fwpolicy2
$FWObj2.rules | Where-Object { $_.enabled } | Sort-Object -Property direction | Format-Table -Property direction, protocol, localports, name -AutoSize -Wrap
} | Out-String | Set-Content $PC2Log

Compare-Object $(Get-Content $PC1Log) $(Get-Content $PC2Log) -includeequal | Format-Table -AutoSize -Wrap | Out-String | Set-Content $($LogDir + "FWRules_Compare.log")

Только один комментарий Комментировать

  1. itpadla /

    Reblogged this on Заметки IT Менеджера and commented:
    Очень полезный скрипт

Добавить комментарий