Created
November 30, 2017 19:25
-
-
Save johannesprinz/537a409e2328318d22ca6004f1f4e449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Add-HostFileEntry { | |
[OutputType([String])] | |
[CmdletBinding( | |
SupportsShouldProcess=$true, | |
ConfirmImpact="Medium")] | |
param ( | |
[Parameter(Position=1, Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[ValidatePattern("(?# Valid IP Address range 1.1.1.1 - 255.255.255.255)\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\z")] | |
[string]$IPAddress, | |
[Parameter(Position=2, Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Hostname, | |
[switch]$Force) | |
Begin{ | |
$formats = @{ | |
"Begin" = "Begin {0}..."; | |
"Process" = "...processing {0}..."; | |
"End" = "...ending {0}"; | |
}; | |
Write-Verbose -Message ($formats.Begin -f $MyInvocation.MyCommand); | |
} Process { | |
Write-Verbose -Message ($formats.Process -f $MyInvocation.MyCommand); | |
$hostFilePath = "$env:windir\System32\drivers\etc\hosts"; | |
$entry = "$IPAddress `t $Hostname"; | |
if(Test-HostFileEntry -IPAddress $IPAddress -Hostname $Hostname) { | |
Write-Verbose -Message "$entry already exists"; | |
return $entry; | |
} | |
if($null -ne (Get-HostFileEntry -Hostname $Hostname) -and -not $Force) { | |
throw "$Hostname already has an entry use -Force to overwrite"; | |
} | |
if ($pscmdlet.ShouldProcess($entry)) { | |
$entry | Out-File -Encoding ASCII -Append -FilePath $hostFilePath; | |
return $entry; | |
} | |
} End { | |
Write-Verbose -Message ($formats.End -f $MyInvocation.MyCommand); | |
} | |
} | |
function Remove-HostFileEntry { | |
[OutputType([void])] | |
[CmdletBinding( | |
SupportsShouldProcess=$true, | |
ConfirmImpact="Medium")] | |
param ( | |
[Parameter(Position=1, Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Hostname) | |
Begin{ | |
$formats = @{ | |
"Begin" = "Begin {0}..."; | |
"Process" = "...processing {0}..."; | |
"End" = "...ending {0}"; | |
}; | |
Write-Verbose -Message ($formats.Begin -f $MyInvocation.MyCommand); | |
} Process { | |
Write-Verbose -Message ($formats.Process -f $MyInvocation.MyCommand); | |
$hostFilePath = "$env:windir\System32\drivers\etc\hosts"; | |
$entry = " `t $Hostname"; | |
if($null -eq (Get-HostFileEntry -Hostname $Hostname)) { | |
Write-Verbose -Message "$entry does not exists"; | |
} | |
if ($pscmdlet.ShouldProcess($entry)) { | |
Get-Content -Path $hostFilePath | Select-String $entry -NotMatch | Out-File -Encoding ASCII -FilePath $hostFilePath; | |
} | |
} End { | |
Write-Verbose -Message ($formats.End -f $MyInvocation.MyCommand); | |
} | |
} | |
function Get-HostFileEntry { | |
[OutputType([string])] | |
[CmdletBinding( | |
ConfirmImpact="None")] | |
param ( | |
[Parameter(Position=1, Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Hostname) | |
Begin{ | |
$formats = @{ | |
"Begin" = "Begin {0}..."; | |
"Process" = "...processing {0}..."; | |
"End" = "...ending {0}"; | |
}; | |
Write-Verbose -Message ($formats.Begin -f $MyInvocation.MyCommand); | |
} Process { | |
Write-Verbose -Message ($formats.Process -f $MyInvocation.MyCommand); | |
$hostFilePath = "$env:windir\System32\drivers\etc\hosts"; | |
Get-Content -Path $hostFilePath | Select-String $Hostname; | |
} End { | |
Write-Verbose -Message ($formats.End -f $MyInvocation.MyCommand); | |
} | |
} | |
function Test-HostFileEntry { | |
[OutputType([bool])] | |
[CmdletBinding( | |
ConfirmImpact="None")] | |
param ( | |
[Parameter(Position=1, Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[ValidatePattern("(?# Valid IP Address range 1.1.1.1 - 255.255.255.255)\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\z")] | |
[string]$IPAddress, | |
[Parameter(Position=2, Mandatory=$true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Hostname) | |
Begin{ | |
$formats = @{ | |
"Begin" = "Begin {0}..."; | |
"Process" = "...processing {0}..."; | |
"End" = "...ending {0}"; | |
}; | |
Write-Verbose -Message ($formats.Begin -f $MyInvocation.MyCommand); | |
} Process { | |
Write-Verbose -Message ($formats.Process -f $MyInvocation.MyCommand); | |
$entry = "$IPAddress `t $Hostname"; | |
Get-HostFileEntry -Hostname $Hostname -eq $entry; | |
} End { | |
Write-Verbose -Message ($formats.End -f $MyInvocation.MyCommand); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment