summaryrefslogtreecommitdiffstats
path: root/test/lib/ansible_test/_data/playbooks/windows_hosts_prepare.ps1
blob: b9e563d113411b418be426037b2fa1ea7fb47bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<#
.SYNOPSIS
Add one or more hosts entries to 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 "Adding 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

foreach ($entry in $hosts_entries) {
    if ($entry -notin $hosts_file_lines) {
        $hosts_file_lines += $entry
        $changed = $true
    }
}

if ($changed) {
    Write-Verbose -Message "Host file is missing entries, adding missing entries"
    [System.IO.File]::WriteAllLines($hosts_file, $hosts_file_lines)
}