使用python获取powershell结果

时间:2017-01-17 08:19:57

标签: python powershell subprocess

我想设计一个GUI程序来回调一些powershell的命令结果。 问题是当选择cb_option_a并单击执行按钮时,调用powershell并运行命令是成功的。

但是,将结果回调到textarea框并不成功,它有以下错误:

CalledProcessError: Command 'get-itemproperty
HKLM:\SOFTWARE\Microsoft\InetStp\  | select setupstring,versionstring'
returned non-zero exit status 255

ipconfig这样的cmd命令的其他选项是回调成功。我该如何解决这个问题? 程序代码:

option_a = IntVar()
option_b = IntVar()
option_c = IntVar()

def startpowershell():
    powershellcommand = command_gen()
    commandresult = runpowershellcommand(powershellcommand)
    txtarea_output.insert(INSERT, commandresult)


def command_gen():

    if option_a.get():
        command = "get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\  | select setupstring,versionstring"

    if option_b.get():
        command = "ipconfig"

    if option_c.get():
        command ="ping 127.0.0.1"

    command = command

    result = command
    return result

def runpowershellcommand(callpowershell):
    output = subprocess.call(["powershell.exe", command_gen()])
    output = subprocess.check_output(callpowershell, stderr=subprocess.STDOUT, shell=TRUE)
    txtarea_output.config(state=NORMAL)
    txtarea_output.insert(INSERT, output)
    txtarea_output.config(state=DISABLED)
    return output


lb_RT = Label(root, text="PowerShell:")
lb_RT.grid(row=0, column=0, padx=10, pady=10, sticky=W)

cb_option_a = Checkbutton(root, text="IIS ver.", variable=option_a)
cb_option_a.grid(row=1, column=0, padx=10, pady=10, sticky=W)

cb_option_b = Checkbutton(root, text="cmd command1", variable=option_b)
cb_option_b.grid(row=2, column=0, padx=10, pady=10, sticky=W)

cb_option_c = Checkbutton(root, text="cmd command2", variable=option_c)
cb_option_c.grid(row=3, column=0, padx=10, pady=10, sticky=W)

btn_run = Button(root, text="Execute", command=startpowershell)
btn_run.grid(row=8, column=3, padx=20, pady=5, sticky=E)

txtarea_output = Text(root, height=10, width=80, fg="red",bd=8)
txtarea_output.grid(row=10, column=0, columnspan=4, padx=10, pady=10)
txtarea_output.config(state=DISABLED)


root.mainloop()

1 个答案:

答案 0 :(得分:0)

那一行

subprocess.call(["powershell.exe", command_gen()])

赢了,因为command_gen()输出被视为唯一的参数,实际上它是几个参数。所以subprocess.call引用了这个论点,而不是你想要的。

此外,您正在使用|,因此您必须激活shell=True

那一行

output = subprocess.check_output(callpowershell, stderr=subprocess.STDOUT, shell=TRUE)

赢了,因为你没有powershell.exe为它添加前缀(另外:TRUE应为True

无论如何,subprocess行是多余的:

output = subprocess.call(["powershell.exe", command_gen()])
output = subprocess.check_output(callpowershell, stderr=subprocess.STDOUT, shell=True)

你想只做一次调用,检索输出:

def runpowershellcommand(callpowershell):
    output = subprocess.check_output("powershell.exe "+callpowershell, stderr=subprocess.STDOUT, shell=True)

这样可行,因为参数中没有空格。

除此之外:小心点:

    command = "get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\  | select setupstring,versionstring"

有效,但你很幸运。它应该是带有原始字符串的前缀,以避免通过python解释转义序列:

   command = r"get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\  | select setupstring,versionstring"