-
Notifications
You must be signed in to change notification settings - Fork 30
/
Invoke-Grep.ps1
241 lines (211 loc) · 6.29 KB
/
Invoke-Grep.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
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
$source = @"
using System;
using System.IO;
namespace Netzwerker.Shell
{
public class GrepParameterInformation
{
public bool Skip = false;
public FileInfo File;
public string Content;
public GrepParameterInformation(FileInfo info)
{
this.File = info;
}
public GrepParameterInformation(DirectoryInfo info)
{
this.Skip = true;
}
public GrepParameterInformation(string Content)
{
this.Content = Content;
}
}
}
"@
Add-Type $source
function Invoke-Grep
{
<#
.SYNOPSIS
Performs Grep Operations
.DESCRIPTION
Filters strings (or text files) for occurences of a specific pattern and returns Information based on switches.
.PARAMETER InputObject
Alias: io
ParSet: Normal, IP
The strings to filter or the full path of files to read from.
.PARAMETER Pattern
Alias: p
ParSet: Normal
The regex pattern to look for.
.PARAMETER IPAddress
Alias: ip
ParSet: IP
Extracts all IP addresses from the input and returns IPAddress objects.
.PARAMETER Exact
Alias: e
ParSet: Normal
Switches to only returning the exact passages that match it (instead of the whole line).
-IPAddress takes precedence over -Exact
.PARAMETER NoRes
Internal Use Only
.EXAMPLE
PS C:\> dir c:\service | grep "Friedrich"
Searches all files in c:\service and returns each line that contains the word "Friedrich".
.EXAMPLE
PS C:\> nslookup.exe "haka.de" | grep -IP
This command first queries your default dns server for haka.de, then greps all IP-Addresses contained in the reply (Usually, that's in nslookup's case the resolving DNS Server and the target).
.NOTES
Supported Interfaces:
------------------------
Result Caching Interface
Stores full Result
------------------------
Author: fwn
Company: die netzwerker Computernetze GmbH
Created: 04.09.2014
LastChanged: 04.09.2014
Version: 1.0
#>
[CmdletBinding(DefaultParameterSetName = 'Normal')]
[OutputType([string[]], ParameterSetName = 'Normal')]
[OutputType([System.Net.IPAddress[]], ParameterSetName = 'IP')]
param (
[Parameter(ParameterSetName = 'Normal',
Mandatory = $true,
ValueFromPipeline = $true,
Position = 1)]
[Parameter(ParameterSetName = 'IP',
Mandatory = $true,
ValueFromPipeline = $true,
Position = 1)]
[AllowEmptyString()]
[Alias('io')]
[Netzwerker.Shell.GrepParameterInformation[]]
$InputObject,
[Parameter(ParameterSetName = 'Normal',
Mandatory = $true,
Position = 0)]
[Alias('p')]
[string]
$Pattern,
[Parameter(ParameterSetName = 'IP',
Mandatory = $true)]
[Alias('ip')]
[switch]
$IPAddress,
[Parameter(ParameterSetName = 'Normal')]
[Alias('e')]
[switch]
$Exact,
[Switch]
$NoRes
)
Begin
{
# Set the functionname variable
$fn = (Get-PSCallStack)[0].Command
Write-Debug "[Start] [Greping content]"
# Get active ParameterSet
$ParSet = $PSCmdlet.ParameterSetName
Write-Debug "Active Parameterset: $ParSet"
# If IP Mode is active, overwrite Pattern with IP Pattern
if ($ParSet -eq "IP") { $Pattern = "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" }
# Prepare Result for caching
$Results = @()
#region Helper Function
function Append-Result
{
[CmdletBinding()]
Param (
[Parameter(Position = 0, ValueFromPipeline = $true)]
$Result
)
Begin
{
$temp = (Get-Variable -Scope 1 -Name "Results").Value
}
Process
{
$temp += $Result
$Result
}
End
{
Set-Variable -Name "Results" -Scope 1 -Value $temp
}
}
#endregion Helper Function
}
Process
{
switch ($ParSet)
{
#region ParSet Normal
'Normal'
{
# Iterate over each object
foreach ($Obj in $InputObject)
{
# If a Fileinfo Object was passed
if ($Obj.File -ne $null)
{
# Process content of files
Write-Debug "Processing $($Obj.File.FullName)"
if (!$Exact) { Get-Content $Obj.File.FullName | Select-String $Pattern | %{ $_.ToString() } | Append-Result }
else { Get-Content $Obj.File.FullName | Select-String $Pattern -AllMatches | Select -ExpandProperty Matches | Select -ExpandProperty Value | Append-Result }
}
# If a string was passed
elseif ($Obj.Content.Length -gt 0)
{
foreach ($line in ($Obj.Content | %{ $_.Split("`n") } | Remove-Null))
{
# Process lines of string
Write-Debug "Processing line $line"
if (!$Exact) { $line | Select-String $Pattern | %{ $_.ToString() } | Append-Result }
else { $line | Select-String $Pattern -AllMatches | Select -ExpandProperty Matches | Select -ExpandProperty Value | Append-Result }
}
}
}
break
}
#endregion ParSet Normal
#region ParSet IP
'IP'
{
# Iterate over each object
foreach ($Obj in $InputObject)
{
# If a Fileinfo Object was passed
if ($Obj.File -ne $null)
{
# Process content of files
Write-Debug "Processing $($Obj.File.FullName)"
Get-Content $Obj.File.FullName | Select-String $Pattern -AllMatches | Select -ExpandProperty Matches | Select -ExpandProperty Value | %{ [System.Net.IPAddress]::Parse($_) | Append-Result }
}
# If a string was passed
elseif ($Obj.Content.Length -gt 0)
{
foreach ($line in ($Obj.Content | %{ $_.Split("`n") } | Remove-Null))
{
# Process lines of string
Write-Debug "Processing line $line"
$line | Select-String $Pattern -AllMatches | Select -ExpandProperty Matches | Select -ExpandProperty Value | %{ [System.Net.IPAddress]::Parse($_) | Append-Result }
}
}
}
break
}
#endregion ParSet IP
}
}
End
{
# Write closing line
Write-Debug "[End] [Greping content]"
# Return Results
if (!$NoRes) { $script:NW_Result = $Results }
}
}
New-Alias -Name "grep" -Value "Invoke-Grep" -Option 'AllScope' -Force