forked from jdhitsolutions/Mylmhost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMylmhost.psm1
More file actions
253 lines (210 loc) · 7.51 KB
/
Mylmhost.psm1
File metadata and controls
253 lines (210 loc) · 7.51 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#requires -version 5.0
#region Main
Function Get-LmhostsEntry {
[cmdletbinding(DefaultParameterSetName="All")]
Param(
[Parameter(Position=0,Mandatory,ParameterSetName="Name")]
[Alias("CN","Name")]
[string]$Computername,
[Parameter(Mandatory,ParameterSetName="IP")]
[ValidatePattern("(\d{1,3}\.){3}\d{1,3}")]
[string]$IPAddress,
[Parameter(Mandatory,ParameterSetName="All")]
[switch]$All,
[switch]$Raw
)
Begin {
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
#display PSBoundparameters formatted nicely for Verbose output
[string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd()
Write-Verbose "[BEGIN ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*4)$_"}) | Out-String) `n"
if (-not (Test-Path $lmfile )) {
Write-Warning "No file found at $lmfile. Use Set-LmhostsEntry to add an entry."
$Verified = $False
}
else {
$Verified = $True
}
} #begin
Process {
If ($Verified) {
Write-Verbose "[PROCESS] Get entry by $($PSCmdlet.ParameterSetName)"
Switch ($PSCmdlet.ParameterSetName) {
"Name" {
$find = $Computername
}
"IP" {
$find = $IPAddress
}
Default {
#get all entries
}
} #switch
if ($all) {
$data = Get-Content $lmfile | Select-String -pattern "^[^#.*]"
if ($raw) {
$IPv4Pattern.matches($data).value
}
else {
#create an object for each entry
$IPv4Pattern.matches($data).Captures |
foreach {
[pscustomobject]@{
Computername = $_.Groups["Computername"]
IPAddress = $_.Groups["IP"]
}
} #foreach
}
} #get all
else {
Write-Verbose "[PROCESS] ...$find"
$entry =
Get-Content -Path $lmfile | Select-String -pattern $find -AllMatches
if ($entry) {
Write-Verbose "[PROCESS] Found one or more matching entries"
if ($Raw) {
Write-Verbose "[PROCESS] Writing raw results to the pipeline"
#write the raw entry to the pipeline
$entry
}
else {
foreach ($item in $entry) {
#split into parts and write a new object to the pipeline
$m = $IPv4Pattern.Match($item)
[pscustomobject]@{
Computername = $m.Groups["Computername"].value
IPAddress = $m.Groups["IP"].value
}
} #foreach
} #else
} #if entry found
else {
Write-Warning "Entry for $($PSBoundParameters.values) not found."
}
} #else find single entry
} #if verified
}# process
End {
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end
} #close Get
Function Remove-LmhostsEntry {
[cmdletbinding(SupportsShouldProcess,DefaultParameterSetName="Name")]
Param(
[Parameter(Position=0,Mandatory,ParameterSetName="Name")]
[Alias("CN","Name")]
[string]$Computername,
[Parameter(Mandatory,ParameterSetName="IP")]
[ValidatePattern("(\d{1,3}\.){3}\d{1,3}")]
[string]$IPAddress
)
Begin {
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
#display PSBoundparameters formatted nicely for Verbose output
[string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd()
Write-Verbose "[BEGIN ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*4)$_"}) | Out-String) `n"
if (-not (Test-Path $lmfile )) {
Write-Warning "No file found at $lmfile. Use Set-LmhostsEntry to add an entry."
$Verified = $False
}
else {
$Verified = $True
}
} #begin
Process {
If ($Verified) {
Write-Verbose "[PROCESS] Removing entry by $($PSCmdlet.ParameterSetName)"
$findparams = @{Raw = $True}
Switch ($PSCmdlet.ParameterSetName) {
'Name' {
$findparams.Add("Computername",$PSBoundParameters.item("Computername"))
} #computername
'IP' {
$findparams.Add("IPAddress",$PSBoundParameters.Item("IPAddress"))
} #IP
} #switch
Write-Verbose "Searching for entry"
$entry = Get-LmhostsEntry @findparams
if ($entry) {
Write-Verbose "[PROCESS] Creating a backup file copy"
Copy-Item -Path $lmfile -Destination $lmbackup
$content = Get-Content -Path $lmfile
Write-Verbose "[PROCESS] Removing $get"
($content -replace $entry,"") | Out-File -FilePath $lmfile -Encoding ascii
}
} #if verified
}# process
End {
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end
} #close Remove
Function Set-LmhostsEntry {
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Mandatory)]
[Alias("CN","Name")]
[string]$Computername,
[Parameter(Mandatory)]
[ValidatePattern("(\d{1,3}\.){3}\d{1,3}")]
[string]$IPAddress,
[switch]$Passthru
)
Begin {
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
if (-not (Test-Path $lmfile )) {
Write-Verbose "[BEGIN ] Creating a new lmhosts file: $lmfile"
Set-Content -Value "#lmhosts file for resolving NETBIOS names `n" -Path $lmfile -Encoding Ascii
}
} #begin
Process {
$entry = "$IPAddress $Computername"
Write-Verbose "[PROCESS] Check for existing entry $entry"
#There should only be one or at least we'll only process one
$existing = Get-Content -Path $lmfile | Select-String $Computername | Select -last 1
if ($existing) {
[string]$line = $existing.Line
Write-Verbose "[PROCESS] Processing existing entry: $line"
$m = $IPv4Pattern.Match($line)
$OldComputername = $m.Groups["Computername"].value
$oldIP = $m.Groups["IP"].value
Write-Verbose "Existing Computername = $oldComputername"
Write-Verbose "Existing IPAddress = $oldIP"
if (($oldIP -eq $IPAddress) -and ($OldComputername -eq $Computername)) {
Write-Host -foreground Green "The Computername and IP address matches the current entry. No changes needed." -ForegroundColor Magenta
#exit
Return
}
else {
#modify the line
$Replace = $True
}
} #if existing
#create a backup copy of the file
Write-Verbose "[PROCESS] Creating a backup file copy"
Copy-Item -Path $lmfile -Destination $lmbackup
#insert the line
if ($Replace) {
$content = Get-Content -Path $lmfile
Write-Verbose "[PROCESS] Replacing $oldIP with $Ipaddress"
($content -replace $line,$entry) | Out-File -FilePath $lmfile -Encoding ascii
}
else {
#insert at the end
Write-Verbose "[PROCESS] Appending $entry"
$entry | Out-File -FilePath $lmfile -Append -Encoding ascii
}
If ($Passthru) {
Get-LmhostsEntry -Computername $Computername
}
} #process
End {
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end
} #close Set
#endregion
#define variables
$lmfile = "$env:windir\system32\drivers\etc\lmhosts"
#define a backup file variable
$lmbackup = "$env:windir\system32\drivers\etc\lmhosts.bak"
#IPv4 Regex pattern
[regex]$IPv4Pattern = "(?<IP>\b(\d{1,3}\.){3}\d{1,3}\b)\s+(?<Computername>\b\S+\b)"