Count the lines a text file and email if it more than 1000 lines

Welcome Forums General PowerShell Q&A Count the lines a text file and email if it more than 1000 lines

Viewing 4 reply threads
  • Author
    Posts
    • #215418
      Participant
      Topics: 42
      Replies: 42
      Points: 38
      Rank: Member

      Hi,

      I wrote a query which will get an output to text file. Below is the query:

      psexec \\servername netstat -aon | findstr toe >> \\servernam1\c$\temp\output\a1.log

      Now, I need to count the lines in the log file, if it more than 1000 lines email. I tried few options, but could not achieve email part. Below is the script:

       

      read -r num < a1.txt
      if [[ “$num” -gt 1000 ]]; then
      echo “Take action!”
      else
      echo “All OK!”
      fi

       

      Can some help me out at this?

    • #215436
      Participant
      Topics: 12
      Replies: 519
      Points: 1,194
      Helping Hand
      Rank: Community Hero

      I thought this was a PowerShell forum lol

    • #215439
      Participant
      Topics: 42
      Replies: 42
      Points: 38
      Rank: Member

      Ok.
      I wrote a bit powershell command
      Get-Content \\servernam1\c$\temp\output\a1.log | Measure-Object –Line | Out-File \\servernam1\c$\temp\output\a2.log

      a2.log contains more than 1000 lines. Now, I need to get an email if it is equal or grater than 1000 lines.

    • #215454
      Participant
      Topics: 0
      Replies: 6
      Points: 83
      Rank: Member

      I would suggest the following:

      $Lines = (Get-Content \\servernam1\c$\temp\output\a1.log | Measure-Object –Line).Lines
      if ($Lines -ge 1000) {
          # email code goes here
      }
    • #215904
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      You can just use Count, Get-Content is an array of lines:

      @(Get-Content -Path C:\Scripts\file1.txt).Count
      

      Look at Send-MailMessage to send an email.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.