【问题标题】:Batch File - Commands not executing after Powershell command批处理文件-Powershell命令后未执行的命令
【发布时间】: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


【解决方案1】:

arco444 的回答在 cmets 中对我有用:

您只需调用不带参数的 powershell.exe,它就会启动一个交互式会话。如果您要键入 exit,您的批处理脚本将继续。使用 -command 开关执行您想要执行的任何操作。请编辑您的问题并在其中包含代码。此外,您需要将 powershell 命令放在引号中。即 powershell -command "get-content file.cs"

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 2020-08-13
    • 2011-08-27
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多