[英]How to run a powerShell script from a python file
I couldn't find much information on this unfortunately, but I wish to run a powerShell script from a python file I've written. 不幸的是,我找不到很多信息,但是我希望从我编写的python文件中运行powerShell脚本。 I want the user to actually see the powerShell script being run and the user can enter inputs that the powerShell script requires from python.
我希望用户实际看到powerShell脚本正在运行,并且用户可以从python输入powerShell脚本所需的输入。 I am using pyCharm as an IDE.
我正在使用pyCharm作为IDE。
When I run the script to call this powerShell script, it gives me this error: 当我运行脚本来调用此powerShell脚本时,它给了我这个错误:
File "C:\TestAutomation\eFuse\eFuse.ps1", line 19
SyntaxError: Non-ASCII character '\xc2' in file C:\Test\eK\eK.ps1 on line 19, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Here is the relevant part of the code: 这是代码的相关部分:
elif switch_result == "eTool":
subprocess.call(['python', 'C:\\TestAutomation\\eFuse\\eFuse.ps1'], stdout=sys.stdout)
This elif statement is a part of other if/elif statements that run other python files using the subproccess module, but for some reason I can't get this powerShell file to be run. 此elif语句是其他使用subproccess模块运行其他python文件的if / elif语句的一部分,但是由于某些原因,我无法运行此powerShell文件。 Any advice is appreciated.
任何建议表示赞赏。 Thank you
谢谢
First, you should already know .py
file's interpreter is python.exe
首先,您应该已经知道
.py
文件的解释器是python.exe
So, it's easy to understand .ps1
file's interpreter is powershell.exe
, not python.exe
因此,很容易理解
.ps1
文件的解释器是powershell.exe
,而不是python.exe
I just copy & paste from yours, your code should look like following, 我只是复制并粘贴您的代码,您的代码应如下所示,
subprocess.call('powershell.exe -File "C:\\TestAutomation\\eFuse\\eFuse.ps1"', stdout=sys.stdout)
Details about powershell.exe -?
有关
powershell.exe -?
详细信息powershell.exe -?
You can do following for provide input from user to PS (powershell) script from python : 您可以执行以下操作,以从python提供用户到PS(powershell)脚本的输入:
1) Create parameterised powershell script 1)创建参数化的Powershell脚本
2) Take input in python and set it as PS script parameter. 2)在python中获取输入并将其设置为PS脚本参数。
3) run/execute the script with given params. 3)使用给定的参数运行/执行脚本。
Here is the sample to run powershell script from python with param. 这是使用param从python运行powershell脚本的示例。 Its a python code :
它是python代码:
* Hope you have the PS script with parameters. *希望您具有带有参数的PS脚本。
import subprocess
params = ['param1', 'param2'] # POWERSHELL SCRIPT PARAMETERS ( optional )
script_path = "C:\\PowershellScripts\\test.PS1" # POWERSHELL SCRIPT PATH
commandline_options = ["Powershell.exe", '-ExecutionPolicy', 'Unrestricted', script_path] # INITIALIZING COMMAND
for param in params: # FOREACH LOOP OF PARAMETERS
commandline_options.append(param) # ADDING SCRIPT PARAMETERS TO THE COMMAND
result = subprocess.run(commandline_options, stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines = True) # RUN THE SCRIPT USING SUBPROCESS WITH PARAMS
print(result.returncode) # PRINT THE RETURN CODE FROM POWERSHELL SCRIPT
print(result.stdout) # PRINT THE STANDARD OUTPUT FROM POWERSHELL SCRIPT
print(result.stderr) # PRINT THE STANDARD ERROR FROM POWERSHELL SCRIPT
You can see the output of python if it exist ( if there is no error in powershell script. you will not get any output from last line ) 您可以看到python的输出(如果存在)(如果powershell脚本中没有错误。您将不会从最后一行获得任何输出)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.