Files
memo_powershell/parseLog.ps1
CodyZhang 4774203e76 20200602
2020-06-02 18:42:29 +08:00

165 lines
4.9 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<#==========================
Created Date : 2020-04-22
Author : Yaoyuan
Memo : 在很大的Log檔中取出所需的資訊
==========================#>
<# 設定日期區間 #>
# 開始時間
$startDate = Get-Date "2020-05-13"
# 結束時間
$endDate = Get-Date "2020-05-15"
# 間隔
$daysToSkip = 1
<# 如果Log檔是在多個資料夾可以先設一個Array然後在之後取得檔名的時候代入 #>
$folders = "PAYVENDERAPI01P", "PAYVENDERAPI02P", "PAYVENDERAPI07P"
<# 設定要將結果寫入的檔案 #>
# 設定檔名 (可以連路徑一起設)
$okFile = "success.txt"
# 如果檔案已經存在,先刪除
if (Test-Path $okFile)
{
Remove-Item $okFile
}
# 建位新檔
New-Item $okFile -ItemType File
$errFile = "fail.txt"
if (Test-Path $errFile)
{
Remove-Item $errFile
}
New-Item $errFile -ItemType File
# 如果開始時間小於或等於結束時間,就執行
while ($startDate -le $endDate) {
# 取得日期的字串
$dateString = $startDate.ToString("yyyy-MM-dd")
# 多個不同資料夾
foreach($folder in $folders)
{
# 建新檔名
$file = "\\paylog01p\Logs\$($folder)\LogsTemp\$($dateString)\VenderAPI.log"
# 要找的字串
# 在這是 JCICP40Query
# Select-String 會把有關鍵字那一串全部輸出成 MatchInfo
# 如果有多筆的話,就會是 List<MatchInfo>
$matches = Select-String $file -Pattern JCICP40Query
# 因為每次的查詢都會有一個SessionID每個查詢會有多筆Log紀錄
# 所以用lastSessionID來判斷是不是要換下一筆的紀錄
$lastSessionID = ""
foreach($line in $matches)
{
# 將MatchInfo轉成String
$strLine = $line.ToString()
<# step 1: 取得 Session ID #>
# 用 regular expression 來處理字串
$strLine -match 'Ѯ(.{32})Ѯ'
# 如果上一行 regex match 的結果是 true就可以用 $matches[] 來取得所需的字串
# [0]是全部符合的字串,[n]是第n個group字串
$sessionID = $matches[1]
<# step 2: 用 Session ID 檢查是不是同一個查詢 #>
# 因為這次要查詢的log不會多個不用 Session ID 穿插,所以可以單線循序來取得
if($sessionID -ne $lastSessionID)
{
<# step 7: 同一個 Session ID 結束而且不是第一筆在這裡幾多筆log的資料組合好寫入檔案 #>
if(![string]::IsNullOrEmpty($lastSessionID))
{
# 要寫入的檔案
$fileName = $errFile
if([string]::IsNullOrEmpty($errMsg))
{
$fileName = $okFile
}
# 寫入檔案
Add-Content $fileName "============ $datetime $folder =============="
Add-Content $fileName "SessionID: $sessionID"
Add-Content $fileName "----"
Add-Content $fileName "timer: $timer"
Add-Content $fileName "----"
Add-Content $fileName "postData: $postData"
Add-Content $fileName "----"
Add-Content $fileName "result: $result"
Add-Content $fileName "----"
Add-Content $fileName "errMsg: $errMsg"
Add-Content $fileName ""
Add-Content $fileName ""
}
# 上一個 Session ID 的資料都處理完了,開始下一筆資料
$lastSessionID = $sessionID
# 清空上一筆的資料
$postData = ""
$result = ""
$errMsg = ""
$datetime = ""
# step 3: 取得 Post 的資料
$strLine -match 'Post參數「(.+)」'
$postData = $matches[1]
# step 4: 取得 datetime
$strLine -match "\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{4}"
$datetime = $matches[0]
}
else
{
#step 5: 取得 result message
if($strLine -match '\[restJCICP40QueryResp\]:(.+)')
{
$result = $matches[1]
}
#step 6: 取得 error message
if($strLine -match '\[restJCICP40QueryResp.Errmsg\]:(.+)$')
{
$errMsg = $matches[1]
}
#step 7: 取得 timer
if($strLine -match 'Timer: \*(.+)\*')
{
$timer = $matches[1]
}
}
}
}
# 下一個日期的 Log
$startDate = $startDate.AddDays($daysToSkip)
}