Filip Juristovski Filip Juristovski - 3 months ago 11
MySQL Question

Windows PowerShell command that changes text in a file

I need to change the my.ini file in my MySQL setup. I want to increase the max_connections. So basically have a .bat file that calls some powershell commands that replace the text file in a file and save it.

I am trying to do this by running a .bat file that will run a powershell command to change the text, so far this has been a huge headache. I have tried several solutions and it either doesn't give me the permissions to replace the text or it tells me that the object cannot be used in the piped command.

I managed to get it working in the admin powershell without using a .bat file but it won't let me do it in non admin and in a .bat file.

I tried this below

PowerShell -NoExit -Command "(Get-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini') | ForEach-Object { $_ -replace \"max_connections=[0-9]+$\", \"max_connections=10000\" } | Set-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini' -"


and get the following error


Set-Content : The input object cannot be bound to any parameters for the command either because the command does not
take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.


I have also tried this variation using the admin powershell call

PowerShell -NoExit -Command "Start-Process powershell -verb runas -ArgumentList \"-NoExit -Command (Get-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini') | ForEach-Object {{ $_ -replace \""max_connections=[0-9]+$\"", \""max_connections=10000\"" } | Set-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini' -\""


but I get the following error


PowerShell -NoExit -Command "Start-Process powershell -verb runas -ArgumentList \"-NoExit -Command (Get-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini') | ForEach-Object {{ $_ -replace \""max_connections=[0-9]+$\"", \""max_connections=10000\"" } | Set-Content 'C:\ProgramData\MySQL\MySQL Server 5.7\my.ini' -\""
'ForEach-Object' is not recognized as an internal or external command,
operable program or batch file.


I have looked at several stack overflow questions regarding this but I cannot seem to get it to work.

If there is an easier way get the solution done then I am open to suggestions.

UPDATE:

When I copy and paste the solution or get another program to pass it to powershell.exe I get these weird square brackets in the command, why does this happen? (I use innoscript to call powershell.exe)

enter image description here

Answer

Being able to do it in a batch file should just be getting your syntax right. Try this:

PowerShell -NoExit -Command {(Get-Content 'C:\ProgramData\MySQ‌​L\MySQL Server 5.7\my.ini') | ForEach-Object { $_ -replace "max_connections=[0-‌​9]+$", "max_connections=100‌​00" } | Set-Content 'C:\ProgramData\MySQ‌​L\MySQL Server 5.7\my.ini'}

As for not having to run in Admin, check the security tab on your file and make sure your user account has rights to it, and not just people in the Administrators group.

Comments