{"id":682,"date":"2023-08-09T12:47:00","date_gmt":"2023-08-09T12:47:00","guid":{"rendered":"http:\/\/draith.com\/?p=682"},"modified":"2024-03-07T00:54:43","modified_gmt":"2024-03-07T00:54:43","slug":"couple-of-logs-analyzing-function","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=682","title":{"rendered":"Couple of Logs Analyzing Function"},"content":{"rendered":"\n<p>Heya all &#8211; here are a couple of quick functions to help analyze logs files.  Coming from a ConfigMgr\/SCCM background, I got used to reading a LOT of logs.  Having a couple of functions like this would have greatly helped!<\/p>\n\n\n\n<p>First &#8211; let&#8217;s see if there are warning and\/or error messages in a log (or stack of logs)<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">function Analyze-LogContent {\n    [CmdletBinding()]\n    param (\n        [Parameter(Mandatory=$true)]\n        [string]$LogFilePath,\n\n        [string]$ErrorPattern = 'ERROR|Error|error',\n        [string]$WarningPattern = 'WARNING|Warning|warning'\n    )\n\n    if (-not (Test-Path -Path $LogFilePath)) {\n        Write-Error &quot;Log file does not exist at the path: $LogFilePath&quot;\n        return\n    }\n\n    # Reading the log file\n    $logContent = Get-Content -Path $LogFilePath\n\n    # Analyzing for errors\n    $errors = $logContent | Where-Object { $_ -match $ErrorPattern }\n    $warnings = $logContent | Where-Object { $_ -match $WarningPattern }\n\n    # Output analysis\n    $output = @()\n    if ($errors.Count -gt 0) {\n        $output += &quot;Found $($errors.Count) errors in the log.&quot;\n    } else {\n        $output += &quot;No errors found in the log.&quot;\n    }\n\n    if ($warnings.Count -gt 0) {\n        $output += &quot;Found $($warnings.Count) warnings in the log.&quot;\n    } else {\n        $output += &quot;No warnings found in the log.&quot;\n    }\n\n    return $output\n}\n\n# Example usage\n$logPath = &quot;C:\\Path\\To\\Your\\LogFile.log&quot;\n$result = Analyze-LogContent -LogFilePath $logPath\n$result | ForEach-Object { Write-Host $_ }\n<\/pre><\/div>\n\n\n\n<p>Change the patterns as necessary &#8211; ERR, for example.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The second function is pretty straight forward &#8211; summarize a log counting the number of INFO, Warning, and Error messages: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">function Summarize-LogFile {\n    [CmdletBinding()]\n    param (\n        [Parameter(Mandatory=$true)]\n        [string]$LogFilePath\n    )\n\n    if (-not (Test-Path -Path $LogFilePath)) {\n        Write-Error &quot;Log file does not exist at the path: $LogFilePath&quot;\n        return\n    }\n\n    $logContent = Get-Content -Path $LogFilePath\n\n    $infoCount = 0\n    $errorCount = 0\n    $warningCount = 0\n\n    foreach ($line in $logContent) {\n        switch -Regex ($line) {\n            &quot;INFO&quot; { $infoCount++ }\n            &quot;ERROR&quot; { $errorCount++ }\n            &quot;WARNING&quot; { $warningCount++ }\n        }\n    }\n\n    $summary = @&quot;\nLog File Summary:\nInfo Entries: $infoCount\nError Entries: $errorCount\nWarning Entries: $warningCount\nTotal Entries: $($logContent.Count)\n&quot;@\n\n    return $summary\n}\n\n# Example usage\n$logPath = &quot;C:\\Path\\To\\Your\\LogFile.log&quot;\n$summary = Summarize-LogFile -LogFilePath $logPath\nWrite-Host $summary\n<\/pre><\/div>\n\n\n\n<p>There ya go!  I will keep adding to these, and eventually get them in Github so you all can tell me how wrong they are \ud83d\ude42<\/p>\n\n\n\n<p>Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Heya all &#8211; here are a couple of quick functions to help analyze logs files. Coming from a ConfigMgr\/SCCM background, I got used to reading a LOT of logs. Having a couple of functions like this would have greatly helped! First &#8211; let&#8217;s see if there are warning and\/or error messages in a log (or &hellip; <a href=\"https:\/\/draith.com\/?p=682\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Couple of Logs Analyzing Function&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[16],"class_list":["post-682","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=682"}],"version-history":[{"count":1,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/682\/revisions"}],"predecessor-version":[{"id":683,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/682\/revisions\/683"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}