-1

I need some help with an powershell script I just can not figure out on how to do it.

The very basic question is, if I have 2 config files, 1. default and 2. specific updated values. How do I "compare" the two setting files, and how do I create a third config File.

Lets say, config one (default_config.txt):

## general settings ##
user = 1
ringtonetype = 1
ringtonevolume = 10

## user settings
volumespeaker = 10
volumeheadset = 9

## ldap settings
address = 192.168.0.244
subnet = 255.255.255.0
user = admin
password = admin

And specific Values for config two (config_new_site.txt):

## general settings ##
ringtonetype = 5
ringtonevolume = 15

## user settings
volumespeaker = 5
volumeheadset = 5

How to create "config_new_site_updated.txt with default values and updated from "new site"?

It should look like this:

## general settings ##
user = 1
ringtonetype = 5
ringtonevolume = 15

## user settings
volumespeaker = 5
volumeheadset = 5

## ldap settings
address = 192.168.0.244
subnet = 255.255.255.0
user = admin
password = admin

Any advice is highly appreciated and thank you in advance!

  • Loop trough each line and compare it to the line on the opposite file. Take a look at `Get-Content` , `Out-File` and https://ss64.com/ps/for.html . – Paxz Jul 05 '18 at 13:15
  • 3
    As you can understand, this site (of necessity) cannot be a code-generation service. That being said, what does your code look like so far? What have you tried, and with what results? – Bill_Stewart Jul 05 '18 at 13:15
  • @Bill_Stewart I don't want anyone to write a working code, I am looking for advices to get it done by myself. sorry if I did not mentioned it that way. I fix my error and post the code – Christian B. Jul 05 '18 at 13:22
  • @paxz So it is neccessary to read "file1/line1" compare to "file2/line 1 to [end of file] ? – Christian B. Jul 05 '18 at 13:27
  • It is probaly the easiest way, even more if you are new to this.. – Paxz Jul 05 '18 at 13:30
  • @paxz thanks for your advice! – Christian B. Jul 05 '18 at 13:31
  • 2
    Direct line to line comparison will not work. I would suggest reading each line and create a data structure in memory. What that data structure looks like will depend on the data itself. In your case if you need to keep those section headers then I would personally create a custom object for each section and have the object have properties that match the values in that section. If you don't need the headers and the setting names are all unique then a hashtable would be easier. It is really going to be up to you and how you want to do it. – EBGreen Jul 05 '18 at 13:35
  • If I got this right you want to have a general config and update it with a specific one, right? So, read in the general config file and create variables from each single setting. Then you read the specific config file and create variables for each single setting. If the setting is already there you will overwrite if not you create a new one. – Olaf Jul 05 '18 at 13:50
  • 1
    Tonnes of examples here that could help: https://stackoverflow.com/questions/417798/ini-file-parsing-in-powershell – Matt Jul 05 '18 at 13:53

1 Answers1

0
$OldConfig = @(Get-Content "C:\test\default_config.txt")
$NewConfig = @(Get-Content "C:\test\config_new_site.txt")


for ($i = 0; $i -lt $OldConfig.Count; $i++)
{ 
    # Skipping NewLines, Comments/Headlines and Invalid Lines
    if($OldConfig[$i] -eq "" -or $OldConfig[$i].Substring(0,1) -eq "#" -or (!$OldConfig[$i] -like "*=*"))
    {
        continue
    }

    #Split at '=' and Remove Spaces to get clean Property & Value
    $OldProperty = $OldConfig[$i].Split('=')[0].TrimEnd().TrimStart()
    $OldValue = $OldConfig[$i].Split('=')[1].TrimEnd().TrimStart()

    $NewValue = $null

    # Loop throuh new Config and Compare Property
    foreach ($NewLine in $NewConfig)
    {
        # Skipping NewLines, Comments/Headlines and Invalid Lines
        if($NewLine -eq "" -or $NewLine.Substring(0,1) -eq "#" -or (!$NewLine -like "*=*"))
        {
            continue
        }

        #Split at '=' and Remove Spaces to get clean Property
        $NewProperty = $NewLine.Split('=')[0].TrimEnd().TrimStart()  

        # Compare Property
        if($OldProperty -eq $NewProperty)
        {
            # Set NewVal if Propertys matched
            $NewValue = $NewLine.Split('=')[1].TrimEnd().TrimStart()
            break
        }     
    }

    # Update Property with new Value if Value has changed
    if($NewValue -ne $null -and $NewValue -ne $OldValue)
    {
        $OldConfig[$i] = "$OldProperty = $NewValue"
        Write-Host "Set `"$OldProperty`" from `"$OldValue`" to `"$NewValue`""          
    }       
}

$OldConfig | Out-File "C:\test\config_new_site_updated.txt" -Force
Evilcat
  • 388
  • 3
  • 9