SysPrep in Depth - Part 8 - Automating your Product Keys⚓︎
November 17, 2007
Automate Entering Your non-VLK Product Keys into Sysprep⚓︎
In our environment we have 500+ computers, each with its own product key, and no VLK’s at hand to use. I decided one day that typing in all these keys every time we cloned was getting old, so I sat down to find a way around the issue. I needed a database of all the product keys, and a way to map the keys to the box they came with. After finding out how to do all this I needed a way to use the keys within sysprep.
The idea behind this is to take 10+ keys, 10+ computers, a little bit of laziness, and wanting to get the clones done in a timely manor. Hang with me while I explain this as there are a few steps to getting this all to work.
As we had all the computers running with their own keys already, I figured the easiest way to get the database was to get the key from the computer itself, and not have to walk around to each computer, pull the cover off and write the key down.
I pulled out a product from my toolkit that does just that, ProduKey. This free product will show you your installed keys for various MS applications and has the ability to export this list to a file. With a little scripting I could easily get an exported key for each PC, but this is far from what I needed to get this all working.
I needed a way to get this exported key to map to the computer it came from, so after a little thought, I pulled up a freshly syspreped computer and verified that I could get its MAC address during the sysprep process and guess what,
ipconfig /all would show me exactly what I needed.
Now that I had a product key and a matching mac address, but I wanted to be able to match these two fields to the computer name.
Getting the Database Made⚓︎
Now I had an idea of what was needed and how to create it. I wanted one file that contained a list of all the MAC addresses, Product Keys, and Computer Names and to get all this without having to go touch every PC.
I decided to use AutoIt to write the program which could accomplish the following.
- Use ProduKey to export the computers’ product key
- Parse the exported file to find only the Windows XP key
- Use “ipconfig /all” to get the computers MAC address and export this to a file
- Parse the exported ipconfig file to find only the mac address of the network card
- Grab the computer name
- Throw this all into an ini file for use later on
AutoIt source file to do all this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
If you need help with the code feel free to ask, but for now I will leave it as is. This AutoIt file uses the ProduKey file stored on our server and edits the file on the server as well. The resulting ini file looks something like this.
1 2 3 4
Now that we have our ini file we can move on to getting sysprep to use this file.
Adding our INI to Sysprep⚓︎
Unfortunately, there is no way with this method to have on-the-fly updates to our ini file being pushed out to new clones, meaning that this static database needs to be included in the image of our computers, and that it cannot be stored on a server during the sysprep process. If you know a way around this feel free to correct me, but if I remember correctly the network card drivers are installed before the product key is entered (allowing us to get the mac), but TCP/IP is installed after XP asks for the key…
Anyways, all you need to do after you have tested the above script is get it running on all your computers via a startup script or some other means. To make this a valid ini we need to add one line to the beginning of the file. We need to add a section to it called ProductKeys. To do this add “[ProductKeys]” as the first line in the ini file (without the quotes). Your ini should now look like this:
1 2 3 4 5
Copy this edited ini file to your clone image computer and move on to the next step.
Complications in the Process⚓︎
There is a section within the sysprep.inf file for product keys, so I tried to get the sysprep.inf file edited on boot so this was all automated, but the sysprep.inf file was being cached long before I could get the key written to it. I also wanted to be able to get the ini file I was creating on the network so that I could easily update it, but as I explained earlier this was impossible with this method.
Now I had all the info I needed and no way of getting it working, so now what?
Type the Product Key in Automatically and Dynamically⚓︎
Using another AutoIt file I was able to get this ini file I created to be worth its time. Once we have the computer at the window where it is asking for the product key we can press Shift+f10 and get to a command prompt, type in
ipconfig /all, and if we have done all our drivers right we will see our MAC address.
The following code pulls the mac address from the ini file, and the network card, and tries to find a match. If it does it pulls the product key from the ini and starts typing it in for us. This only enters in the computers product code, but as we have the computer name in or database it would be an easy edit to this code to start typing in computer names as well if we wanted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
Putting this all Together⚓︎
Once you have made the necessary edits to the .au3 files, compile them and you’re ready to go.
Find a way to get your clients to start running the FindProductKey.exe; we were on a domain so creating a computer startup script made this an easy task. Once you have a list of all the computers with their MAC address, product keys, and computer names, (mac2product.ini) we can make our edit to the first line and copy this file off to our clone computer.
On our clone computers we need to place TypeProductKey.exe and mac2product.ini within the same directory. I didn’t want to let someone just get a list of every product key in the building, even though it wouldn’t be hard for them to do, so I placed the database of product keys in the sysprep folder so that after sysprep finishes it is automatically deleted. Once you have TypeProductKey.exe and mac2product.ini contained in your clone you can pull an image and start pushing it out. Once your clones get to the screen asking for the product key simply hit shift+f10 to get a command prompt and start-up TypeProductKey.exe.
I wanted a basic version of mac2product.ini contained on every computer for ease but you could also keep an up-to-date version on a usb key with TypeProductKey.exe for those computers that you know were not in the cloned mac2product.ini. You can also do a fair amount of debugging using TypeProductKey.au3 and autoit3.exe from a usb drive within sysprep if you want to work with automating any other parts of sysprep.
Let me know what you thing about this process and if you have any ways to make it more refined.