This is almost certainly a string issue. You wrote:
"PsInfo.exe -d -nobanner \\vhabosdev25"
In Python, strings accept various slosh escapes such as \n
to mean a
newline character and so forth, and in keep with that the sequence \\
means a slosh/backslash. This means that the resulting command string
is:
PsInfo.exe -d -nobanner \vhabosdev25
which only has one backslash, unlike your intended command.
The simplest approach is a “raw string”:
r"PsInfo.exe -d -nobanner \\vhabosdev25"
That leading r
indicates this this is a “raw string” and that the
backslash is not specially handled. That would pass it through directly.
The other piece of advice I have is that you should, almost always,
strive to avoid using shell=True
. That passes a single command string
to your system’s shell. That string itself is subject to the shell’s own
syntax parsing. Yet another layer of opportunity for something to mangle
the arguments to the command you intend.
Instead, avoid shell=True
and pass the actual command line arguments
you intend as a list:
x = check_output([r"PsInfo.exe", r"-d", r"-nobanner", r"\vhabosdev25"])
On a UNIX system that is invoked directly; on Windows something equivalent
happens - I believe. The documentation at subprocess — Subprocess management — Python 3.9.6 documentation says:
On Windows, if args is a sequence, it will be converted to a string
in a manner described in Converting an argument sequence to a string
on Windows. This is because the underlying CreateProcess() operates
on strings.
which is detailed here: subprocess — Subprocess management — Python 3.9.6 documentation
Nonetheless, it is better to avoid shell=True
and try always to use a
list - even on Windows it is better to have this handled in a consistent
and well define way.
Cheers,
Cameron Simpson cs@cskk.id.au