Private/Hosts/New-HostsEntry.ps1
<#
.SYNOPSIS Creates an entry in the HOSTS file. .PARAMETER Hostname The hostname to create. .PARAMETER IPAddress The IP address to map. .PARAMETER Hosts The path to the hosts file. #> Function New-HostsEntry { [CmdletBinding(SupportsShouldProcess = $true)] Param( [Parameter(Position = 1, Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$Hostname , [Parameter(Position = 2, Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$IPAddress , [Parameter(Position = 3)] [ValidateScript({ Test-Path $_ -PathType "Leaf" })] [string]$Hosts = (Join-Path $env:windir -ChildPath "System32\drivers\etc\hosts") ) Process { $existing = Get-HostsEntry $Hostname -Hosts $Hosts If (($existing -ne $IPAddress) -and $PSCmdlet.ShouldProcess($Hosts, "Add $($IPAddress) $($Hostname)")) { "$($IPAddress)`t$($Hostname)" | Add-Content $Hosts } } } |