Kaseya Community

Edit an .ini file

This question is answered

Hoping someone has an idea on how to do what on the surface is an easy thing but getting it to automate through Kaseya may be more of an issue.

We're upgrading a piece of software that has a .ini file that contains different important bits of info including registration to the hardware dongle attached to the computer.  Inside the .ini I need to change all references to the old directory to the new one.  So if I was doing it in Notepad I'd just do a find/replace of all instances of C:\wmars with C:\wmarsnet and then save the file.

I get the sneaking suspicion that there's a way to do this with a simple command or script.

Verified Answer
  • Just noticed you specifed what you were replacing...   make a copy of the INI and test it on a local system in powershell first but this should do what you are looking for.

    ((Get-Content -path c:\path\to\my.ini -raw) -replace 'C:\\wmars', 'C:\wmarsnet') | set-content -path c:\path\to\my.ini

    (obviously replacing the path to the ini file)

All Replies
  • First Thoughts .... Read the file into a variable within Kaseya ... Make Changes ... Write variable back to file

    The first 2 are the gimmes, and the obvious thing to do, the Make Changes part is much harder and where I am not sure can even be done within Kaseya ... So this probably scraps that idea of doing it completely within Kaseya.

    So the next solution would have to be if you were doing it directly on the system itself what steps would you take if you could not open the file within notepad, if you had to do this from a command prompt - how would you proceed? Whatever you come up with then becomes the batch script or the powershell script that you can push and run from Kaseya ...

  • I'll have to dig back into my bag of tricks so it might be tomorrow before I can get it, but did something similar once using a windows version of the SED utility.

  • For finding and replacing text in a file I always used the "Find And Replace Tool" in short; fart.exe.

    Make an agent procedure that sends fart.exe to the endpoint (writefile) and then use CLI with arguments to replace the text (executeshell).

  • Use the correct ExecutePowerShell for the processor (check with OS if call) and execute the following:

    $text = (Get-Content -Path "yourINI" -ReadCount 0) -join "`n"; $text -replace 'dEf', 'xyz' | Set-Content -Path "YourINI"

    Change the "YourINI" to be your .ini file path

  • Got to thinking more and realized there's a much quicker way with a simple powershell.

    ((Get-Content -path c:\somefolder\some.ini -raw) -replace 'searchtext here', 'replacement text here') | set-content -path c:\somefolder\some.ini

    Simply replace the c:\somefolder\some.ini with the full path to the ini file, and the searchtext here and replacement text here with the appropriate text and you should be good.  The one thing to note if you are dealing with changing windows paths in the ini, then you have to escape any backslashes in the search text, but *not* in the replacement text.  

    So for example if you were replacing c:\windows\system with c:\windows\system32, then it would look more like this:

    ((Get-Content -path c:\somefolder\some.ini -raw) -replace 'c:\\windows\\system', 'c:\windows\system32) | set-content -path c:\somefolder\some.ini

  • Pretty much what I thought.  Inside Kaseya itself it wouldn't be feasible but with a PowerShell or batch script it would be.  Then with Kaseya either copy the ini to another directory or do it within the current directory, write the script to that directory, run the script via Kaseya, then depending on step 1 save the file or write the file back to the original directory.

    This would definitely be easier if each user's program ID and hardware dongle weren't unique in each ini.

  • Just noticed you specifed what you were replacing...   make a copy of the INI and test it on a local system in powershell first but this should do what you are looking for.

    ((Get-Content -path c:\path\to\my.ini -raw) -replace 'C:\\wmars', 'C:\wmarsnet') | set-content -path c:\path\to\my.ini

    (obviously replacing the path to the ini file)

  • Fortunately I had a copy of a user's ini file on my computer so I could test.  Unfortunately the script didn't change the text when using the 'C:\\wmars' and 'C:\\wmarsnet'.  Instead I just used 'wmars' and "wmarsnet' as the entries and it worked perfectly.  Fortunately in this instance the only times wmars is used in the ini is when it's using C:\\wmars.

    Thanks Jonathan!

  • ... Didn't think about the fact that the ini would probably have the backslash already escaped... In that case you would have had to use c:\\\\wmars for the search and c:\\wmarsnet for the replace.  

    In any case glad I could point you in the right direction.