commit db4ba7274f8388651ee428cebb096fb5cd181405 Author: CodyZhang Date: Thu Apr 23 14:46:53 2020 +0800 log查詢 diff --git a/parseLog.ps1 b/parseLog.ps1 new file mode 100644 index 0000000..cade6bd --- /dev/null +++ b/parseLog.ps1 @@ -0,0 +1,164 @@ +<#========================== +Created Date : 2020-04-22 +Author : Yaoyuan +Memo : 在很大的Log檔中取出所需的資訊 +==========================#> + +<# 設定日期區間 #> +# 開始時間 +$startDate = Get-Date "2020-04-01" +# 結束時間 +$endDate = Get-Date "2020-04-21" +# 間隔 +$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 = "D:\APLog\$($folder)\LogsTemp\$($dateString)\VenderAPI.log" + + # 要找的字串 + # 在這是 JCICP40Query + # Select-String 會把有關鍵字那一串全部輸出成 MatchInfo + # 如果有多筆的話,就會是 List + $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) + +} + + + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..381d7ab --- /dev/null +++ b/readme.md @@ -0,0 +1,50 @@ +## PowerShell 常用的指令 + + +### Log 查詢 + +#### 顯示內容 +```powershell +Get-Content -Path C:\Windows\System32\LogFiles\HTTPERR\httperr1.log +``` + +#### 分頁顯示內容 +```powershell +Get-Content -Path C:\Windowns\System32\LogFiles\HTTPERR\httperr1.log | Out-Host -Paging +``` + +#### 顯示最後幾行 +```powershell +Get-Content -Path C:\Windows\Logs\DISM\dism.log -Tail 50 +``` + +#### 持續監控 +以下指令會先顯示最後5筆,然後持續監控檔案,只要一更新,馬上顯示。 +```powershell +Get-Content -Path C:\Windows\WindowsUpdate.log -Tail 5 -Wait +``` + + +#### 顯示符合條件的 log +以下會顯示最後20筆符合 Drop 的 log 行 +```powershell +Select-String -Path C:\Windows\System32\LogFiles\Firewall\pfirewall.log -Pattern 'Drop' | Select-Object -Last 20 +``` + +#### 多個條件 +```powershell +Select-String -Path C:\Windows\WindowsUpdate.log -Pattern 'error','warning' +``` + +#### 顯示連同符合條件的上下行 +以下指令會顯示符合條的的資料行和前面的0行,後面的3行 +```powershell +Select-String -Path C:\Windows\WindowsUpdate.log -Pattern 'error' -Conteext 0,3 +``` + + +#### 查看log檔特定行號間的紀錄 +以下查詢會顯示 log 檔從 30 到 45 行之間的資料。 +```powershell +Get-Content C:\Windows\debug\netlogon.log | Select-Object -First 30 -Skip 45 +``` \ No newline at end of file