简体   繁体   English

子流程在完成之前终止PowerShell脚本

[英]subprocess terminating a PowerShell Script before completion

Only in Python 3.7 using subprocessing methods run , Popen etc. or even using os.system() The PowerShell Script I'm calling seems to terminate before completion. 只有在Python 3.7使用子处理方式runPopen等,甚至使用os.system() PowerShell脚本我打电话,似乎在完成前终止。 I have a much more complex script, but I have been able to simplify both the Python script and PowerShell script to exhibit the issue. 我有一个更加复杂的脚本,但是我已经能够简化Python脚本和PowerShell脚本来解决这个问题。 The PowerShell Script is also shown below. PowerShell脚本也如下所示。

Here is the Python code: 这是Python代码:

''' subprocess truncates output ''' '''子进程截断输出'''

import subprocess       # subprocess library

p = subprocess.run("powershell -ExecutionPolicy ByPass -File C:/PS/testAquire.ps1",
                   shell=True, check=True, stdout=subprocess.PIPE)
print("stdout:", p.stdout)
print("stderr:", p.stderr)

This is the Python script output showing no errors: 这是Python脚本输出,不显示任何错误:

stdout: b''
stderr: None

This is the entire contents of the result.txt file after execution using the Python script above. 这是使用上述Python脚本执行后的result.txt文件的全部内容。

Directory: C:\windows\system32\drivers


Mode                LastWriteTime         Length Name

----                -------------         ------ ----                                                                   d-----        4/12/2018   5:15 AM                en-US
d-----        4/12/2018   5:15 AM                UMDF

-a----        1/19/2017   5:43 AM           3301 1028_Dell_INS_24-7459.mrk
-a----        4/11/2018   7:34 PM          29696 afunix.sys
-a----        4/11/2018   7:34 PM        3440660 gm.dls
-a----        4/11/2018   7:34 PM            646 gmreadme.txt
-a----        9/12/2015   4:59 AM          18720 IntelMEFWVer.dll

This is the PowerShell Script 这是PowerShell脚本

<#
.synopsis
Sample script to demonstrate unusual results when called from Python 3.7
#>

Get-ChildItem c:/windows/system32/drivers/ |  Format-Table | Out-File c:/PS/result.txt -Encoding ascii

This is the normal output of the script when run from the PowerShell Command or when using Python 2.7 to launch the PowerShell script. 当从PowerShell命令运行或使用Python 2.7启动PowerShell脚本时,这是脚本的正常输出。

Directory: C:\windows\system32\drivers


Mode                LastWriteTime         Length Name

----                -------------         ------ ----                                                                         d-----        4/11/2018   7:38 PM                DriverData
d-----        8/15/2018   5:33 PM                en-US
d-----        5/30/2018   9:31 AM                etc
d-----       11/14/2018   5:32 PM                UMDF
d-----       12/10/2018   5:50 PM                wd

-a----        1/19/2017   5:43 AM           3301 1028_Dell_INS_24-7459.mrk

-a----        4/11/2018   7:33 PM         237568 1394ohci.sys
-a----        4/11/2018   7:33 PM         107416 3ware.sys
-a----        4/11/2018   7:33 PM         654232 acpi.sys
-a----        4/11/2018   7:33 PM          20480 AcpiDev.sys
-a----        4/11/2018   7:33 PM         127904 acpiex.sys
-a----        4/11/2018   7:33 PM          12800 acpipagr.sys
-a----        4/11/2018   7:33 PM          14848 acpipmi.sys
-a----        4/11/2018   7:33 PM          13824 acpitime.sys
-a----        4/11/2018   7:33 PM        1135520 adp80xx.sys
-a----        4/11/2018   7:34 PM         626592 afd.sys
-a----        4/11/2018   7:34 PM          39424 afunix.sys
-a----        4/11/2018   7:34 PM         108032 agilevpn.sys
-a----        4/11/2018   7:34 PM         254464 ahcache.sys
-a----        9/24/2015   5:17 AM         109200 aksdf.sys
-a----        9/24/2015   5:17 AM         205528 aksfridge.sys
-a----        4/11/2018   7:33 PM         181760 amdk8.sys

... ... I reduced the output for brevity ...

-a----        4/11/2018   7:34 PM          33184 WppRecorder.sys
-a----        4/11/2018   7:34 PM          23040 ws2ifsl.sys
-a----        4/11/2018   7:33 PM          23040 WSDPrint.sys
-a----        4/11/2018   7:33 PM          25088 WSDScan.sys
-a----        4/11/2018   7:34 PM         125440 WUDFPf.sys
-a----        4/11/2018   7:34 PM         264192 WUDFRd.sys
-a----        6/15/2018  12:44 AM         295424 xboxgip.sys
-a----        4/11/2018   7:33 PM          46592 xinputhid.sys

I should note I have tried dozens of methods to launch the script with the same results. 我应该注意,我已经尝试了多种方法来启动具有相同结果的脚本。

Did you try to run the subprocess without using a shell? 您是否尝试在不使用Shell的情况下运行子流程? Like so: 像这样:

subprocess.run(
  ["powershell", "-ExecutionPolicy", "ByPass", "-File", "C:/PS/testAquire.ps1"],
  shell=False,
)

Alternatively, if you prefer to make use of the PowerShell, why not use it to redirect the output into the destination file (simple add a redirection to the end of the command; ... > c:/output.file ) 另外,如果您更喜欢使用PowerShell,为什么不使用它将输出重定向到目标文件中(简单地在命令末尾添加重定向; ... > c:/output.file

Thanks for looking into this. 感谢您查看这个。 I uninstalled Python 3.7.2 and then installed 3.6.8 and it works as designed. 我卸载了Python 3.7.2,然后安装了3.6.8,它按设计工作。 I have not determined if the problem was 3.7.2 or something to do with the installation. 我尚未确定问题是否出在3.7.2或与安装有关。 I will investigate. 我会调查。

Again, thanks for all your help and suggestions. 再次感谢您的所有帮助和建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM