2

I want to automatically check if some KB is installed on the machine. Part of my python script:

import subprocess
result = subprocess.run(['cmd', '/c wmic qfe'], shell=True, stdout=subprocess.PIPE)
ftemp = open('mylog.txt','w') #just to check what is going wrong
ftemp.write(str(result.stdout))
ftemp.close()
if str(result.stdout).find('KB2999226')==-1:
    print('Nah, you don't have KB')
    sys.exit()

What I got in the shell while executing:

qfe" - Alias not found.
Nah, you don't have KB

mylog.txt:

b''

So, looks like some stupid problem with dashes or encodings. I've tried variety of commands, but nothing succeded. (Yeah, "dism" causes another tons of errors). Some advices?

12
  • Is the single quote in 'Nah, you don't have KB' a typo? Commented Feb 20, 2018 at 6:30
  • 1
    But the command executes successfully from the cmd? So it's just the python script that causes the error? Commented Feb 20, 2018 at 6:30
  • also, is wmic path win32_quickfixengineering get KB2999226 working? Commented Feb 20, 2018 at 6:42
  • 1) message was in Russian, without spec characters like in "don't". 2) command from cmd works with the same error. 3) ` result = subprocess.run(["cmd", '/c dism /Online /Get-Packages'], shell=True, stdout=subprocess.PIPE) - returns some unreadable text in the result.stdout, while dism /Online /Get-Packages` works perfectly from the cmd. Commented Feb 20, 2018 at 6:56
  • I don't like the single occurance of double-quotes in the error message you got. Is it for sure qfe" - Alias not found. and not "qfe" - Alias not found.??? In the latter case, what happens if you enter from the commandline cmd /c wmic qfe? Commented Feb 20, 2018 at 6:56

2 Answers 2

2

Here is part of your problem:

print('Nah, you don't have KB')

should be

print("Nah, you don't have KB")
Sign up to request clarification or add additional context in comments.

2 Comments

In fact, this message in Russian, without special characters like " ' ". So yeah, my bad - incorrect example
That makes sense and clears up some confusion. Otherwise you'd get a syntax error (which you didn't)
2

Try separating all of the components into element in a list.

Replace:

result = subprocess.run(['cmd', '/c wmic qfe'], shell=True, stdout=subprocess.PIPE)

with

result = subprocess.run(['cmd', '/c', 'wmic', 'qfe'], shell=True, stdout=subprocess.PIPE)

1 Comment

Exactly the same output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.