From 97b826a91548cb8c5122699fe6d418434aa84719 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 4 Apr 2018 22:12:48 +0200 Subject: [PATCH] Revert to Resolve-DnsName in BlockHostByIP(), File in BlockHost() can use comment --- cleanW10.ps1 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cleanW10.ps1 b/cleanW10.ps1 index 8397c05..8ee4f23 100755 --- a/cleanW10.ps1 +++ b/cleanW10.ps1 @@ -26,7 +26,7 @@ function BlockHost { [object]$params ) 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 "" ) { Write-Host "`n`tBlock host $($params.host) : " @@ -63,12 +63,12 @@ function BlockHostByIP { param( [string]$hostname ) - $resolv = [system.net.Dns]::GetHostAddresses($hostname) | Select IPAddressToString - #$resolv = Resolve-DnsName $hostname -ErrorAction SilentlyContinue | select Address,Type | Where { $_.type -match "^A{1,4}$" } + #$resolv = [system.net.Dns]::GetHostAddresses($hostname) | Select IPAddressToString + $resolv = Resolve-DnsName $hostname -ErrorAction SilentlyContinue | Where { $_.type -match "^A{1,4}$" } | select Address $resolv | Foreach { Write-Host -NoNewLine "`t" - $ip = $_.IPAddressToString - Write-Debug "Found a valid IP $($_.IPAddressToString)" + $ip = $_.Address + Write-Debug "Found a valid IP $ip" $rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $ip } | Get-NetFirewallRule if ( $rule ) { write-host -NoNewLine "`tFW Rule exist : " @@ -107,10 +107,11 @@ function FwBlockOutputIP { else { $name = $FW_RULE_NAME_PREFIX + "_IP_" + $params.name + "-" + $params.ip } - Write-Host -NoNewline "`tAdd FW IP rule $name ($($params.ip)) : " - if ( Get-NetFirewallRule -Name $name -ErrorAction SilentlyContinue) { - Write-Host -ForegroundColor Yellow "already exist" - return + Write-Host -NoNewline "`tAdd FW IP rule $name ($($params.ip)) : " + $rule = Get-NetFirewallAddressFilter | Where-Object { $_.RemoteAddress -eq $params.ip } | Get-NetFirewallRule + if ( $rule ) { + write-host -NoNewLine " exist : " + write-host -ForegroundColor yellow $rule.name } else { Try {