Extract substring and make it a variable

This topic contains 5 replies, has 5 voices, and was last updated by Profile photo of Adam Hoge Adam Hoge 2 months, 1 week ago.

  • Author
    Posts
  • #70417
    Profile photo of Adam Hoge
    Adam Hoge
    Participant

    I'm all alone in a group where I'm becoming a one man devops crew and working on the start of some logging/monitoring scripts that I need some feedback on, so I'll get right to it.

    I am doing log analysis and need to extract some information out of each log file. In this world there are 200 nodes. Each node has several subdirectories (Days), and each day has multiple log files depending on how many times they booted.

    \Node001\day001\log001\Logfile.log

    I'm starting with one log file and I will scale up from that. I have a start, that works but I'm pretty sure I'm going at this the wrong way.

    I'm looking to the group to get some better suggestions on how to do this.

  • #70418
    Profile photo of Don Jones
    Don Jones
    Keymaster

    ...to do what?

    You can use the Substring method (https://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx) of a string.

    $sub = $mystring.substring(2,4)

    But I'm not sure that's what you're asking.

    • #70498
      Profile photo of Adam Hoge
      Adam Hoge
      Participant

      First, I want to say thank you to Don for all of the powershell material you have put out. The videos and books have gotten me to this point and I really appreciate all of your time and effort. I'm sure that when I actually get time to actually read the entire lunches book, and the Dev ops manifesto

      My apologies that my post was so vauge and missing the GIST that I thought I attached, however after reading all of this, I am working on a new version.

      Here is the old stuff..

  • #70435
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    If you are looking on how to process these logs, you should look at Get-ChildItem and Get-Content to recurse the directory structure and open and parse the logs.

    #Assuming C:\Nodes is the root of the logs
    foreach ($file in (Get-ChildItem -Path C:\Nodes -Include *.log -File -Recurse)) {
        "Processing log file {0}" -f $file
        foreach ($line in Get-Content $file.FullName) {
            #Do something in each log
        }
    }
    
  • #70436
    Profile photo of Hannah
    Hannah
    Participant

    cool

  • #70439
    Profile photo of Jeffery Hayes
    Jeffery Hayes
    Participant

    Another option is using findstr and filter each log for a select text.

    https://technet.microsoft.com/en-us/library/bb490907.aspx

You must be logged in to reply to this topic.