【问题标题】:how to run .bat file with pass some parameter in powershell script?如何在powershell脚本中传递一些参数来运行.bat文件?
【发布时间】:2021-08-29 14:24:52
【问题描述】:

在我的机器中,这个位置内容,windows bat 文件。 “D/too/w.bat”。这个bat文件可以在cmd中运行,像这样w.bat I :/ o:/在Powershell中,可以像这样运行“./w.bat i: o:/”。现在我需要使用一些自动化的 .ps 脚本来运行它。我试着像下面这样,

    #
    function run--bat {
     Write-host "run bat";
     Start-Process -File "C:/t/w.bat" "i:/ o:/"
    }

但我没有成功。需要一些专家的帮助来解决这个问题。

【问题讨论】:

  • 为什么要使用powershell来运行批处理文件?如果您只使用该批处理文件中的任何内容并将其作为命令也直接添加到 powershell 中,那可能会更好。同样因为这显然适用于 Windows,那些正斜杠应该是反斜杠,因为这是 Windows 用于根目录和路径分隔符的内容。此外,由于您的问题仅仅是由于您未能阅读 start-process 命令的帮助信息,因此它离题了,因为您会清楚地注意到 -ArgumentList-FilePath 参数输出:get-help start-process -detailed。跨度>

标签: powershell batch-file


【解决方案1】:

传递参数列表:

Start-Process C:\t\w.bat -ArgumentList 'i:\','o:\'

逗号运算符在 PowerShell 中构建数组,因此 'i:\','o:\' 是一个二元素数组。

Start-Process 将使用空格连接参数以创建最终的命令行。这意味着您也可以将两个参数作为一个字符串传递:

Start-Process C:\t\w.bat -ArgumentList 'i:\ o:\'

w.bat的角度来看没有区别。

要将包含空格的值(例如路径)作为单个参数传递,您需要将其用双引号引起来:

Start-Process C:\t\w.bat -ArgumentList '"i:\something\with spaces"','o:\'

然后w.bat 将看到两个参数:

  • %1 = "i:\something\with spaces"(和%~1 = i:\something\with spaces
  • %2 = o:\

【讨论】:

    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2011-08-01
    相关资源
    最近更新 更多