Editor's note: This is a continuing interactive series on scripting that appears on SearchWinComputing.com on a...
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
regular basis. Christa Anderson, a noted authority on the subject, will explain basic scripting concepts and then move on to teach you how to use VBScript to perform common tasks. You can e-mail your questions to email@example.com.
Usually, I focus on larger questions that make a good column, but this time I'd like to take a few smaller questions -- I don't want anyone to feel left out. It's August and a good time to make a little script soup from some reader mail that's been coming in.
Making a variable + string name
Q: I'm used to writing basic scripts, but I'm in a jam. Here is the current situation. We have home folders on a server (approx: 140 users) with their current home folder paths set to DC1USERNAME in their profiles. We will be moving the Home folder to another member server and changing all user folders to administrative shares for security. We need to change the home folder paths for all users to NASusername$. The problem is, I cannot find a variable for either Windows or VBScript for changing all users with the $ for administrative shares. Using %username% will only create the users without the appending $.
A: This isn't hard to do -- we're going to take some code that we used in an existing script and modify it very little by adding a concatenation operand and then appending the appropriate character. Remember operands? You can use them to perform mathematical functions, or compare strings, but you can also use them (the ampersand, in this case) to string pieces of text together. Even though we're working here with text and a variable, it works in the same way.
To add a dollar sign to a folder named after a user (assuming that user's name is represented by sUser and the path to the user home directories was represented by sHome), you'd type:
sHomeDir = sHome&sUser&"$"
rather than this:
sHomeDir = sHome&sUser
That's it. Just make sure that the dollar sign, or whatever string you're adding to the variable, is in quotes with no spaces as I've typed it here.
While we're on the subject, anyone who's using VBScript to set up backups might like to take advantage of another possible naming convention. The Date function returns the current date. Substitute Date for sUser, and you can create a series of backup directories named with the current date.
Killing a process
Q: I found your column while searching for a batch script that I could use to initiate shutdown on my laptop. I have an essential program that runs on Startup. Sadly, there is no support for it currently and Windows will not shutdown normally as long as it is running. I thought I could make (or rather find, since I'm pretty clueless on the programming end) a batch script that would first end this program (it runs as an .exe) and then initiate Windows shutdown. It would be neat if I could make two versions that would go through complete shutdown and another for reboot. Do you by chance know of a pre-made script that I could modify for my needs? Or do you have any suggestions on what to do? I don't know how difficult it would be to write this script, but I'm guessing it's not so uncommon a need.
A: This isn't so much a scripting question as a batch file question. (Scripting is great, but if someone else has already done the hard work, let's take advantage of it!) If you have the Windows 2000 Resource Kit, you have a tool called KILL.exe that can kill processes according to executable name or process ID. (SysInternals makes a variation of this called PSKILL that you can use to terminate processes running on a remote machine.) Terminating a process by its ID allows you to terminate a single instance of a process; terminating it by name terminates all instances of that process on that computer. For your needs here, terminating it by name should work. From Microsoft, a helpful document here: How to kill an orphaned process.
To shut down or reboot the computer, you can use the built-in shutdown command on Windows XP. Here's a link that explains the shutdown process.
In your shoes, I'd combine these two commands into a batch file and create one version for rebooting and one for shutting down.
Should I learn to script?
Finally, let's go with a variant of a question I get from time to time. In this most recent case, the writer asked if she should take a class to learn to script Excel in order to pull large amounts of data from files and graph it, rather than paying a contract developer to do a job she estimates will take only a couple of hours.
I'd love to give you a definite answer, but I don't know. I don't know how quickly you learn this kind of thing, or whether you can afford to take the time to learn to script, or how often you'll need this ability. If you learn how to script to perform this task, you'll have added a skill to your collection of tools, and new skills are great to have. Conversely, your programmer can be tasked on a per-project basis to build a program exactly to your specifications, with you not paying the final bill until it's done to your satisfaction. It might take a skilled programmer two hours to create the tool you need, but a newbie Excel developer might need quite a bit more time than that (not to mention the time required to learn the basics), and that's time you won't be spending on other tasks.
For example, I've been making some renovations to my house -- more precisely, I've been having them done. On the one hand, I've got some basic skills and I think I could acquire more skills if I tried. On the other, it doesn't pay me to learn how to build a wall, install a new door, put up new siding, or (no joke) remove honeybees from my house. Sure, I could probably learn to do these things and it would cost me less money if I did them, but it would take me a lot longer to finish them than it would take for the people I hire, and my first efforts would probably not be as good. I find the opportunity cost of my doing electrical work to be too high because I don't expect to need this skill very often. Therefore, I pay an electrician and do my job so I can pay that electrician.
The short answer is that if you're wondering whether you should learn to script to perform a specific task, balance the opportunity cost -- of you spending the time to learn -- against the amount of money you'll save by doing it yourself. Only then will you know what you should do.
|ABOUT THE AUTHOR:|
| Christa Anderson
When Christa Anderson began working with Windows Server operating systems in 1992, she became increasingly interested in finding more efficient and flexible ways of performing routine tasks. Christa has written extensively about administrative scripting and taught technical sessions on the subject at conferences such as Comdex and CeBIT, helping people who had never done any scripting to write their own scripts in half a day. In addition to her interest in scripting Windows management, Christa is an authority on server-based computing and the program manager for Terminal Services licensing in Longhorn. If you have a scripting question for Christa, please e-mail her at editor@SearchWincomputing.com.