-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathAdd-HostFileEntry.ps1
81 lines (62 loc) · 2.14 KB
/
Add-HostFileEntry.ps1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<#
$Metadata = @{
Title = "Add Host File Entry"
Filename = "Add-HostFileEntry.ps1"
Description = ""
Tags = "powershell, function, host, file, manipulation"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-07-10"
LastEditDate = "2013-09-30"
Version = "1.0.0"
License = @'
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Add-HostFileEntry{
<#
.SYNOPSIS
Add an new entry to the hosts file
.DESCRIPTION
Add an new entry the hosts file.
.PARAMETER IP
IP address
.PARAMETER DNS
DNS address
.EXAMPLE
PS C:\> Get-HostFileEntry -IP "192.168.50.4" -DNS "local.wordpress.dev"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String]
$IP,
[Parameter(Mandatory=$true)]
[String]
$DNS
)
#--------------------------------------------------#
# main
#--------------------------------------------------#
$HostFile = "$env:windir\System32\drivers\etc\hosts"
[string]$LastLine = Get-Content $HostFile | select -Last 1
if($IP -match [regex]"(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9])[.]){3}(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9]))"){
if(!(Get-HostFileEntries | where{$_.DNS -eq $DNS})){
if($LastLine -ne ""){
Add-Content -Path $HostFile -Value "`n"
}
Write-Host "Add entry to hosts file: $IP`t$DNS"
Add-Content -Path $HostFile -Value "$IP $DNS"
# reformat hostfile
Set-Content -Path $HostFile -Value (Get-Content -Path $HostFile | %{if($_.StartsWith("#") -or $_ -ne ""){$_}})
}else{
Write-Error "$DNS is already in use!"
}
}else{
Write-Error "IP address is not valid!"
}
}