从Python运行PowerShell脚本不工作

2024-03-28 11:42:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图运行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

我要检查输出。在


Tags: nametestinputoutputagedataplaceme
2条回答

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


检查逻辑如何应用

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

你想要的东西大致是:

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

相关问题 更多 >