mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
904 B
PowerShell
38 lines
904 B
PowerShell
![]()
3 years ago
|
<#
|
||
|
.SYNOPSIS
|
||
|
Remove one or more hosts entries from the Windows hosts file.
|
||
|
|
||
|
.PARAMETER Hosts
|
||
|
A list of hosts entries, delimited by '|'.
|
||
|
#>
|
||
|
|
||
|
[CmdletBinding()]
|
||
|
param(
|
||
|
[Parameter(Mandatory=$true, Position=0)][String]$Hosts
|
||
|
)
|
||
|
|
||
|
$ProgressPreference = "SilentlyContinue"
|
||
|
$ErrorActionPreference = "Stop"
|
||
|
|
||
|
Write-Verbose -Message "Removing host file entries"
|
||
|
|
||
|
$hosts_entries = $Hosts.Split('|')
|
||
|
$hosts_file = "$env:SystemRoot\System32\drivers\etc\hosts"
|
||
|
$hosts_file_lines = [System.IO.File]::ReadAllLines($hosts_file)
|
||
|
$changed = $false
|
||
|
|
||
|
$new_lines = [System.Collections.ArrayList]@()
|
||
|
|
||
|
foreach ($host_line in $hosts_file_lines) {
|
||
|
if ($host_line -in $hosts_entries) {
|
||
|
$changed = $true
|
||
|
} else {
|
||
|
$new_lines += $host_line
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ($changed) {
|
||
|
Write-Verbose -Message "Host file has extra entries, removing extra entries"
|
||
|
[System.IO.File]::WriteAllLines($hosts_file, $new_lines)
|
||
|
}
|