Previous Section Table of Contents Next Section

Selecting the Appropriate Tools

Now comes what is truly the most difficult part of administrative scripting: selecting the right tools. You know what you want your script to do, and you know VBScript can do it (or you at least hope it can), so you just need to figure out how to make it work.

Software developers do this all the time. Typically, they know so much about the tools they have to work with, though, that they select the right ones without even thinking about it. As an administrator, I'm more likely to have to do some research first.

Looking at the task list, there are really six types of tasks I need the computer to perform:

  1. Displaying a message

  2. Mapping a drive

  3. Checking group membership

  4. Mapping a printer

  5. Getting the local IP address

  6. Getting the third octet from the IP address

I'll show you how I research each of these tasks to figure out how they can be accomplished.

TIP

The Appendix of this book is a quick script reference. It's designed to list common administrative tasks and briefly describe what VBScript tools you can use to accomplish them. This reference should make it easier for you to figure out which tools to use when you write your own scripts.

Displaying a Message

I always start my research in the VBScript documentation. You can find it online at www.microsoft.com/scripting, and there's even a downloadable version that you can use offline. You can also find the documentation in the MSDN Library. That's available online at http://msdn.microsoft.com/library, or you can receive it on CD or DVD as part of a yearly subscription. In either case, I find an offline version of the docs to be more convenient.

TIP

At the very least, download the offline VBScript documentation. Go to www.microsoft.com/scripting and look for the appropriate link. The actual download URL changes from time to time, so you're better off starting at the main scripting page and locating the link.


When I need to search the VBScript documentation, I usually start with the alphabetical list of available functions and commands. That's just an easy way for me to scan through the docs and spot likely looking tools. In this case, down in the M section of the function list, I ran across MsgBox. Even if you know nothing about VBScript, MsgBox certainly sounds as if it displays a message box. Looking into the details of the function, I see that it does, in fact, display a dialog box with a message in it. I can specify the message, the title of the dialog box, and which buttons and icons appear on the dialog box. Sounds perfect for my welcome message.

There's no need at this point in the design process to actually start writing script. However, MsgBox appears to be a simple command.


'Display welcome message

MsgBox "Welcome to BrainCore.Net. You are now logged on."

Suppose you don't want to browse through the VBScript documentation (and you don't have this book handy). First, I definitely recommend the browsing method, because it exposes you to a lot of other functions and commands that might be useful later in life. Still, if that's just not your way of working, you can always fall back on my favorite search engine, Google.

Go up to www.google.com and enter a search phrase. Here are some tips.

  • I always use "VBScript" as my first search term, because it narrows down Google's billions of Web pages to those that deal with VBScript.

  • I always include "-browser" in the phrase. Doing so eliminates a lot of pages that talk about using VBScript in Dynamic HTML (DHTML) Web pages, which isn't what I'm usually looking for.

  • Finally, include a term that describes what you're trying to do. In this case, "display message" should do the trick.

TIP

Use quotes carefully in a search phrase. For example, if you type display message, you'll get hits that include both words, and hits that include just one of the two words. If you include "display message" in quotes, you'll only get hits that have the entire phrase "display message" in the page. That might not be helpful in this case; it's more likely the pages will contain something like "display a message" or "display the message." Using display and message outside of quotes will find these pages; using "display message" in quotes won't.


With the Google search phrase vbscript display message -browser, my first hit is a Web site on GeoCities that describes how to use the VBScript MsgBox function. Farther down the first page of hits is a page entitled, "VBScript MsgBox Function," which could work, too.

Google's great for finding example scripts that do what you want, and I'll be using it a lot more as I try to figure out how to perform more complicated tasks.

I'll show you how to use the MsgBox function in more detail in Chapter 6, under "Displaying Messages."

Mapping a Drive

Speaking of more complicated tasks, this one's a bit more difficult. There are plenty of command-line programs that can map a drive, including the easy-to-use net command, but that's cheating. I'm looking for a way to do it in VBScript.

Running through the VBScript documentation doesn't provide any help, either. I don't see any commands with "map" or "drive" anywhere in them. I do see something about a Drive object, but that seems to have something to do with accessing drives. I need to map it before that'll be possible.

Back to Google. Searching for vbscript map drive -browser doesn't return anything helpful in the first page of hits, so I'll need to be a little more creative. Searching for vbscript "maps a drive" -browser gets me a promising article in the first page of hits. Clicking on the first hit, I find myself at MyITForum.com (www.myitforum.com/articles/11/section.asp?w=2&au=lduncan). There's a list of articles here by Larry Duncan, and there are actually two that look useful: How do I retrieve the IP Address using VBScript, and How can I map a drive using WSH? WSH, VBScript, whatever. It's worth a look.

TIP

The terms VBScript, ADSI, WSH, and WMI are interchangeable when you're looking through search results. They're all more or less a part of the larger world of administrative scripting.


Larry's article is actually a short snippet of VBScript code that uses just two lines of code. It uses the WScript.Network object, which seems to have a command called MapNetworkDrive associated with it. No need to go into more detail right now; this is the information I was looking for. I'll bookmark the URL for later reference and go on to the next task.

I cover the WScript.Network object, too; look in Chapter 11 under "The Network Object."

Checking Group Membership

This task also seems complicated. This time, I'm not even going to bother with the VBScript docs, because I've been through them twice already and I don't remember seeing anything even remotely related to group membership. On to Google, where I search for vbscript group membership -browser. The first hit is entitled Detecting Group Membership using VBScript. Perfect!

The link takes me to www.sanx.org/tipShow.asp?articleRef=66. The article in question provides an example script. It's actually a complete function, where I just provide the name of a group, and the function tells me if the currently logged-on user is a member of that group or not. Great! Another bookmark in the browser, and I'll come back to the example when it's time to start writing the script.

Mapping a Printer

This is a place where a little logic can save some time. I already discovered this WScript.Network thing, which maps drives. Surely, it also maps printers, right? Searching the MSDN Library for WScript.Network takes me to the documentation for that object, which does in fact include an AddWindowsPrinterConnection. I also find that it can set the default printer for the current user, which means it's exactly what I need. No need to perform any more complicated search than that, and I can review the documentation later to figure out how to use it. Right now, it's enough to know that it'll do what I need it to do.

Getting the Local IP Address

I already found out how to do this, based on the list of Larry Duncan articles I ran across when looking for drive mapping techniques. Larry's article is at www.myitforum.com/articles/11/view.asp?id-3340, and it's a brief example of how to get the local IP address from within a script. At the very end of the script is the command MsgBox Line. I know that MsgBox displays a message, so it appears as if Line is a variable that contains the IP address I'm looking for. Keep in mind that I need to work with that IP address a little bit, so it's important for me to adapt Larry's script to my purposes.

Larry's script seems to be able to list all of the IP addresses associated with a computer. That's an important thing to understand! Remember that a computer usually has multiple network adapters. One of them might be a FireWire port, another might be a wireless network card, and still another might be an Ethernet card. Even if none of them is connected, they all have an IP address-even if it's 0.0.0.0.

This makes my task a bit more complicated. I was thinking I just had to pull out the IP address, but in fact, it looks like I have to pull all of the IP addresses, and then look for one that isn't 0.0.0.0. Looking again at Larry's example, it might be worth taking a quick guess at what my own IP address script might look like. Listing 4.4 shows an example. I don't know if this is perfect yet, but it's a guess.

TIP

You'll see some code in the next few examples that won't make much sense. Remember: You're only on Chapter 4! I'm using this example because it's something you can use immediately in your environment. I promise you'll see these again in later chapters, where I'll also explain what each line of code is doing.


Listing 4.4. Retrieve IP Address. I'll need to test this script later and figure out more about how it works so that I can make sure it'll work in my logon script.

Set myObj = GetObject("winmgmts:{impersonationLevel=" & _

 "impersonate}" & _

 "!//localhost".ExecQuery _

 ("select IPAddress from Win32_" & _

 "NetworkingAdapterConfiguration" & _

 " where IPEnabled=TRUE")



'Go through the addresses

For Each IPAddress in myObj

 If IPAddress.IPAddress(0) <> "0.0.0.0" Then

  LocalIP = IPAddress.IPAddress(0)

  Exit For

 End If

Next

MsgBox LocalIP

Larry's script was saving all of the IP addresses, so I just looked for the section of his script that seemed to be pulling the IP address out of the computer. I added an If…Then section to grab the first IP address that isn't 0.0.0.0. I'll try it later to see how it works.

I'm getting a little ahead of myself, but if you want to check out using If…Then, turn to Chapter 10 and look under "Conditional Execution." I cover variables in Chapter 5 under "What Are Variables?" and the rest of this script uses WMI, which I introduce in Chapter 17.

Anyway, it looks like I'll have a variable named LocalIP that contains my local IP address, which is exactly what I wanted.

Getting the Third Octet from the IP Address

With my IP address in a variable, I need to figure out how to get just the third octet. Now, this seems like it could be harder than it looks. I can't just grab the ninth, tenth, and eleventh characters from the IP address, because in an address like "10.123.52.4," that wouldn't be right. What I need to do is what I put into my task list: Look for the location of the second and third periods, and then grab everything in between.

Back to the VBScript function list. It turns out there are two functions that might work: InStr(), which returns the specific location of a specific character, and Mid(), which grabs characters out of the middle of a string variable. These two look like they'll do the job, so I'm not going to worry too much about exactly how they work. I know I need to do something like this:

  • Use InStr() to get the location of the first period. This way, I'll know that the next one is the second one.

  • Use InStr() to get the location of the second period.

  • Use Instr() to get the location of the third period.

  • Use Mid() to grab everything in between the second and third periods.

All Tasks Accounted For

It was a bit of work, but I think I know how to do everything I need my script to do. Hopefully, this helps you see how I go about figuring these things out; it's certainly not as easy as just sitting down and starting to type lines of VBScript! A little bit of up-front research is necessary, although it's not usually too hard. The Web, fortunately, is loaded with examples (as is this book), and you can usually find something that does what you want, or at least points you in the right direction.

    Previous Section Table of Contents Next Section