1

I am trying to edit the Mirth property file to add microsoft sql server driver url and class name using powershell. I am able to edit and save . When I start the Mirth services after editing, the Mirth property file is corrupted. But I reviewed both the original and the edited content. Everything is the same except the added changes and the file size is increased from 5kb to 10 kb. Can anyone help me out? Here is the powershell script.

$server="Localhost"
$PropertyfilePath = "C:\Program Files\Mirth Connect\conf\mirth.properties"
$connectionString = "jdbc:sqlserver://$($server):1433;instanceName=$($server);databaseName=Mydb;integratedSecurity=true" 
$driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
$data= @()
Copy-Item $PropertyfilePath "D:\Mirthbackup" -force
$newstreamreader = New-Object System.IO.StreamReader($PropertyfilePath)
[int]$eachlinenumber = 1
while (($readeachline = $newstreamreader.ReadLine()) -ne $null)
{
    if($readeachline.Contains("= derby")){
            
        $readeachline.Remove(0)
        $update= "database = sqlserver"
        $data +=$update
        $eachlinenumber++
    }
   elseif($readeachline.Contains("database.url"))
    {
       
        $update=$readeachline.Substring(0,12)+" = "+$connectionString
        $data +=$update
        $eachlinenumber++
        
    }
    elseif($readeachline.Contains(".driver"))
   {
        $readeachline.Remove(0)
        $update ="database.driver = "+$driverName
        $data +=$update
        $eachlinenumber++
    
    }
    else{

        $data +=$readeachline
        $eachlinenumber++
     }
}
$newstreamreader.Dispose()
 
$data | Out-File -FilePath $PropertyfilePath 
Shamie
  • 49
  • 8
  • 2
    The doubled size suggests that you have changed the encoding to something double-byte, probably unicode. Find out what encoding the original file is and make the script keep that. – Palle Due Sep 28 '20 at 12:43
  • 1
    According to this [question](https://stackoverflow.com/questions/34718637/java-util-properties-encoding) a java properties file is ISO-8859-1 encoded. – Palle Due Sep 28 '20 at 12:47
  • @PalleDue Really appreciated for your answer. But the content is all writing into a single line. When I replaced the WriteAllText method with WriteAllLines then it was more accurate. – Shamie Sep 29 '20 at 05:54

1 Answers1

3

Your properties file should be ISO-8859-1 (latin1) encoded.

Try to change your code to:

$server="Localhost"
$PropertyfilePath = "C:\Program Files\Mirth Connect\conf\mirth.properties"
$connectionString = "jdbc:sqlserver://$($server):1433;instanceName=$($server);databaseName=Mydb;integratedSecurity=true" 
$driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
$data= @()
Copy-Item $PropertyfilePath "D:\Mirthbackup" -force
$encoding = [System.Text.Encoding]::GetEncoding('iso-8859-1'))
$newstreamreader = New-Object System.IO.StreamReader($PropertyfilePath, $encoding)
[int]$eachlinenumber = 1
while (($readeachline = $newstreamreader.ReadLine()) -ne $null)
{
  # if statements left out
}
$newstreamreader.Dispose()

[System.IO.File]::WriteAllLines($PropertyfilePath,$data, $encoding)
Shamie
  • 49
  • 8
Palle Due
  • 5,929
  • 4
  • 17
  • 32