从 Python 脚本运行 PowerShell 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14508809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:40:01  来源:igfitidea点击:

Run PowerShell function from Python script

pythonfunctionpowershell

提问by H_H

I have a need to run a PowerShell function from a Python script. Both the .ps1 and the .py files currently live in the same directory. The functions I want to call are in the PowerShell script. Most answers I've seen are for running entire PowerShell scripts from Python. In this case, I'm trying to run an individual function within a PowerShell script from a Python script.

我需要从 Python 脚本运行 PowerShell 函数。.ps1 和 .py 文件当前都位于同一目录中。我要调用的函数在 PowerShell 脚本中。我见过的大多数答案都是从 Python 运行整个 PowerShell 脚本的。在这种情况下,我尝试从 Python 脚本在 PowerShell 脚本中运行单个函数。

Here is the sample PowerShell script:

这是示例 PowerShell 脚本:

# sample PowerShell
Function hello
{
    Write-Host "Hi from the hello function : )"
}

Function bye
{
    Write-Host "Goodbye"
}

Write-Host "PowerShell sample says hello."

and the Python script:

和 Python 脚本:

import argparse
import subprocess as sp

parser = argparse.ArgumentParser(description='Sample call to PowerShell function from Python')
parser.add_argument('--functionToCall', metavar='-f', default='hello', help='Specify function to run')

args = parser.parse_args()

psResult = sp.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
'-ExecutionPolicy',
'Unrestricted',
'. ./samplePowerShell',
args.functionToCall],
stdout = sp.PIPE,
stderr = sp.PIPE)

output, error = psResult.communicate()
rc = psResult.returncode

print "Return code given to Python script is: " + str(rc)
print "\n\nstdout:\n\n" + str(output)
print "\n\nstderr: " + str(error)

So, somehow, I want to run the 'hello()' or the 'bye()' function that is in the PowerShell sample. It would also be nice to know how to pass in parameters to the function. Thanks!

因此,不知何故,我想运行 PowerShell 示例中的“hello()”或“bye()”函数。知道如何将参数传递给函数也会很好。谢谢!

回答by Adam R. Grey

You want two things: dot source the script(which is (as far as I know) similar to python's import), and subprocess.call.

您需要两件事:点源脚本((据我所知)类似于 python 的导入)和subprocess.call

import subprocess
subprocess.call(["C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe", ". \"./SamplePowershell\";", "&hello"])

so what happens here is that we start up powershell, tell it to import your script, and use a semicolon to end that statement. Then we can execute more commands, namely, hello.

所以这里发生的是我们启动powershell,告诉它导入您的脚本,并使用分号结束该语句。然后我们可以执行更多的命令,即hello。

You also want to add parameters to the functions, so let's use the one from the article above (modified slightly):

您还想为函数添加参数,所以让我们使用上面文章中的一个(稍作修改):

Function addOne($intIN)
{
    Write-Host ($intIN + 1)
}

and then call the function with whatever parameter you want, as long as powershell can handle that input. So we'll modify the above python to:

然后使用您想要的任何参数调用该函数,只要 powershell 可以处理该输入。所以我们将上面的python修改为:

import subprocess
subprocess.call(["C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe", ". \"./SamplePowershell\";", "&addOne(10)"])

this gives me the output:

这给了我输出:

PowerShell sample says hello.
11