Powershell CSV to text match values

Answered Powershell CSV to text match values

  • Monday, February 04, 2013 3:02 PM
     
     

    I have a csv file with 2 columns services and severs, it looks something like this except its over a thousand rows and some 300+ values for the service file

    Service     Servers

    svc1        svr2

    svc1        svr3

    svc2        svr1

    svc2          svr5

    svc2        svr3

    I need to get it to give me a text output similar to this

    ( ('services', 'svc1', {}), ['/' + FOLDER_PATH + '/+'], ['svr2','svr3'] ),

    ( ('services', 'svc2r', {}), ['/' + FOLDER_PATH + '/+'], ['svr1',svr5','svr3'] ),

    Can powershell help me with this? I have used import-csv before but never to get to a text manipulation like this.

All Replies

  • Monday, February 04, 2013 3:13 PM
    Moderator
     
     

    Why do you need that kind of output?

    Bill

  • Monday, February 04, 2013 3:31 PM
     
     

    Yeah, thats a good question. I need that text rile to feed into a new network monitoring system I am bringing up using Check_MK. It's very nice, It's an overlay to nagios. As we add in 100's of items I am trying to find a way to fastpass the entry in a gui and get it straight in as text output. I should note in my sample the output file 

     ('services', 'svc2r', {}), ['/' + FOLDER_PATH + '/+'], ['svr1',svr5','svr3'] ),  

    The word service is not derived from the csv but is a static text.

  • Monday, February 04, 2013 4:02 PM
    Moderator
     
      Has Code

    Hi,

    You can use group-object to group your input CSV by services and then get the list of servers associated with each service name in the grouping. For example, based on your CSV sample:


    import-csv "input.csv" | group-object Service | foreach-object {
      $output = new-object PSObject
      $output | add-member NoteProperty Service $_.Name
      $servers = $_.Group | foreach-object { $_.Servers }
      $output | add-member NoteProperty Servers $servers
      $output
    }
    

    The above code results in the following output:


    Service  Servers
    -------  -------           
    svc1     {svr2, svr3}      
    svc2     {svr1, svr5, svr3}
    

    You can use this technique to output a separate line of text in the way that you need.

    Bill

  • Monday, February 04, 2013 5:45 PM
    Moderator
     
     Answered Has Code

    Here's a complete example of what I think you're looking for:


    import-csv "t0.csv" | group-object Service | foreach-object {
      $servers = $_.Group | foreach-object { $_.Servers }
      $serverList = ""
      $servers | foreach-object {
        if ( $serverList -eq "" ) {
          $serverList += "'{0}'" -f $_
        } else {
          $serverList += ",'{0}'" -f $_
        }
      }
      "( ('services', '{0}', {{}}), ['/' + FOLDER_PATH + '/+'], [{1}] )," -f $_.Name, $serverList
    }
    

    Output:


    ( ('services', 'svc1', {}), ['/' + FOLDER_PATH + '/+'], ['svr2','svr3'] ),
    ( ('services', 'svc2', {}), ['/' + FOLDER_PATH + '/+'], ['svr1','svr5','svr3'] ),
    

    This code uses the .NET string format operator (-f) to insert the server and service names into the correct positions in their strings. Note that when you use -f you have to write { for {{ and } for }}.

    Bill



  • Monday, February 04, 2013 5:57 PM
     
     
    Wow, awesome! Thanks so much, you just saved some other guys a LOT of clicking