Note that there are some explanatory texts on larger screens.

plurals
  1. POPowerShell Select-String to look for latest log-file entries
    text
    copied!<p>Let me be brief here:</p> <p>I have a couple of machines in AD which I need to check frequently. I am the newcomer to PowerShell and looking for a working way to make it happen. </p> <p>What needs to be checked? Certain file on remote system, lets call it File1.log (its name is the same in every machine). The file contains numerous lines as follows:</p> <p>15.10.2013 23:38:54|FA OK 98.9 USD</p> <p>I need to know how many entries like above for past 2 hours or 24 hours depending on the time of check. ("FA OK" as expression works fine) and get the amount on the last line.</p> <p>What I already managed to do: Using $checktime2 = (Get-Date).AddHours(-2).ToString("dd.MM.yyyy HH") [to match the formating]. The script just generates empty file, despite correct entries in the test file... The code looks awful, but I had no other idea how to check for different hours. It seems to me that AddHours() doesn't accept the variable in brackets or I just don't know how to set it up. Question here - Google advised me that Select-String could accept multiple different values (with OR, not AND logical operator). I am not sure what is wrong here and how to move on. Script works and doesn't report errors. Any feedback on how to make this script work or at least clean it up are very welcome. Thanks in advance!</p> <pre><code>$title = "Check" $message = "2 or 24 hours back?" $checktime0 = (Get-Date).AddHours(0).ToString("dd.MM.yyyy HH") $checktime1 = (Get-Date).AddHours(-1).ToString("dd.MM.yyyy HH") $checktime2 = (Get-Date).AddHours(-2).ToString("dd.MM.yyyy HH") $checktime3 = (Get-Date).AddHours(-3).ToString("dd.MM.yyyy HH") $checktime4 = (Get-Date).AddHours(-4).ToString("dd.MM.yyyy HH") $checktime5 = (Get-Date).AddHours(-5).ToString("dd.MM.yyyy HH") $checktime6 = (Get-Date).AddHours(-6).ToString("dd.MM.yyyy HH") $checktime7 = (Get-Date).AddHours(-7).ToString("dd.MM.yyyy HH") $checktime8 = (Get-Date).AddHours(-8).ToString("dd.MM.yyyy HH") $checktime9 = (Get-Date).AddHours(-9).ToString("dd.MM.yyyy HH") $checktime10 = (Get-Date).AddHours(-10).ToString("dd.MM.yyyy HH") $checktime11 = (Get-Date).AddHours(-11).ToString("dd.MM.yyyy HH") $checktime12 = (Get-Date).AddHours(-12).ToString("dd.MM.yyyy HH") $checktime13 = (Get-Date).AddHours(-13).ToString("dd.MM.yyyy HH") $checktime14 = (Get-Date).AddHours(-14).ToString("dd.MM.yyyy HH") $checktime15 = (Get-Date).AddHours(-15).ToString("dd.MM.yyyy HH") $checktime16 = (Get-Date).AddHours(-16).ToString("dd.MM.yyyy HH") $checktime17 = (Get-Date).AddHours(-17).ToString("dd.MM.yyyy HH") $checktime18 = (Get-Date).AddHours(-18).ToString("dd.MM.yyyy HH") $checktime19 = (Get-Date).AddHours(-19).ToString("dd.MM.yyyy HH") $checktime20 = (Get-Date).AddHours(-20).ToString("dd.MM.yyyy HH") $checktime21 = (Get-Date).AddHours(-21).ToString("dd.MM.yyyy HH") $checktime22 = (Get-Date).AddHours(-22).ToString("dd.MM.yyyy HH") $checktime23 = (Get-Date).AddHours(-23).ToString("dd.MM.yyyy HH") $checktime24 = (Get-Date).AddHours(-24).ToString("dd.MM.yyyy HH") $2 = New-Object System.Management.Automation.Host.ChoiceDescription "&amp;A", ` "Check for 2 hours back" $24 = New-Object System.Management.Automation.Host.ChoiceDescription "&amp;B", ` "Check for 24 hours back" $options = [System.Management.Automation.Host.ChoiceDescription[]]($2, $24) $result = $host.ui.PromptForChoice($title, $message, $options, 0) switch ($result){ 0 {Get-Content File1.log | Select-String '($checktime2|$checktime1|$checktime0)' | Select-String "FA OK" | measure-object -line | out-file OPTcheck.txt -Append Get-Content File1.xjou | Select-String '($checktime2|$checktime1|$checktime0)' | Select-String "FA OK" | select -Last 1 | out-file OPTcheck.txt -Append } 1 {Get-Content File1.log| Select-String '($checktime24|$checktime23|$checktime22|$checktime21|$checktime20|$checktime19|$checktime18|$checktime17|$checktime16|$checktime15|$checktime14|$checktime13|$checktime12|$checktime11|$checktime10|$checktime9|$checktime8|$checktime7|$checktime6|$checktime5|$checktime4|$checktime3|$checktime2|$checktime1|$checktime0)' | Select-String "FA OK" | measure-object -line | out-file OPTcheck.txt -Append Get-Content File1.log | Select-String $checktime24 | Select-String "FA OK" | select -Last 1 | out-file OPTcheck.txt -Append } default {Get-Credential} //used to catch the error in switch } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload