【发布时间】:2020-09-01 17:33:05
【问题描述】:
我有一个功能齐全的 Powershell 脚本,可以毫无问题地生成 HTML 报告。我正在尝试创建一个 Python 脚本来运行 PowerShell 脚本,以便我可以通过电子邮件发送带有 Python 的报告以及我工作的其他原因。这是我第一次尝试在 Python 脚本中运行 Powershell 脚本,但我仍然是 Python 的新手,并且 Python 比 Powershell 更好。研究了这个问题 2-3 个小时,我摸不着头脑。我的最终目标是使用 Python 创建 AD 报告,但我必须从头开始学习,而我拥有的 Powershell 脚本可以完成我现在需要做的所有事情。
提前致谢!
代码:
import subprocess
result = subprocess.run(['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', 'C:\\Users\\wills.b\\Documents\\scripts\\ADHTMLREPORT.ps1'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
print(result)
错误:
bryanwi09@KY-IT-002:/mnt/c/Users/wills.b/Documents/scripts$ python3 report.py
File "report.py", line 2
result = subprocess.run(['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', 'C:\Users\wills.b\Documents\scripts\ADHTMLREPORT.ps1'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
bryanwi09@KY-IT-002:/mnt/c/Users/wills.b/Documents/scripts$ python3 report.py
CompletedProcess(args=['C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', 'C:\\Users\\wills.b\\Documents\\scripts\\ADHTMLREPORT.ps1'], returncode=127, stdout=b'C:\\Users\\wills.b\\Documents\\scripts\\ADHTMLREPORT.ps1: 1: C:WindowsSystem32WindowsPowerShellv1.0powershell.exe: not found\n')
【问题讨论】:
-
如果您在 Powershell 提示符下运行
.ps1文件,它是否有效?如果是这样,那么问题出在 Python 部分。如果没有,那么它在 Powershell 部分。 -
@vonPryz,是的,.ps1 脚本输出一个 .html 文件,然后通过电子邮件发送。
-
python3 report.py与python.exe report.py的结果不同(Windows 10 上的 WSL)...
标签: python python-3.x powershell subprocess