Extract substring and make it a variable

This topic contains 5 replies, has 5 voices, and was last updated by  Adam Hoge 1 year ago.

  • Author
  • #70417

    Adam Hoge

    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.


    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

    Don Jones

    ...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

      Adam Hoge

      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

    Rob Simmers

    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



  • #70439

    Jeffery Hayes

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


You must be logged in to reply to this topic.