Hosts file – add an IPv6 address
This builds on adding an IPv4 address - http://msmvps.com/blogs/richardsiddaway/archive/2011/10/24/hosts-file-add-a-record.aspx
function add-IPv6hostfilecontent {
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[string]$IPAddress,
[parameter(Mandatory=$true)]
[string]$computer
)
$file = Join-Path -Path $($env:windir) -ChildPath "system32\drivers\etc\hosts"
if (-not (Test-Path -Path $file)){
Throw "Hosts file not found"
}
$data = Get-Content -Path $file
$data += "$IPAddress $computer"
Set-Content -Value $data -Path $file -Force -Encoding ASCII
}
The only difference is that I’ve removed the regex that checks an IPv4 address. i haven’t been able to figure out a sensible regex for an IPv6 address. if any one wants to post one as a comment I’ll add it to the function with fill credit
The get-hostfilecontent from http://msmvps.com/blogs/richardsiddaway/archive/2011/10/23/reading-the-hosts-file.aspx works with IPv4 and IPv6 addresses
PS> get-hostfilecontent
Server IPAddress
------ ---------
RSLAPTOP01 fe80:0000:0000:0000:4547:ee51:7aac:521e
RSLAPTOP01 10.10.54.202