来自Powershell的Python Capture回复 [英] Python Capture reply from powershell

查看:77
本文介绍了来自Powershell的Python Capture回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在手动键入时有效,但是当我运行程序时,没有任何输出.我的最终目标是从用户PC中检索这些数据,以创建一种重新创建快捷方式的简便方法....我的用户莫名其妙地失去了它们,大声笑

The code below works when typed manually however when I run the program.py nothing prints. My ultimate goal is to retrieve this data from user pc to create an easy way to recreate shortcuts.... My users somehow lose them lol

import smtplib, os, subprocess, sys
from string import ascii_uppercase
from cStringIO import StringIO

data = os.popen(r"dir %userprofile%\desktop\*.lnk* /s/b").read()
file = open("testitem.txt", "w")
file.write(data)
file.close()


my_data = dict(zip(ascii_uppercase,open("testitem.txt")))


old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()

for key, value in my_data.iteritems():
    subprocess.Popen([r"powershell.exe", "$sh = New-Object -COM WScript.Shell" + "\n" +     "$sh.CreateShortcut(\"%s\").TargetPath" % my_data[key].replace("\n", "")], stdout=subprocess.PIPE).communicate()[0]


sys.stdout = old_stdout

shared = mystdout.getvalue()
print shared

推荐答案

尝试删除此sys.stdout = old_stdout行并将stdout=sys.stdout添加到您的主过程行中,例如.Popen(***,stdout=sys.stdout)

try removing this sys.stdout = old_stdout line and add stdout=sys.stdout to your main process line for ex like .Popen(***,stdout=sys.stdout)

这篇关于来自Powershell的Python Capture回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆