从 Python 脚本运行 PowerShell 函数 [英] Run PowerShell function from Python script

查看:40
本文介绍了从 Python 脚本运行 PowerShell 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是示例 PowerShell 脚本:

Here is the sample PowerShell script:

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

Function bye
{
    Write-Host "Goodbye"
}

Write-Host "PowerShell sample says hello."

和 Python 脚本:

and the Python script:

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:WINDOWSsystem32WindowsPowerShellv1.0powershell.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 "

stdout:

" + str(output)
print "

stderr: " + str(error)

所以,不知何故,我想运行 PowerShell 示例中的hello()"或bye()"函数.知道如何将参数传递给函数也会很好.谢谢!

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!

推荐答案

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

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

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

所以这里发生的是我们启动powershell,告诉它导入您的脚本,并使用分号结束该语句.然后我们可以执行更多的命令,即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.

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

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)
}

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

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:

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

这给了我输出:

PowerShell sample says hello.
11

这篇关于从 Python 脚本运行 PowerShell 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆