get-content from .properties file, "xml" style

This topic contains 4 replies, has 2 voices, and was last updated by  Kevyn 1 week, 5 days ago.

  • Author
    Posts
  • #79411

    Burps
    Participant

    Hello I have a .properties file containing the following lines :

    env.servers.list.id=1 2
    
    env.server.1.type=STANDALONE
    env.server.1.host=serv1
    env.server.1.port=1234
    env.server.1.conntype=SOAP
    
    env.server.1.virtualhost=srv1.example.com
    env.server.1.servername=jvm1
    
    env.server.2.type=STANDALONE
    env.server.2.host=serv2
    env.server.2.port=5678
    env.server.2.conntype=SOAP
    
    env.server.2.virtualhost=srv2.example.com
    env.server.2.servername=jvm3
    

    I'd like to manipulate the data as objects.

    For now, I have this :

    $Configuration = (Get-Content "C:\tmp\test.properties" )  | ConvertFrom-StringData
    $Configuration 
    
    Name                           Value            
    ----                           -----            
    env.servers.list.id            1 2              
    env.server.1.type              STANDALONE       
    env.server.1.host              serv1            
    env.server.1.port              1234             
    env.server.1.conntype          SOAP             
    env.server.1.virtualhost       srv1.example.com 
    env.server.1.servername        jvm1             
    env.server.2.type              STANDALONE       
    env.server.2.host              serv2            
    env.server.2.port              5678             
    env.server.2.conntype          SOAP             
    env.server.2.virtualhost       srv2.example.com 
    env.server.2.servername        jvm3             
    
    

    Could you help me go further please ?

  • #79441

    Kevyn
    Participant

    What is the final output you're trying to accomplish? Based on the file data you've provided, can you draw it out for us?

  • #79504

    Burps
    Participant

    Hello

    I don't know yet how I'll go through this, but I would like to get something like

    foreach $serverlist # using id 1 and 2 in this case
    {
    Write-Host "Servername : $host; VirtualHost : $virtualhost
    }
    

    Thanks for your help

  • #79507

    Burps
    Participant

    Sorry, forgot the waited output :

    Servername : serv1; VirtualHost : srv1.example.com
    Servername : serv2; VirtualHost : srv2.example.com
    
    
  • #79654

    Kevyn
    Participant

    Using the original file data you posted, I found this to work. This code assumes that, in the data file, the ServerName & VirtualHost keys/values are always in pairs.

    env.server.1.virtualhost=srv1.example.com
    env.server.1.servername=jvm1
    ...
    env.server.2.virtualhost=srv2.example.com
    env.server.2.servername=jvm3

    $Configuration = (Get-Content "c:\data\file.txt" )  | ConvertFrom-StringData
    ForEach($Line in $Configuration)
    {
      If($Line.Keys -like "*servername")
      {
        $ServerName = $Line.Values
      }
      If($Line.Keys -like "*virtualhost")
      {
        $VHostName = $Line.Values
      }
      If($ServerName -And $VHostName)
      {
        Write-Output "ServerName = $ServerName; VirtualHost = $VHostName"
        Remove-Variable ServerName,VHostName
      }
    }
    
    ServerName = jvm1; VirtualHost = srv1.example.com
    ServerName = jvm3; VirtualHost = srv2.example.com
    

    If you want the data in a table format instead, you can create a new PSObject object and add the data to it, so you'd get:

    ServerName     VirtualHost
    -----------    ------------
    jvm1           srv1.example.com
    jvm3           srv2.example.com
    

    Hope that helps.

You must be logged in to reply to this topic.