3

Objective: The company I'm working for recently imaged some computers. Unfortunately with the way they did it, it also tries to use the product key of the original image. We can pull each computer's OEM license and change it back to that in the settings, but I am trying to create a python script to do that quickly.

(please note I am brand new to python, I'm basically looking up how to do this one task at a time and just combining it together.)

Problem: As a part of this process I am getting the license key through command prompt, copying the output to a text file, and then isolating just the actual key in a separate document (the command prompt output includes another line I don't want). When the isolation part happens, python seems to add spaces between all the characters in the product key, meaning it won't copy and paste correctly with the "Change Product Key" input.

Code:

import os

prod_key = os.system("wmic path softwarelicensingservice get OA3xOriginalProductKey > prod_key_output.txt")

#this helps to index file into a variable that we later use to extract the key on its own
lines = []
with open ("prod_key_output.txt", "rt") as readfile:
    for line in readfile:
        lines.append(line)
        
#Should create and write the product key to a new text file.
prod_key_solo = open("prod_key_solo.txt", "w")
prod_key_solo.write(lines[2])
prod_key_solo.close()

The txt file output of the first section (prod_key_output.txt) looks like this (key changed to just x's):

OA3xOriginalProductKey         
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX  

But then the output of the last section (prod_key_solo.txt) looks like this.

 X X X X X - X X X X X - X X X X X - X X X X X - X X X X X

Attempted solutions: I've tried a few different things I could find, like using strip, re, replace, and join. I've tried them all on the "solo" file and also tried removing the extra whitespace from the "output" file first but it still adds the space in "solo". Just not sure where the problem is or how to go about fixing it at this point. To not this post any more gargantuan than it already is here is a link to the code above and all of my little attempts at clearing the spaces in a google drive folder.

Thank you for any help you may be able to give.

  • Why `lines[2]` and not `lines[1]`? – lllrnr101 Apr 19 '21 at 03:35
  • @lllrnr101 it seemed like the "output" file has some extra spaces at the end of the first line, so when I did `lines[1]` it got me nothing but `lines[2]` got me the key (albeit with the spaces) – Isaac Marcus Apr 19 '21 at 03:51
  • If your input does not have space, the output should not have either. Can you print(len(line)) in your loop and then print(len(lines[2])) just to be sure. – lllrnr101 Apr 19 '21 at 03:58
  • @lllrnr101 wweeellll... print(len(line)) output 1, print(len(lines[2])) got me 64 – Isaac Marcus Apr 19 '21 at 04:09

1 Answers1

1

First take a look at Running shell command and capturing the output

TL;DR

import subprocess
command = "wmic path softwarelicensingservice get OA3xOriginalProductKey".split()

result = subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf-8')
first_line, second_line = result.strip().split('\n')

# To clean up the string if you have some messy spacing, try:
prod_key = ''.join(second_line.split())

print(prod_key)

If you need to output to a file:

with open("prod_key_solo.txt", "w") as fout:
    fout.write(prod_key)
alvas
  • 115,346
  • 109
  • 446
  • 738
  • 1
    Dang, this worked perfectly! Thank you! Sorry, this was probably terribly simple, I just don't know enough yet but the job is only for a couple months so I wanted to give it a go. – Isaac Marcus Apr 19 '21 at 04:00
  • 1
    Read the https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output, it helps a lot when you need to run a lot of command lines outside Python =) Have fun coding! – alvas Apr 19 '21 at 04:04