-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyze-LogFile.ps1
254 lines (192 loc) · 7.14 KB
/
Analyze-LogFile.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
242
243
244
245
246
247
248
#
# Analyze_LogFile.ps1
#
<#
.SYNOPSIS
Analyze_LogFile
.DESCRIPTION
This function will seach the log files base on the keyword.
.EXAMPLE
Analyze_LogFile
.EXAMPLE
Analyze_LogFile -FilePath "C:\Test\" -KeyWord "txt"
.NOTES
Please keep above information
#>
function Analyze_LogFile {
[CmdletBinding()]
param (
[Parameter(HelpMessage="Enter Log File Path, eg C:\Logfiles\*.txt")][string]$FilePath="C:\Logfiles\SA\*.txt",
[Parameter(HelpMessage="Enter KeyWord Search through the files, eg Error:")][string]$KeyWord="Error|"
)
Select-String -Path $FilePath -Pattern $KeyWord
}
<#
.SYNOPSIS
Analyze_LogFile_Daily
.DESCRIPTION
This function will seach the log files base on the keyword.
.EXAMPLE
Analyze_LogFile_Daily
.EXAMPLE
Analyze_LogFile_Daily -FilePath "C:\Test\" -KeyWord "*|Error|"
.NOTES
Please keep above information
#>
function Analyze_LogFile_Daily {
[CmdletBinding()]
param (
[Parameter(HelpMessage="Enter Log File Path, eg C:\Logfiles\*.txt")][string]$RootPath="C:\Logfiles\SA\",
[Parameter(HelpMessage="Enter KeyWord Search through the files, eg Error:")][string]$KeyWord="|Error|"
)
$date = get-date
$resultFileName = "result"+ $date.ToString("yyyyMMdd")+".txt"
Write-Host ($RootPath + $resultFileName)
Write-Host $resultFileName
if(!(Test-Path ($RootPath+$resultFileName)))
{
new-item -ItemType file -Path ($RootPath+$resultFileName)
}
Write-Host ($RootPath+$resultFileName)
$logpath = $RootPath+"*log"+ $date.ToString("yyyyMMdd")+".txt"
Clear-Content ($RootPath+$resultFileName)
Get-Content $logpath | Where-Object { $_.Contains($KeyWord) }|Set-Content ($RootPath+$resultFileName)
}
#
#Parse and Analyze LogFiles V 0.2
# *.txt as the source format
# Usage: Analyze_LogFile_V2 | Export-Csv C:\LogFiles\2.csv
function Analyze_LogFile_V2 {
[CmdletBinding()]
param (
[Parameter(HelpMessage="Enter Log File Path, eg C:\Logfiles\*.txt")][string]$DirPath="C:\Logfiles\SA\",
[Parameter(HelpMessage="Enter Log File Path, eg C:\Logfiles\*.txt")][string]$FilePattern="*.txt",
[Parameter(HelpMessage="Enter KeyWord Search through the files, eg Error:")][string]$KeyWord="*|Error|*"
)
if(Test-Path $DirPath)
{
foreach($file in Get-ChildItem -Filter $FilePattern $DirPath )
{
#variale for the file contents
$filetext= Get-Content $file.FullName
Write-Host $file.FullName
#Loop through each line for the file
for ($i = 1; $i -lt $filetext.Length; $i++)
{
if(($filetext[$i] -like $KeyWord))
{
$output= New-Object psobject
$output | Add-Member NoteProperty "FileName" $file.Name
$output | Add-Member NoteProperty "Date" $filetext[$i].Split("|")[0]
$output | Add-Member NoteProperty "Module" $filetext[$i].Split("|")[1]
$output | Add-Member NoteProperty "ErrorMessage" $filetext[$i].Split("|")[2]
#Write-Host $i
#$output | Export-Csv C:\Logfiles\1.csv
$output
}
}
}
}
}
#Analyze the Service Agent DEV server's Log
Analyze_LogFile_V2 -DirPath "\\D9W0602G.houston.hp.com\Logfiles\SA\" |Export-Csv C:\Logfiles\QA_Error_Result.csv
#Analyze the my local's Log
Analyze_LogFile_V2 |Export-Csv C:\Logfiles\Local_Error_Result.csv
#
#Parse and Analyze LogFiles V 0.3
#Include the progress of the parsing.
# *.txt as the source format
# Usage: Analyze_LogFile_V3 | Export-Csv C:\LogFiles\2.csv
function Analyze_LogFile_V3 {
[CmdletBinding()]
param (
[Parameter(HelpMessage="Enter Log File Path, eg C:\Logfiles\*.txt")][string]$DirPath="C:\Logfiles\SA\",
[Parameter(HelpMessage="Enter Log File Path, eg C:\Logfiles\*.txt")][string]$FilePattern="*.txt",
[Parameter(HelpMessage="Enter KeyWord Search through the files, eg Error:")][string]$KeyWord="*|Error|*"
)
if(Test-Path $DirPath)
{
#Variable for file count progress
$filecounter = 0
#Variable for count of files to process
$filecount = (get-Childitem -filter "*.txt" $DirPath | Where {$_.psIsContainer -eq $false}).Count
foreach($file in Get-ChildItem -Filter $FilePattern $DirPath | Where {$_.psIsContainer -eq $false})
{
#variale for the file contents
$filetext= Get-Content $file.FullName
Write-Host $file.FullName
#Loop through each line for the file
for ($i = 1; $i -lt $filetext.Length; $i++)
{
if(($filetext[$i] -like $KeyWord))
{
$output= New-Object psobject
$output | Add-Member NoteProperty "FileName" $file.Name
$output | Add-Member NoteProperty "Date" $filetext[$i].Split("|")[0]
$output | Add-Member NoteProperty "Module" $filetext[$i].Split("|")[1]
$output | Add-Member NoteProperty "ErrorMessage" $filetext[$i].Split("|")[2]
#Write-Host $i
#$output | Export-Csv C:\Logfiles\1.csv
$output
}
#Display progress bar to show progress
$filesprocessed = "$filecounter of $filecount"
Write-Progress -Id 1 -activity "Parsing files" -status "Files parsed: $filesprocessed" -percentComplete (($filecounter / $filecount) * 100)
}
#Increment file counter used in progress bar
$filecounter++
}
}
}
Analyze_LogFile_V3 | Export-Csv C:\LogFiles\2.csv
#
#Parse and Analyze log file in xml format.
# *.xml as the source format
# Usage: Analyze_XML_Log_File_V1 | Export-Csv C:\LogFiles\2.csv
function Analyze_XML_Log_File_V1 {
[CmdletBinding()]
param (
[Parameter(HelpMessage="Enter Log File Path, eg C:\Logfiles\*.txt")][string]$DirPath="C:\Data\LNGitProject\LN-PS-Functions\",
[Parameter(HelpMessage="Enter Log File Path, eg C:\Logfiles\*.txt")][string]$FilePattern="*.xml",
[Parameter(HelpMessage="Enter KeyWord Search through the files, eg Error:")][string]$KeyWord="The physical drive has failed."
)
if(Test-Path $DirPath)
{
foreach($file in Get-ChildItem -Filter $FilePattern $DirPath | Where {$_.psIsContainer -eq $false} )
{
#Create new XML object
[System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument
#Load XML file
$xd.load($file.FullName)
#Get Physical Drives
$drives = $xd.SelectNodes("//Device") | Where {$_.devicetype -eq "PhysicalDrive"}
#Loop through each drive
foreach ($drive in $drives)
{
#Create PowerShell object to hold data
$Output = New-Object psobject
#Add filename member to PowerShell object
$Output | add-member NoteProperty "Filename" $file.FullName
#Get device type and add to PowerShell Object
$Output | add-member NoteProperty "Devicetype" $drive.devicetype
#Get drive ID and add to PowerShell Object
$Output | add-member NoteProperty "Drive ID" $drive.id
#Get marketing name and add to PowerShell Object
$Output | add-member NoteProperty "MarketingName" ($drive.marketingName).ToString().ToUpper()
#check if drive logged a failure
$driveerror = ($drive.errors).message | where {$_.message -eq $KeyWord}
if ($driveerror.message -match $KeyWord)
{
$Output | add-member NoteProperty "Marked as Failed" "Yes"
}
else
{
$Output | add-member NoteProperty "Marked as Failed" "No"
}
#Output the PowerShell object data
$Output
}
}
}
}
Analyze_XML_Log_File_V1