-
Notifications
You must be signed in to change notification settings - Fork 13
/
Get-ValidIPAddressFromString.ps1
106 lines (69 loc) · 2.66 KB
/
Get-ValidIPAddressFromString.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
<#
.SYNOPSIS
This cmdlet is used to extract all of the unique IPv4 addresses out from each line of a log file
.DESCRIPTION
Use a ForEach type statement to extract unique IPv4 address out from each line of a log file
.PARAMETER String
Defines the string of text that the regular expression of an IPv4 address should be tested for
.PARAMETER Path
Defines the path to a file you want to grab unique IP addresses out out
.EXAMPLE
ForEach ($Line in (Get-Content -Path C:\Temp\firewall.log)) { Get-ValidIPAddressFromString -String $Line }
# This example parses the text file firewall.log and lists any IPv4 Addresses found on each line
.EXAMPLE
Get-ValidIpAddressFromString -Path C:\Windows\System32\LogFiles\Firewall\domainfw.log
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.INPUTS
System.String
.OUTPUTS
System.String
#>
Function Get-ValidIPAddressFromString {
[CmdletBinding(DefaultParameterSetName="Line")]
param(
[Parameter(
ParameterSetName="Line",
Position=0,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$False,
HelpMessage="`n[H] Enter a string to extract the IPv4 address out of `n[E] EXAMPLE: Log File 8/6/2020 10.10.10.10. DENY TCP")] # End Parameter
[String]$String,
[Parameter(
ParameterSetName="File",
Mandatory=$True,
ValueFromPipeline=$False)] # End Parameter
[String]$Path) # End param
$Obj = @()
$Regex=‘(?<Address>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))’
Switch ($PsCmdlet.ParameterSetName) {
'File' {
$FileContents = Get-Content -Path $Path -Tail 5000
ForEach ($Line in $FileContents) {
If (($Line -Match $Regex) -and ($Obj -notcontains $Matches.Address)) {
$Obj += $Matches.Address
} # End If
} # End ForEach
Return $Obj
} # End File Switch
'Line' {
If ($String -Match $Regex) {
$Obj = $Matches.Address
} # End If
$Obj
} # End Default Switch
} # End Switch
} # End Function Get-ValidIPAddressFromString