从Python脚本运行PowerShell功能 [英] Run PowerShell function from Python script
问题描述
我需要从Python脚本运行PowerShell函数。 .ps1和.py文件当前都位于同一个目录中。我想调用的函数在PowerShell脚本中。我见过的大部分答案都是从Python运行整个PowerShell脚本。在这种情况下,我试图从Python脚本中的PowerShell脚本中运行单个函数。
以下是示例PowerShell脚本:
#sample PowerShell
函数hello
{
Write-Host来自hello函数的嗨:
函数bye
{
写主机再见
}
写主机PowerShell示例问好。
和Python脚本:
<$ p $
import subprocess as sp
parser = argparse.ArgumentParser(description ='从Python调用PowerShell函数的示例')
解析器。 )
$ b $ args = parser.parse_args()
$ add_argument(' b $ b psResult = sp.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
'--ExecutionPolicy',
'无限制',
'./samplePowerShell',
args.functionToCall],
stdout = sp.PIPE,
stderr = sp.PIPE)
输出,错误= psResult.communicate()
rc = psResult.returncode
print给Python脚本的返回码是:+ str(rc)
print\ n \ nstdout:\\\
\\\
+ str(输出)
打印\\\
\\\
stderr:+ str(错误)
所以,不知何故,我想要t o运行PowerShell示例中的'hello()'或'bye()'函数。知道如何将参数传递给函数也是很好的。
Thanks!
您需要两件事:点源脚本(这是(as据我所知)类似于python的导入)和 subprocess.call 。
导入子流程
subprocess.call([C:\\WINDOWS\\system32\\\ \\ Windows \\ PowerShell \\\\\\\\\\\\\\\\\\\\')
这里发生的是我们启动了powershell,告诉它导入你的脚本,并用分号结束该语句。然后我们可以执行更多的命令,也就是你好。
你也想为函数添加参数,所以我们使用上面的文章中的修改(略微修改): 然后用你想要的任何参数调用该函数,只要powershell能够处理输入。所以我们将修改上面的python为: 这给了我输出: 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. Here is the sample PowerShell script: and the Python script: 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! You want two things: dot source the script (which is (as far as I know) similar to python's import), and subprocess.call. 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): 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: this gives me the output:
这篇关于从Python脚本运行PowerShell功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b $ pre $ 函数addOne($ intIN)
{
写主机($ intIN + 1)
$ / code>
import subprocess
subprocess.call([C:\ \ WINDOWS \\\ system32 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ])
PowerShell示例打个招呼。
11
# sample PowerShell
Function hello
{
Write-Host "Hi from the hello function : )"
}
Function bye
{
Write-Host "Goodbye"
}
Write-Host "PowerShell sample says hello."
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)
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePowershell\";", "&hello"])
Function addOne($intIN)
{
Write-Host ($intIN + 1)
}
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePowershell\";", "&addOne(10)"])
PowerShell sample says hello.
11