The other day whe had a stressfull moment. We noticed that one of our passwords was visibile in clear in one of our log files. We new that one of our scripts was responsible for that. But whe only had one question : “Which one ?”.
We realized that whe had sooo many scripts, that going through all of them would take for FOREVER ! I wrote recurse script that would find a specefic occurence in any text type of file (which means any script file actually).
find the script below :
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
|
function find-occurence {
<#
.SYNOPSIS
Searches for a specific occurence in any type of text file.
.DESCRIPTION
will read and search a specific occurence in any of the following extensions if found: “.ini”,“.txt”,“.vbs”,“.cmd”,“.xml”,“.inf”,“.bat”,“.wsf”,“.ps1”,“.psm1”,“.log”
.PARAMETER Recurse
If this parameter is specified, the function will search recursivly for the specified text.
.PARAMETER Verbose
Launches the script in verbose mode.
.EXAMPLE
find-occurence -Path “C:tempScripts” -Pattern “UserPassword” -verbose
searches for the pattern “UserPassword” in the C:tempscripts folder in verbose mode.
.EXAMPLE
find-occurence -Path “C:tempScripts” -Pattern “UserPassword” -recurse -verbose
searches for the pattern “UserPassword” recursivley in the C:tempscripts folder in verbose mode.
.INPUTS
System.String,System.Int32
.OUTPUTS
System.String
.NOTES
-Author: Stéphane van Gulick
-Email :
-CreationDate: 08/05/2014
-LastModifiedDate: 08/05/2014
-Version: 0.2
-History:
08/05/2014:Added comment based help : SVG
.LINK
www.powershelldistrict.com
#>
[CmdletBinding()]
Param(
$Pattern,
$Path,
[switch]$Recurse
)
$Items = Get-ChildItem -Path $Path
foreach ($Item in $Items){
if (Test-Path $item.FullName -PathType Container) {
if ($PSBoundParameters.ContainsKey(“recurse”)){
#Calling the function recursively
Write-verbose “Going one level deeper. Recursive call to $($item.FullName)”
if ($PSBoundParameters.ContainsKey(“verbose”)){
Find-occurence -Path $item.FullName -Pattern $Pattern -recurse -Verbose
}else{
Find-occurence -Path $item.FullName -Pattern $Pattern -recurse
}
}
}
else
{
$extension = $null
$extension = (Get-Item $Item.FullName).extension
if (“.ini”,“.txt”,“.vbs”,“.cmd”,“.xml”,“.inf”,“.bat”,“.wsf”,“.ps1”,“.psm1”,“.log” -eq $extension ){
write-verbose “Searching for occurence in file $($item.fullname) with extension $($extension) “
Select-String -Pattern $pattern -Path $item.FullName
}else{
if ($PSBoundParameters.ContainsKey(“verbose”)){
Write-Host “The file $($item.fullname) is not a readable text file.” -ForegroundColor ‘DarkCyan’
}
}
}
}
}
|
The function can be called as followed in order to find any specefic word in a text file whitin a hierarchy.
1
|
find-occurence -Path “C:tempScripts” -Pattern “UserPassword” -recurse -verbose
|
Leave A Comment