I am a huge fan of scripting. If I have to do a certain task on a computer often, then I would definitely try and create an automated script so all I have to do is run one file.
Additionally, having these scripts can give me an edge over my competitors. Lets say I have 2 computers to repair on my workbench. One of them has a tricky issue that requires most of my attention and the other one requires me to do something I have done a hundred times before.
Since I have made a script to do the task I have done a hundred times before, I can focus my attention on the other computer. If the script does the task quicker than I can do manually, it gives me an advantage over my competitors since I can either lower my price because it took less time; or have a fixed price and use the time I saved to work on another computer resulting in more computers fixed on the day.
In this article I am going to show you some scripting basics so you can make some useful computer technician tools for your business. Alright, lets get started. To begin, we’ll work with good old Windows BATCH (.BAT) scripts.
Disclaimer: This article is for people with a good understanding of Windows only. I highly, HIGHLY recommend that you do scripting in either a Virtual Machine environment or a dedicated, unimportant machine in case you make a mistake with one of your commands and do some damage to the system. Technibble or I cannot be held responsible if you do damage to the operating system or lose any data.
Now we have that out of the way, a task that computer technicians frequently have to do is backup their clients data, so lets make a little script to do that.
I am going to want the clients CDKeys for their operating system and maybe Microsoft Office if they have it. While there isnt a command to do this in Windows batch scripting, I can use Nirsofts ProduKey to get it. I really like Nirsofts software because just about everything they release can be run from the command line allowing us to work them into scripts.
Download ProduKey from here (32bit) (64bit version here) and extract the zip somewhere you will remember like your desktop.
In the same folder as the extracted ProduKey.exe, create a new notepad text file and open it. Now we need to find out what the commands are for ProduKey so we can make it work from a script. Goto the Produkey website by clicking here and if we scroll half way down the page we will see “Command-Line Options”. The first table has commands for getting information remotely but we are going to assume we are actually at the computer when we do this. So, look at the first line of the second table and we will see:
/stext <filename> - Save the list of product keys into a regular text file
In our text file we made just a moment ago, write:
produkey.exe /stext Keys.txt
What we are telling the script to do is to run ProduKey and write the CDKeys to a text file called Keys.txt
Now, save this text file as a .BAT file (or save as a TXT, then change the extension to .BAT) in the same directory as the ProduKey.exe
If you did it correctly, when we run this .BAT file it should show a black command window very quickly and a file called Keys.txt should appear. In this Keys.txt file you should see the CD Keys for the installed Microsoft software.
This is a very simple Batch script but lets take it even further. Lets grab the Mail settings too using Nirsofts MailPassView.
Download MailPassView here and extract it to the same directory we put ProduKey.exe in. Now lets goto the MailPassView page here and find out what the command line options are.
It appears that we can also use the /stext <filename> command on MailPassView as well. So lets edit our .BAT file again in Notepad (Right click and choose Edit if you closed Notepad) and add our MailPassView command on a new line.
produkey.exe /stext Keys.txt
mailpv.exe /stext Mail.txt
When you save and run the BAT file, two files called Keys.txt and Mail.txt should be created containing the information we want. There are lots of programs that allow you to do things from the command line like this, but I’ll get to them in a later part of this series.
You know, since I am working with many computers a day, I am going to forget which computer these Keys.txt and Mail.txt belong to, so I am going to separate them.
Microsoft Windows has many built in variables you can use for scripting purposes. If you are new to programming, variables are values that change.
Here is a good example of why we want to use variables. Lets say I wanted to access my desktop in a script. I could do the following:
cd “c:\Documents and Settings\Bryce\Desktop”
But there is one big problem, the computers I will be running this on wont have “Bryce” as a username, it’ll be something else like Admin, Owner, Jeff, John, Jill etc..
So, I’ll need to use a Windows variable called %USERPROFILE%.
Windows will replace this variable with “c:\Documents and Settings\Username” and the Username part will be replaced with the current users Username. If the current user was John, %USERPROFILE% would become “c:\Documents and Settings\John”. So if I wanted to access the current users desktop, regardless of what the username is, I would use: cd “%USERPROFILE%\Desktop”
For future reference, here is a good source listing the Windows Environment Variables.
Now lets get back to separating our Keys.txt and Mail.txt files for each computer. I am going to use the computers name to separate the text files. To do this I am going to fetch the computers name using the %COMPUTERNAME% variable and make a directory of that name, then place our text files in there. The command to make a directory is “md” or “mkdir”.
I personally prefer mkdir so I am tell mkdir to make a directory with the name of the computer, but I am going to put this at the top of the script. The reason why we are going to put it at the top of the script is because we need to make the directory first so next lines can place their files in it:
mkdir "%COMPUTERNAME%"
If my computer was named Bryce-PC, the directory that was just made would also be called Bryce-PC. Now I need to tell ProduKey and MailPassView to put their information in this folder so I am going to change the location it places the files to "%COMPUTERNAME%\Keys.txt"
. I have also added quotation marks around this variable because if the computer name has any spaces in it, it wont work. This is what the entire script should look like now:
mkdir "%COMPUTERNAME%"
produkey.exe /stext "%COMPUTERNAME%\Keys.txt"
mailpv.exe /stext "%COMPUTERNAME%\Mail.txt"
Save these changes and delete the old Keys.txt mail Mail.txt files that might have been created earlier so we dont get confused. Now run the script.
It should create a folder with the name of the computer you are running it on and have the two text files inside it. You could place this script (along with produkey.exe and mailpv.exe of course) on your USB drive and run it on any Windows based PC.
Here is the complete set of files and scripts we talked about above if you are having trouble and need to see what it should look like. I have included the Help Files in this zip file to comply with Nirsoft’s licensing, but they are not needed. However, they do have some command line information in them if you dont want to use the Nirsoft site. Be sure to extract the zip file first before running anything in it.
Thats the end of the first part of our scripting tutorial. In the next part I’ll show you how to create a script to backup the main parts of a system (Desktop, Favorites, My Documents etc..).
You aren’t just limited to creating a backup script or even using Nirsofts tools, you can use any application with command line options but this is a good starting point. If you wish to continue experimenting with this before the next article is posted, maybe you could expand the script to fetch the saved Wireless Keys as well? (if one exists, otherwise it would be blank).
Session expired
Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.
wow looking forward to future instalments
Thanks Bryce!
Great article Bryce, something I’ve been meaning to jump in to.
Excellent article, Bryce and very easy to follow. I’m looking forward to more tutorials.
Yet another great article from Bryce!
A round of applause!
Excellent article.
May it be noted that some anti-virus programs may show a false positive when it comes to the WireKeyView program.
Looks VERY useful.. It happens to me quite alot that I forget the ISP`s mail settings or user name and it takes ages for the ISP to answer.
Great article.
Scripting is very powerful and in larger environments I have worked in, it was a way of life. Logon scripts, backup scripts, you name it. It is a nice way to automate a lot of things that are rather boring, as you pointed out. It is a vast field and the number of great things you can do with it make it worth learning on some level if you are a tech.
Scripting is great in smaller environments also if it saves time and works reliably. In terms of backups scripts, changing drive letters on portable drives is the biggest “gotcha” I find. I have learned to add lines that generate an error text if the script does not complete properly. I am working on a way to identity the drive by name to eliminate that problem.
Any ideas on that one?
PS.
Here is a great windows scripting resource, particularly as a starting place.
http://technet.microsoft.com/en-us/scriptcenter/default.aspx
Brilliant! This is one the best posts yet, Bryce. I’ve worked with computers for about 14 years now, and NEVER got into scripting. You’ve wet my appetite for sure.
Bryce.Thank you so much for your generocity teaching. I am completely new and working from my living room fixing peoples computer. I am learing everyday, technibble is great asset to me.
Not to steal Bryce’s thunder.. There are a ton of scripting resources out there..
BAT
VBS
KiX (KiXtart)
AU3 (AutoIT)
PS1 (PowerShell)
Note: PowerShell is the new kid on the block.
The batch file is probably the most recognized of the scripting community as it is native scripting languages. KiX or KiXtart is one of the older interpreters out there that is written by a Microsoft Employee in his spare time and went into the limelight with the Windows 2000 Resource Kit. VBS or VBScript gained notariety with the Melissa Virus.. AutoIT is not as well know, but has tremendous Power in what it can do. PowerShell is the latest incarnation from Microsoft and is used for scripting a good many components of Exchange and other Enterprise applications..
HTH,
Kent
Thanks Bryce. I am a big fan of scripting. I try to setup a backup script for every user I encounter since they rarely do backups themselves. Scripting a job and then scheduling the task can be a very powerful tool.
I already use the mailpv and produkey programs, but never set up a script to run them.
This is great tip. Thanks a lot, looking forward to part 2.
Heres a script for the rest of Nirsoft’s Password Recovery tools .. For more on this; you know where to go =P And for those who don’t know where to go, don’t need to know =P
> Create a folder called ‘Backup’
> Create a folder in ‘Backup’ called ‘Apps’
> Download all NirSoft’s Password Recovery tools, and place them in the ‘Apps’ folder.
> Copy and paste the following into a .txt document. Save it as BackupKeys.bat.
> Make sure you save it as a .bat and not as .txt.
###############################################
echo Backing Up FireFox Passwords…
start /wait “” “Apps\nirsoft\PasswordFox.exe” /shtml “Backup\Passwords\foxPasswords.html”
echo Backing Up Internet Exporer Passwords…
start /wait “” “Apps\nirsoft\iepv.exe” /shtml “Backup\Passwords\iePasswords.html
echo Backing Up Google Chrome Passwords…
start /wait “” “Apps\nirsoft\ChromePass.exe” /shtml “Backup\Passwords\ChromePasswords.html
echo Backing Up Email Client Passwords…
start /wait “” “Apps\nirsoft\mailpv.exe” /shtml “Backup\Passwords\mailPasswords.html
echo Backing Up Outlook PST Passwords…
start /wait “” “Apps\nirsoft\PstPassword.exe” /shtml “Backup\Passwords\OutlookPSTPasswords.html
echo Backing Up All Dial-Up Passwords…
start /wait “” “Apps\nirsoft\dialupass2.exe” /shtml “Backup\Passwords\DialUpPasswords.html
echo Backing Up Instant Messaging Client Passwords…
start /wait “”all “Apps\nirsoft\mspass.exe” /shtml “Backup\Passwords\InstantMessengerPasswords.html
echo Backing Up Wireless Network Keys…
start /wait “” “Apps\nirsoft\WirelessKeyView.exe” /shtml “Backup\Passwords\WiFiKeys.html
echo Backing Up Software Product Keys
start /wait “” “Apps\nirsoft\ProduKey.exe” /shtml “Backup\Passwords\ProduKey.html
echo.
echo Finished!
Yes, this can be useful in some situations, but not all. Many situations call for simple backup of one or two items. As for time saved and therefor making more money by lowering prices or such, not really! The amount of time saved would have to be close to a half hour or more to really be considered better time saving. Now a program that could be easily changed by selecting items that need to be done (checklist or checkboxes)so that each situation could be incorporated easily, now that could potentially be a time-saver. This could include programs to be installed although such scrips as Ninite already do some important/general installations. And then add to the program the ability to not only utilize other programs silently but also to have one’s company name and logo displayed so client’s don’t see what programs you are using. That would be the beginnings of a very useful tool!
I like this!
This is one of the things in I really wanted to learn.
I’m looking forward for more.
ThanXs!!
jross asked “I am working on a way to identity the drive by name to eliminate that problem. Any ideas on that one?”
This website has a solution: http://www.computing.net/answers/programming/batch-get-a-volume-label-to-a-variable/18899.html
Which contains this code:
:: Code begins…
@echo off
cls
for /f “tokens=1-5*” %%1 in (‘vol’) do (
set volume=%%6 & goto echoit
)
:echoit
echo %volume%
:: Code ends…
Note that you can specify a drive after the vol command: ‘vol c:’
Thanks JimM,
I’ve been trying to figure that one out for a bit. That does the trick!
JimR
Hello,
On my PC, the “BackupScript1.zip” got tagged as a “PasswordRevealer risk” with my Symantec Antivirus product. Nirsoft addresses this and other “false-positive” issues here:
NirSoft Frequently Asked Questions:
http://www.nirsoft.net/faq.html
NirSoft Antivirus “False Positive” Problems:
http://www.nirsoft.net/false_positive_report.html
NirBlog “Antivirus companies cause a big headache to small developers”:
http://blog.nirsoft.net/2009/05/17/antivirus-companies-cause-a-big-headache-to-small-developers/
Just a heads-up, in case other readers experience “false-positive” detections with this script…
Bryce, you’re awesome. Thanks for this article, I love it!
@jross @JimM
It’s even easier than that when it comes to batch scripts. Look into the built-in variables that batches are given: %~dp0 = Drive and Path to the batch script. There’s many variations to the %~ (Batch Path Modifiers) variables. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
Of course, their examples refer to the first argument passed to your script, but 0 refers to the script itself.
Jager,
Are you suggesting that by using some combination of the batch parameter modifiers one can obtain just the volume label of a drive? I looked it over and don’t see a way to do that. In any case, it’s a good resource, I had been writing batch files for many years before I discovered them.
@JimM
Not the volume label, but it fulfills the duty that jross wanted the label for; dynamically adjusting the batch script so that it refers to the proper drive.
%~d0 = Drive that batch is ran from
%~dp0 = Drive and Path (excluding filename) that batch is ran from
I use these little modifiers in just about all of my own scripts and never have any problems with them addressing the wrong drive or path. As an experienced programmer, I do everything I can to avoid hard-coding things like drive letters, etc, so it was a godsend when I discovered that you don’t have to use a complicated script just to get the drive letter, much less hard code the script.
@jross
I found the easiest way to set a drive to always receive the same drive letter when plugged in. Go to my computer, right click – go to manage
In computer management go to STORAGE/disk management.
Right click on the disk you want, and click Change drive letters and paths. Then tell it which letter it should get for that device.
Now every time you plug that device in , it gets the same letter, rather than windows just picking one for it!
Very nice article.
Biterscripting seems to be helpful for automating “things”. Good basic scripting lessons at
http://www.biterscripting.com/LearningScripting/overview.html
Also, do take a look at this post I created for the background of kernel, shell, shell command, shell script.
http://forums.opensuse.org/programming-scripting/418593-learn-shell-scripting.html
Can I say… Wow. Imagine being in computer field for at least 20 years and never got into scripting for I was afraid of them but now, thanks to Bryce, I can make scripting my friend. Thank you Bryce.