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.