【发布时间】:2015-08-17 10:40:58
【问题描述】:
我编写了简单的批处理脚本来替换文件中的一些文本,当通过命令行执行但当这些命令被复制到 .bat 文件时,执行在 powershell 命令后停止。知道如何在批处理文件中执行 powershell 命令吗?
【问题讨论】:
-
您只需调用
powershell.exe,不带任何参数,因此它会启动一个交互式会话。如果您输入exit,您的批处理脚本将继续。使用-command开关执行您想要执行的任何操作。请edit您的问题并在其中包含代码 -
感谢 arco444 回复我也试过了。您可以看到 REM 行带有 -command 后跟替换代码。
-
执行时 powershell.exe -command Get-Content AssemblyInfo.cs | ForEach-Object { $_ -replace 'assembly: AssemblyVersion', 'assembly11111' } | Set-Content AssemblyInfo2.cs 出现错误“ForEach-Object”无法识别
-
您需要将 powershell 命令放在引号中。即
powershell -command "get-content file.cs" -
那行得通。谢谢你拯救了我的一天。
标签: powershell batch-file