{"id":1,"date":"2019-04-22T18:20:34","date_gmt":"2019-04-22T16:20:34","guid":{"rendered":"http:\/\/sys-code-alpha.com\/?p=1"},"modified":"2019-04-23T22:11:50","modified_gmt":"2019-04-23T20:11:50","slug":"bonjour-tout-le-monde","status":"publish","type":"post","link":"https:\/\/sys-code-alpha.com\/?p=1","title":{"rendered":"Highlight lines with a matching pattern when looking  at logfiles in PowerShell\u00a0console."},"content":{"rendered":"\n<p>I saw a question on how to highlight certain lines of text when viewing logfiles in PowerShell console. It took me a few minutes to produce a function that does the job:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"851\" height=\"589\" src=\"http:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/Capture.jpg\" alt=\"\" class=\"wp-image-10\" srcset=\"https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/Capture.jpg 851w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/Capture-300x208.jpg 300w, https:\/\/sys-code-alpha.com\/wp-content\/uploads\/2019\/04\/Capture-768x532.jpg 768w\" sizes=\"(max-width: 851px) 100vw, 851px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function Find-String {           \n        &lt;#\n        .SYNOPSIS\n        Finds given pattern and highlights each line that it contains.\n\n        .DESCRIPTION\n        Finds given pattern and highlights each line that it contains.\n        By default the lines are highlighted in green.\n        Accepts any input as it converts objects to strings on the fly.\n        This functuion should rather be used as a last step for text file ad-hoc investigation.\n        You cannot effectively pipe the output any further.\n\n        .EXAMPLE\n        This example shows coloured process list:\n        get-process | Find-String -Pattern svchost -Colour magenta\n\n        .EXAMPLE\n        This example shows coloured process list:\n        Get-Content application1.log | Find-String -Pattern error,warning -Colour red\n\n        .PARAMETER Pattern\n        Specify a pattern to highlight.\n                                   \n        .PARAMETER Colour\n        Specify a highlight colour.\n        #> \n\n        [CmdletBinding()]\n        Param(\n              [Parameter( Mandatory=$True, ValueFromPipeline=$true) ]\n              [object[]]$Stream,\n              [Parameter( Mandatory=$True) ]\n              [string[]]$Pattern,\n              [Parameter( Mandatory=$False) ]\n              $Colour = \"Green\"\n              )\n\n              BEGIN{\n                   $out = @()\n                   $pat = $pattern -join (\"|\")\n                   write-verbose \"Pattern applied: $pat\"\n                   }\n              PROCESS{  \n                      $out += $stream \n                      }                                             \n              END{                   \n                  ($out | out-string) -split \"\\n\" | ForEach-Object{ IF($_ -match \"$Pat\"){Write-Host -ForegroundColor $colour  $_}ELSE{Write-Host -ForegroundColor \"white\" $_}}  \n                 }\n\n    }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I saw a question on how to highlight certain lines of text when viewing logfiles in PowerShell console. It took me a few minutes to produce a function that does the job:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/1"}],"collection":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1"}],"version-history":[{"count":9,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":56,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions\/56"}],"wp:attachment":[{"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sys-code-alpha.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}