运行powershell脚本的Python脚本

2024-04-20 06:17:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我的要求是用python运行PowerShell脚本。 但这是错误的

我的Python脚本:

import subprocess, sys

p = subprocess.Popen(["powershell.exe", 
              "D:\\Python\\EXPORT_AD_GROUP.ps1"], 
              stdout=sys.stdout)
p.communicate()

错误:

Traceback (most recent call last):
  File "D:/Python/Power_Shell_Python.py", line 3, in <module>
    p = subprocess.Popen(["powershell.exe",
  File "C:\Users\htembkar\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 804, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\Users\htembkar\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1174, in _get_handles
c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
io.UnsupportedOperation: fileno