Revert to Resolve-DnsName in BlockHostByIP(), File in BlockHost() can use comment

This commit is contained in:
Yorick Barbanneau 2018-04-04 22:12:48 +02:00
parent 6151c51531
commit 97b826a915

View file

@ -26,7 +26,7 @@ function BlockHost {
[object]$params [object]$params
) )
if ( $params.ContainsKey('file') ) { if ( $params.ContainsKey('file') ) {
Foreach ($line in Get-Content $params.file ){ BlockHost -params @{host=$line;firewall=$params.firewall} } Get-Content $params.file | where { $_ -notmatch "^#.*$|^$" } | Foreach{ BlockHost -params @{host=$_;firewall=$params.firewall} }
} }
elseif ( $params.ContainsKey('host') -and $params.host -ne "" ) { elseif ( $params.ContainsKey('host') -and $params.host -ne "" ) {
Write-Host "`n`tBlock host $($params.host) : " Write-Host "`n`tBlock host $($params.host) : "
@ -63,12 +63,12 @@ function BlockHostByIP {
param( param(
[string]$hostname [string]$hostname
) )
$resolv = [system.net.Dns]::GetHostAddresses($hostname) | Select IPAddressToString #$resolv = [system.net.Dns]::GetHostAddresses($hostname) | Select IPAddressToString
#$resolv = Resolve-DnsName $hostname -ErrorAction SilentlyContinue | select Address,Type | Where { $_.type -match "^A{1,4}$" } $resolv = Resolve-DnsName $hostname -ErrorAction SilentlyContinue | Where { $_.type -match "^A{1,4}$" } | select Address
$resolv | Foreach { $resolv | Foreach {
Write-Host -NoNewLine "`t" Write-Host -NoNewLine "`t"
$ip = $_.IPAddressToString $ip = $_.Address
Write-Debug "Found a valid IP $($_.IPAddressToString)" Write-Debug "Found a valid IP $ip"
$rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $ip } | Get-NetFirewallRule $rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $ip } | Get-NetFirewallRule
if ( $rule ) { if ( $rule ) {
write-host -NoNewLine "`tFW Rule exist : " write-host -NoNewLine "`tFW Rule exist : "
@ -108,9 +108,10 @@ function FwBlockOutputIP {
$name = $FW_RULE_NAME_PREFIX + "_IP_" + $params.name + "-" + $params.ip $name = $FW_RULE_NAME_PREFIX + "_IP_" + $params.name + "-" + $params.ip
} }
Write-Host -NoNewline "`tAdd FW IP rule $name ($($params.ip)) : " Write-Host -NoNewline "`tAdd FW IP rule $name ($($params.ip)) : "
if ( Get-NetFirewallRule -Name $name -ErrorAction SilentlyContinue) { $rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $params.ip } | Get-NetFirewallRule
Write-Host -ForegroundColor Yellow "already exist" if ( $rule ) {
return write-host -NoNewLine " exist : "
write-host -ForegroundColor yellow $rule.name
} }
else { else {
Try { Try {