Replace Lini in ini with Powershell
Hi!!
I have to deaktivate a line in an ini File. It is justr adding an ; at the beginning.
I tried to replace it real simpel, but getting an Error. What am I doing wrong?
$NotesINI="$Profile\AppData\Local\Lotus\Notes\Data"
$alterEintrag='AddinMenus=C:\PROGRA~1\NXPOWE~1\NXPMEN~1.DLL'
$neuerEintrag=';AddinMenus=C:\PROGRA~1\NXPOWE~1\NXPMEN~1.DLL'
# Deaktivieren von NXPowerlite
[string[]]$ProfilePaths = Get-UserProfiles -ExcludeNTAccount 'Domain\accountname' | Select-Object -ExpandProperty 'ProfilePath'
foreach ($Profile in $ProfilePaths){
(Get-Content -Path "$NotesINI\notes.ini") -replace "$alterEintrag", "$neuerEintrag" | Set-Content "$NotesINI\notes.ini"
}
Answers (4)
[Notes]
KitType=1
SharedDataDirectory=C:\ProgramData\Lotus\Notes\Data\Shared
UserInterface=de
InstallType=6
InstallMode=1
NotesProgram=c:\Program Files (x86)\IBM\Lotus\Notes\
Configfile=C:\ProgramData\Lotus\Notes\Data\notescfg.txt
IM_DISABLED=1
IM_NO_SETUP=1
Directory=C:\Users\av1gj\AppData\Local\Lotus\Notes\Data
FaultRecovery_Build=Release 9.0.1FP10 SHF68
DSTLAW=3,-1,1,10,-1,1
USING_LOCAL_SHARED_MEM=1
LOCAL_SHARED_MEM_SESSION_ID=2
FileDlgDirectory=C:\Users\av1gj\Documents
AddinMenus=C:\PROGRA~1\NXPOWE~1\NXPMEN~1.DLL
Comments:
-
OK, that was easy. Sorry, i couldn't read your error message, but I think the error would of been "The regular expression pattern AddinMenus=C:\PROGRA~1\NXPOWE~1\NXPMEN~1.DLL is not valid."
The PowerShell "replace" operator your using can also be used with Regular Expression. Because of that feature, your replace string is getting confused with the backslash's. You need to escape the backslash's for the pattern (search and replace) string to work.
Fix below, use the simple test below to double check it works before adding it to your script.
$alterEintrag='AddinMenus=C:\\PROGRA~1\\NXPOWE~1\\NXPMEN~1.DLL'
$neuerEintrag=';AddinMenus=C:\PROGRA~1\NXPOWE~1\NXPMEN~1.DLL'
(Get-Content -Path "C:\A\Test.ini") -replace "$alterEintrag", "$neuerEintrag" | Set-Content "C:\A\Test.ini"
Don't forget to check the encoding is the same before editing the file, and after editing the file, as mentioned in my post. - rileyz 4 years ago
Presuming the PowerShell script worked, and the file was successfully written with the desired changes?
The next step would be to check the file encoding is correct. For example open the INI that has not been modified in NotePad++ and check the encoding.
Also dont forget to check hidden characters for the correct carrge return etc.
PowerShell Set-Content info https://ss64.com/ps/set-content.html
Check/read Encoding section for more information about switch
Summary
When editing files with scripting, always check with Notepad++ or your choice of editor for the correct file encoding and characters before and after a file change.
I believe that the following line is incorrect.
$neuerEintrag= ' ;
Should be
$neuerEintrag= ; '
Comments:
-
Ich want to replace the original Value with ; at the Beginning. So that cant be the Problem. I assume the special Characters are the Problem. Can you tell me, how I work with the Line as plain Text? - av1gj 4 years ago
I found out, that it musst be something with the special charcters.
If I just use the beginning, it works.
$alterEintrag="AddinMenus"
$neuerEintrag=';AddinMenus'
Can you help me with the special Charcters?
Comments:
-
Can you post a sample of the INI file for me to test, will see if I can fix it for you. - rileyz 4 years ago