-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetworkParsers.psm1
180 lines (153 loc) · 6.43 KB
/
NetworkParsers.psm1
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
function Get-Info ($Line) {
$AdapterSetting = @{}
# Split on this so we don't accidentally grab IPv6 address
$LineArray = $Line -split ' : '
$Setting = $LineArray[0].Replace(' ', '')
$Setting = $Setting.Replace('.', '')
$AdapterSetting['Setting'] = $Setting
$AdapterSetting['Value'] = $LineArray[1]
$AdapterSetting
}
function Import-IPConfig {
<#
.SYNOPSIS
Parses the output of ipconfig or ipconfig /all and returns an object.
.DESCRIPTION
Gathers information for each adapter present on the target and returns and array of objects.
.PARAMETER Path
Path to file[s]
.PARAMETER Extension
Extension for output files. Defaults to '*.txt'
.PARAMETER Property
Regex for target properties of each adapter. Defaults to 'Media State|Address|Mask|Gateway|Description'
.PARAMETER ExcludedAdapters
Regex for adapters to exclude. Defaults to 'Tunnel'
.EXAMPLE
PS> .\Import-IPConfig.ps1 -Path C:\Path\To\Files -Property 'address|subnet'
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $True,
ValueFromPipeLine = $True)]
[ValidateScript({Test-Path -Path $_})]
[string]
$Path,
[string]
$Extension = '*.txt',
[string]
$Property = [regex]'Media State|Address|Mask|Gateway|Description',
[string]
$ExcludedAdapters = [regex]'Tunnel'
)
begin {
$Results = @()
$AdapterRegex = [regex]'adapter.*:'
$ConfigFiles = Get-ChildItem -Path $Path -Recurse -File -Include $Extension
}
process {
foreach ($File in $ConfigFiles) {
try {
Select-String -Path $File -Pattern 'Windows IP Configuration'
}
catch {
Write-Verbose -Message "No IP information found in $C"
Break
}
$Adapter = [pscustomobject]@{}
$Hostname = ((Select-String -Path $File -Pattern 'Host Name').Line -split ' : ')[1]
$DNSSuffix = ((Select-String -Path $File -Pattern 'Primary Dns Suffix').Line -split ' : ')[1]
# Get everything but the blank lines
$Contents = Get-Content -Path $File | Where-Object {$_ -ne ''}
# Will need to parse the Windows IP Configuration
foreach ($Line in $Contents) {
# If we should be skipping this adapter, set the name to 'skip' so subsequent lines are ignored
if ($Line -match $ExcludedAdapters) {
$AdapterName = 'skip'
Continue
}
# If the line matches 'adapter' and we haven't yet seen one, create a new object to hold it, and we're not
# skipping this adapter, assign this adapter as the new adapter name.
elseif (($Line -match $AdapterRegex) -and (!($AdapterName)) -and ($AdapterName -ne 'skip')) {
$Adapter = [pscustomobject]@{}
$AdapterName = $Line.Replace(':', '')
Write-Verbose -Message "Adapter found: $AdapterName"
$Adapter | Add-Member -Name Hostname -Value $Hostname -MemberType NoteProperty
$Adapter | Add-Member -Name PrimaryDnsSuffix -Value $DNSSuffix -MemberType NoteProperty
$Adapter | Add-Member -Name 'AdapterName' -Value $AdapterName -MemberType NoteProperty
}
# If the line matches 'adapter' but hasn't been caught yet, we have reached the next adapter and need to
# start over.
elseif ($Line -match $AdapterRegex) {
$Results += $Adapter
$Adapter = [pscustomobject]@{}
$AdapterName = $Line.Replace(':', '')
Write-Verbose -Message "Adapter found: $AdapterName"
$Adapter | Add-Member -Name Hostname -Value $Hostname -MemberType NoteProperty
$Adapter | Add-Member -Name PrimaryDnsSuffix -Value $DNSSuffix -MemberType NoteProperty
$Adapter | Add-Member -Name 'AdapterName' -Value $AdapterName -MemberType NoteProperty
}
# Otherwise we're just adding more properties for the current adapter. Carry on.
elseif ($AdapterName -and ($Line -match $Property) -and ($AdapterName -ne 'skip')) {
$AdapterSetting = Get-Info $Line
$Adapter | Add-Member -Name $AdapterSetting.Setting -Value $AdapterSetting.Value -MemberType NoteProperty
}
else {
Write-Verbose -Message "No adapter found yet, skipping."
}
}
# Add the last adapter to results
$Results += $Adapter
}
}
end {
$Results
}
}
function Import-Netstat {
<#
.SYNOPSIS
Parses Netstat
.DESCRIPTION
Takes saved Netstat output (supports any combination of -a, -n, -o), parses, and returns as an object.
.PARAMETER Path
Path to file(s)
.EXAMPLE
Import-Netstat.ps1 -Path C:\Path\To\Files
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $True,
ValueFromPipeline = $True)]
[ValidateScript({Test-Path -Path $_})]
[string]
$Path
)
begin {
$Results = @()
$Files = Get-ChildItem -Path $Path -Recurse -File
}
process {
foreach ($File in $Files) {
$FileData = Get-Content -Path $File
$Header = $FileData[3]
$Header = $Header.Trim()
# Split on two or more spaces to ensure we don't split property names containing spaces
$HeaderArray = $Header -split ' +'
foreach ($Connection in $FileData[4..$FileData.Length]) {
$ConnObject = [PSCustomObject] @{}
$Connection = $Connection.Trim()
$ConnectionArray = $Connection -split ' +'
# Links the header array with the appropriate connection array. Also remove spaces, because we like that.
for ($i = 0; $i -lt $HeaderArray.Length; $i++) {
Add-Member -InputObject $ConnObject -MemberType NoteProperty -Name $($HeaderArray[$i].Replace(' ', '')) -Value $ConnectionArray[$i]
}
$Results += $ConnObject
}
}
}
end {
$Results
}
}
Export-ModuleMember -Function Import-IPConfig
Export-ModuleMember -Function Import-Netstat