【问题标题】:Running PowerShell script from Python not working从 Python 运行 PowerShell 脚本不起作用
【发布时间】:2018-02-15 11:41:34
【问题描述】:

我正在尝试运行 PowerShell 脚本 test_me.ps1,它需要 3 个参数:-Name name-Age age-Place place

input_data 是我从 pytest 传递的内容。

这是我的代码:

try:
    output = subprocess.call(
        [
            "powershell.exe",
            "-Name" + input_data['name'],
            "-Age" + input_data['age'],
            "Place" + input_data['place']
        ], '&{. "./test_me.ps1"}')
except subprocess.CalledProcessError, e:
    print "subprocess CalledProcessError.output = " + e.output
print output

我想检查输出。

【问题讨论】:

  • 你期望什么输出?你得到了什么?如果您没有发现错误并可能隐藏部分错误,您会得到什么?
  • 在 PowerShell 中运行 test_me.ps1 的输出是什么?

标签: python python-2.7 powershell subprocess


【解决方案1】:

你想要一些类似的东西:

output = subprocess.call(
    [
        "powershell",
        "-File", "./test_me.ps1",
        "-Name", input_data['name'], 
        "-Age", input_data['age'], 
        "Place", input_data['place']
    ])

【讨论】:

    【解决方案2】:

    在您的代码中,您只是尝试输出异常错误,而您不会得到任何错误,因为您尝试运行的 python 文件没有收到任何错误。


    检查逻辑如何应用

    我认为您可以通过从主要 python 文件返回数据然后将其放在文件中以从该脚本中读取来完成它。

    【讨论】:

      猜你喜欢
      • 2019-12-02
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 2013-09-18
      • 2012-08-11
      • 2013-01-08
      • 1970-01-01
      相关资源
      最近更新 更多