Windows 更改文件中文本的 PowerShell 命令
Windows PowerShell command that changes text in a file
我需要更改 MySQL 设置中的 my.ini 文件。我想增加max_connections。所以基本上有一个 .bat 文件调用一些 powershell 命令替换文件中的文本文件并保存它。
我正在尝试通过 运行ning 一个 .bat 文件来做到这一点,该文件将 运行 一个 powershell 命令来更改文本,到目前为止,这是一个非常令人头疼的问题。我尝试了几种解决方案,但要么没有给我替换文本的权限,要么告诉我该对象不能用于管道命令。
我设法在不使用 .bat 文件的情况下在管理员 powershell 中运行它,但它不允许我在非管理员和 .bat 文件中执行此操作。
我在下面试过这个
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' -"
并得到以下错误
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.
我也使用 admin powershell 调用尝试过这种变体
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' -\""
但是我得到以下错误
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.
我已经查看了几个与此相关的堆栈溢出问题,但我似乎无法让它工作。
如果有更简单的方法来完成解决方案,那么我愿意接受建议。
更新:
当我复制并粘贴解决方案或让另一个程序将其传递给 powershell.exe 时,我在命令中看到这些奇怪的方括号,为什么会发生这种情况? (我用innoscript调用powershell.exe)
能够在批处理文件中完成它应该只是让你的语法正确。试试这个:
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'}
至于不必在管理员中 运行,请检查文件上的安全选项卡并确保您的用户帐户有权访问它,而不仅仅是管理员组中的人。
我需要更改 MySQL 设置中的 my.ini 文件。我想增加max_connections。所以基本上有一个 .bat 文件调用一些 powershell 命令替换文件中的文本文件并保存它。
我正在尝试通过 运行ning 一个 .bat 文件来做到这一点,该文件将 运行 一个 powershell 命令来更改文本,到目前为止,这是一个非常令人头疼的问题。我尝试了几种解决方案,但要么没有给我替换文本的权限,要么告诉我该对象不能用于管道命令。
我设法在不使用 .bat 文件的情况下在管理员 powershell 中运行它,但它不允许我在非管理员和 .bat 文件中执行此操作。
我在下面试过这个
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' -"
并得到以下错误
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.
我也使用 admin powershell 调用尝试过这种变体
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' -\""
但是我得到以下错误
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.
我已经查看了几个与此相关的堆栈溢出问题,但我似乎无法让它工作。
如果有更简单的方法来完成解决方案,那么我愿意接受建议。
更新:
当我复制并粘贴解决方案或让另一个程序将其传递给 powershell.exe 时,我在命令中看到这些奇怪的方括号,为什么会发生这种情况? (我用innoscript调用powershell.exe)
能够在批处理文件中完成它应该只是让你的语法正确。试试这个:
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'}
至于不必在管理员中 运行,请检查文件上的安全选项卡并确保您的用户帐户有权访问它,而不仅仅是管理员组中的人。