-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabadmin-deny-hostnames.ps1
60 lines (47 loc) · 1.39 KB
/
labadmin-deny-hostnames.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
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Manage hosts file to denay hostnames access
.PARAMETER ShowHostsFile
Show hosts file content
.PARAMETER DenyHosts
Add a list of hosts (by spaces, lines, tabs...) to denay them
.PARAMETER WipeHostsFile
Wipe all labadmin lines inserted
.PARAMETER RemoveHosts
Remove all lines in hosts file contaning string
.NOTES
File Name: labadmin-edit-hostsfile.ps1
Author : Leonardo Marco
#>
Param(
[Switch]$ShowHostsFile,
[String]$DenyHosts,
[Switch]$WipeHostsFile,
[String]$RemoveHosts
)
# CONFIG VARIABLES
$hosts_path = "$($Env:WinDir)\system32\Drivers\etc\hosts"
$hosts_comment= "# labadmin-edit-hostsfile"
# SHOWHOSTSFILE
function ShowHostsFile {
Get-Content $hosts_path
}
# DENAYHOSTS
function DenyHosts {
$DenyHosts = ($DenyHosts -split "[\r\n]+") | foreach { "127.0.0.1".PadRight(20, " ") + $_.PadRight(40, " ") + $hosts_comment +" ("+ (Get-Date -format "yyyy-MM-dd HH:MM:ss")+")" }
Add-Content -Encoding UTF8 $hosts_path $DenyHosts
}
# WIPEHOSTSFILE
function WipeHostsFile {
$RemoveHosts=$hosts_comment
RemoveHosts
}
# REMOVEHOSTS
function RemoveHosts {
(Get-Content $hosts_path | Where-Object { -not $_.Contains($RemoveHosts) }) | Out-File -Encoding UTF8 -FilePath $hosts_path
}
if($WipeHostsFile) { WipeHostsFile }
if($DenyHosts) { DenyHosts }
if($RemoveHosts) { RemoveHosts }
if($ShowHostsFile) { ShowHostsFile }