Previous Section Table of Contents Next Section

Main Script

The main script performs a good bit of work. Here it is in its entirety.


'Display Message

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



'Map N: Drive

If IsMemberOf("Domain Users") Then

 MapDrive("N:","\\Server\Users")

End If



'Map S: Drive

If IsMemberOf("Research") Then

 MapDrive("R:","\\Server2\Research")

End If



'Map R: Drive

If IsMemberOf("Sales") Then

 MapDrive("S:","\\Server2\SalesDocs")

End If



'Get IP address

sIP = GetIP()



'Figure out 3rd octet

iFirstDot = InStr(0,sIP,".")

iSecondDot = InStr(iFirstDot+1,sIP,".")

iThirdDot = InStr(iSecondDot+1,sIP,".")

sThirdOctet = Mid(sIP, iSecondDot+1, _

 Len(sIP)-iThirdDot)



'Map printer based on octet

Select Case sThirdOctet

 Case "100"

  MapPrinter "\\NYDC\HPColor3"

 Case "110"

  MapPrinter "\\LADC\HP6"

 Case "120"

  MapPrinter "\\TXDC1\LaserJet"

End Select

The main script does act as a sort of table of contents, organizing the flow of the overall script. For example, notice where the MapDrive and MapPrinter subroutines are used, and where the IsMemberOf() and GetIP() functions are used. The main script also utilizes some of VBScript's intrinsic functions, such as InStr() and Mid(). The main script acts as sort of a conductor, orchestrating the flow of the tasks that need to be completed, and calling on specialists-the functions and subroutines-to perform specialized tasks.

TIP

You'll notice that the script uses subs and functions for some things, but uses in-line code for other things, like figuring out the third octet of an IP address. My general rule is to use functions and subs whenever I think the code will be useful elsewhere, or will be used more than once. Otherwise, I just use in-line code in the main script.


In the next few sections, I'll point out specific portions of the main script for you to pay attention to. Again, don't worry much about what these do or how they work; focus for now on the overall structure of the script and how the different pieces fit together. In the next chapter, you'll see how this script went together and what each line does.

Using Custom Functions and Subroutines

Where does the main script call on custom functions and subroutines? I've boldfaced the custom bits in this version of the script.


'Display Message

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



'Map N: Drive

If IsMemberOf("Domain Users") Then

 MapDrive("N:","\\Server\Users")

End If



'Map S: Drive

If IsMemberOf("Research") Then

 MapDrive("R:","\\Server2\Research")

End If



'Map R: Drive

If IsMemberOf("Sales") Then

 MapDrive("S:","\\Server2\SalesDocs")

End If



'Get IP address

sIP = GetIP()



'Figure out 3rd octet

iFirstDot = InStr(0,sIP,".")

iSecondDot = InStr(iFirstDot+1,sIP,".")

iThirdDot = InStr(iSecondDot+1,sIP,".")

sThirdOctet = Mid(sIP, iSecondDot+1, _

 Len(sIP)-iThirdDot)



'Map printer based on octet

Select Case sThirdOctet

 Case "100"

  MapPrinter "\\NYDC\HPColor3"

 Case "110"

  MapPrinter "\\LADC\HP6"

 Case "120"

  MapPrinter "\\TXDC1\LaserJet"

End Select

You can see how using custom functions and subs saves a lot of typing and a lot of room. For example, without the custom function IsMember(), the script would have to look like this (this time, I've boldfaced the changes so they're easy to spot):


'Display Message

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



'Map N: Drive

Set oNetwork = CreateObject("WScript.Network")

sDomain = oNetwork.UserDomain

sUser = oNetwork.UserName

Set oUser = GetObject("WinNT://" & sDomain & "/" & _

 sUser & ",user")

For Each oGroup In oUser.Groups

  If oGroup.Name = "Domain Users" Then

    MapDrive("N:","\\Server\Users")

    Exit For

  End If

Next



'Map R: Drive

For Each oGroup In oUser.Groups

  If oGroup.Name = "Research" Then

    MapDrive("R:","\\Server2\ResearchDocs")

    Exit For

  End If

Next



'Map S: Drive

For Each oGroup In oUser.Groups

  If oGroup.Name = "Sales" Then

    MapDrive("S:","\\Server2\SalesDocs")

    Exit For

  End If

Next



'Get IP address

sIP = GetIP()



'Figure out 3rd octet

iFirstDot = InStr(0,sIP,".")

iSecondDot = InStr(iFirstDot+1,sIP,".")

iThirdDot = InStr(iSecondDot+1,sIP,".")

sThirdOctet = Mid(sIP, iSecondDot+1, _

 Len(sIP)-iThirdDot)



'Map printer based on octet

Select Case sThirdOctet

 Case "100"

  MapPrinter "\\NYDC\HPColor3"

 Case "110"

  MapPrinter "\\LADC\HP6"

 Case "120"

  MapPrinter "\\TXDC1\LaserJet"

End Select

As you can see, the script is a lot easier to read when the repeated code is pulled into a custom function. Also, the function makes the script easier to maintain; if you find a bug, you only have to fix it in the function. If you haven't used functions, you have to go fix the bug everywhere you used the code. For example, suppose I'd used the wrong syntax for For Each oGroup in oUser.Groups. In the original script, I'd just have to fix it in the IsMemberOf() function. In the revised script, without the function, I'd have to make the fix three separate times.

Using Intrinsic Functions and Statements

Where is the script using built-in functions and statements? I'll boldface them to call them out.


'Display Message

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



'Map N: Drive

If IsMemberOf("Domain Users") Then

 MapDrive("N:","\\Server\Users")

End If



'Map S: Drive

If IsMemberOf("Research") Then

 MapDrive("R:","\\Server2\Research")

End If



'Map R: Drive

If IsMemberOf("Sales") Then

 MapDrive("S:","\\Server2\SalesDocs")

End If



'Get IP address

sIP = GetIP()



'Figure out 3rd octet

iFirstDot = InStr(0,sIP,".")

iSecondDot = InStr(iFirstDot+1,sIP,".")

iThirdDot = InStr(iSecondDot+1,sIP,".")

sThirdOctet = Mid(sIP, iSecondDot+1, _

 Len(sIP)-iThirdDot)



'Map printer based on octet

Select Case sThirdOctet

 Case "100"

  MapPrinter "\\NYDC\HPColor3"

 Case "110"

  MapPrinter "\\LADC\HP6"

 Case "120"

  MapPrinter "\\TXDC1\LaserJet"

End Select

You can spot the built-in ones because they don't have a corresponding Function or Sub statement later in the script. If you're curious about what these do, check out the VBScript documentation, or flip through Chapters 8 and 10.

NOTE

Notice that the intrinsic and custom functions and statements look identical. The only way to tell them apart is that the custom ones are defined somewhere in the script by the Function or Sub keywords. Right now, you just need to be adept at spotting the differences between the two.

For a custom function or sub, the only way to tell how it works is to read the corresponding Function or Sub block and figure out what's going on. For intrinsic functions and statements, you can look them up in the VBScript documentation to see how they work.


Making Decisions in a Script

Sometimes, you need a script to do something different based on a set of circumstances. The sample script does this:


'Map printer based on octet

Select Case sThirdOctet

 Case "100"

  MapPrinter "\\NYDC\HPColor3"

 Case "110"

  MapPrinter "\\LADC\HP6"

 Case "120"

  MapPrinter "\\TXDC1\LaserJet"

End Select

The Select…Case construct is making a decision, mapping a different printer based on the third octet of the user's IP address. Select…Case is a special kind of intrinsic VBScript statement, one that helps your script to react to changing conditions automatically by building some kind of logic into the script. For more information on Select…Case, see "Conditional Execution" in Chapter 10.

    Previous Section Table of Contents Next Section