Update a config file from CSV

Topics: Archive - Toolkit Extensions
Dec 30, 2014 at 2:33 PM
Edited Dec 30, 2014 at 2:37 PM
Got this code from a friend, if someone has a cleaner way of replacing values in a config file before deployment feel free to post it below. Using function updatefile and some code to pull config out of a CSV. I know you could do an ini instead of a csv. It's the updating the config after you have the value I wish was cleaner. Hope this helps someone with multiple locations.

Function UpdateFile
{
$file = Get-ChildItem $FILENAME
foreach ($str in $file) 
{
    $content = Get-Content -path $str
    $content | foreach {$_ -replace $OLDSTRING,$NEWSTRING} | Set-Content $str
} 
}

$site = $computername.substring(0,3)
$CSVfile = 'FacilityServers.csv'
$CSV=Import-Csv $CSVfile
[String]$Facility = $CSV | Where-Object {$_.'site' -eq $site} | select 'Facility'
$Facility = $Facility.Replace("@{Facility=","")
$Facility = $Facility.Replace("}","")

-----------------------------------------

$FILENAME = "main.conf"
$OLDSTRING = "XXXXXXXXXXXX"
$NEWSTRING = $GUIServer

-----------------------------------

CSV file contents
Site,GUIServer,Facility
ABC,ABCGUI01,ABC,

config file
[Global]
Replacenew=7za.exe,"XXXXXXXXXXXX\main\update\