Runing bcdedit from python in Windows 2008 SP2

Posted by Lee-Man on Stack Overflow See other posts from Stack Overflow or by Lee-Man
Published on 2010-01-07T01:13:48Z Indexed on 2010/06/18 7:03 UTC
Read the original article Hit count: 261

Filed under:
|

I do not know windows well, so that may explain my dilemma ...

I am trying to run bcdedit in Windows 2008R2 from Python 2.6.

My Python routine to run a command looks like this:

def run_program(cmd_str):
    """Run the specified command, returning its output as an array of lines"""
    dprint("run_program(%s): entering" % cmd_str)
    cmd_args = cmd_str.split()
    subproc = subprocess.Popen(cmd_args, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE, shell=True)
    (outf, errf) = (subproc.stdout, subproc.stderr)
    olines = outf.readlines()
    elines = errf.readlines()
    if Options.debug:
        if elines:
            dprint('Error output:')
            for line in elines:
                dprint(line.rstrip())
        if olines:
            dprint('Normal output:')
            for line in olines:
                dprint(line.rstrip())
    errf.close()
    outf.close()
    res = subproc.wait()
    dprint('wait result=', res)
    return (res, olines)

I call this function thusly:

(res, o) = run_program('bcdedit /set {current} MSI forcedisable')

This command works when I type it from a cmd window, and it works when I put it in a batch file and run it from a command window (as Administrator, of course).

But when I run it from Python (as Administrator), Python claims it can't find the command, returning:

bcdedit is not recognized as an internal or external command,
operable program or batch file

Also, if I trying running my batch file from Python (which works from the command line), it also fails. I've also tried it with the full path to bcdedit, with the same results.

What is it about calling bcdedit from Python that makes it not found?

Note that I can call other EXE files from Python, so I have some level of confidence that my Python code is sane ... but who knows.

Any help would be most appreciated.

© Stack Overflow or respective owner

Related posts about python

Related posts about windows-vista