From c59f57ff8fd5f743fcfd06d13b7d315769b97101 Mon Sep 17 00:00:00 2001 From: Angel Ramboi Date: Sat, 22 Aug 2015 11:48:51 +0200 Subject: [PATCH] Make the script idempotent --- scripts/disable-telemetry.ps1 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/disable-telemetry.ps1 b/scripts/disable-telemetry.ps1 index a035932..9993cbe 100644 --- a/scripts/disable-telemetry.ps1 +++ b/scripts/disable-telemetry.ps1 @@ -7,18 +7,24 @@ Import-Module -DisableNameChecking $PSScriptRoot\..\lib\reg-helper.psm1 echo "Adding telemetry domains to hosts file" $hosts = cat "$PSScriptRoot\..\res\telemetry-hosts.txt" +$hosts_file = "$env:systemroot\System32\drivers\etc\hosts" + [ipaddress[]] $ips = @() foreach ($h in $hosts) { try { # store for next part $ips += [ipaddress]$h } catch [System.InvalidCastException] { - # can be redirected by hosts - echo "0.0.0.0 $h" | Out-File -Encoding ASCII -Append "$env:systemroot\System32\drivers\etc\hosts" + $contaisHost = Select-String -Path $hosts_file -Pattern $h + If(-Not $contaisHost) { + # can be redirected by hosts + echo "0.0.0.0 $h" | Out-File -Encoding ASCII -Append $hosts_file + } } } echo "Adding telemetry ips to firewall" +Remove-NetFirewallRule -ErrorAction SilentlyContinue -DisplayName "Block Telemetry IPs" New-NetFirewallRule -DisplayName "Block Telemetry IPs" -Direction Outbound ` -Action Block -RemoteAddress ([string[]]$ips)