Host is also blocked in firewall if action.firewall is true in BlockHost action

This commit is contained in:
Yorick Barbanneau 2018-04-03 16:05:53 +02:00
parent 4a715091b7
commit d986ccb16d
2 changed files with 37 additions and 7 deletions

View file

@ -26,7 +26,6 @@ function BlockHost {
[object]$params
)
if ( $params.ContainsKey('file') ) {
Foreach ($line in Get-Content $params.file ){ BlockHost -params @{host=$line} }
}
if ( $params.ContainsKey('host') -and $params.host -ne "" ) {
@ -39,12 +38,38 @@ function BlockHost {
Write-Host -ForegroundColor Yellow "already blocked "
return
}
Write-Host -ForegroundColor Green "done"
}
catch {
Write-Host -ForegroundColor Red "error"
Write-Host -NoNewline -ForegroundColor Red "error`n`t"
Write-Host -ForegroundColor DarkRed $Error[0].Exeption.Message
return
}
Write-Host -ForegroundColor Green "done"
if ( $params.ContainsKey('firewall') -and $params.firewall ) {
BlockHostByIP $params.host
}
}
else {
Write-Host -ForegroundColor Red "`tError : No name or file for action $($MyInvocation.MyCommand.Name)"
}
}
function BlockHostByIP {
param(
[string]$hostname
)
$ip = [system.net.Dns]::GetHostAddresses($hostname)
$rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $ip } -ErrorAction SilentlyContinue | Get-NetFirewallRule
if ( $rule ) {
write-host -ForegroundColor yellow "`t`tFW Rule exist : ($($rule.name))"
}
else {
write-host -NoNewline -Foregroundcolor Green "`t`tFW block host.`n`t`t"
FwBlockOutputIP @{
ip=$ip,
name=$hostname
}
}
}