Previous Section Table of Contents Next Section

Creating Modules to Perform Tasks

After you've got your task list nailed down, and you've figured out how to perform each of the tasks in script, you can start designing the modules of the script. I often have to spend a lot of time figuring out how to do things like look up IP addresses or connect to domain controllers; after I've spent that time, I don't ever want to have to do it again. In other words, I want to modularize my scripts, so that difficult or commonly used tasks can be easily cut and pasted into future scripts.

VBScript provides a way for you to write your own functions and statements, making it easy to modularize your code. Most of the time, the tasks your script accomplishes-in this case, mapping drives, getting IP addresses, and so forth-can be easily written as functions and subroutines, which can be easily cut and pasted into future scripts.

For a quick overview of functions and statements, see "What Are Functions?" in Chapter 5. You can see how they fit into a script in "Functions" and "Subroutines" in Chapter 3. Finally, I provide more detail on modular script programming in Chapter 25.

Probably the best way to see how these tasks can be modularized is with an example of the completed login script.

graphics/arrow.gif The Login Script

Listing 4.5 shows what the various functions for the logon script might look like, and also shows how the main script might be written to call on each of these functions.

NOTE

Don't worry for now about how this script actually works. You'll be seeing all of these features again in later chapters, where I'll provide explanations that are more detailed. For now, just focus on how the various things are broken into modules that make them easier to reuse throughout the main script.


Listing 4.5. LoginScript.vbs. This script includes a main script as well as functions, making a modular 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



'--------------------------------------------

' FUNCTIONS

'--------------------------------------------



Sub MapDrive(sLetter, sUNC)

 Set oNet = WScript.CreateObject("WScript.Network")

 oNet.MapNetworkDrive sLetter, sUNC)

End Sub



Function GetIP()

 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

 GetIP = LocalIP

End Function



Sub MapPrinter(sUNC)

 Set oNet = WScript.CreateObject("WScript.Network")

 oNet.AddWindowsPrinterConnection sUNC

 oNet.SetDefaultPrinter sUNC

End Sub



Function IsMemberOf(sGroupName)

  Set oNetwork = CreateObject("WScript.Network")

  sDomain = oNetwork.UserDomain

  sUser = oNetwork.UserName

  bIsMember = False

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

   sUser & ",user")

  For Each oGroup In oUser.Groups

    If oGroup.Name = sGroupName Then

      bIsMember = True

      Exit For

    End If

  Next

  IsMemberOf = bIsMember

End Function

Of course, you'll need to modify this script to suit your environment before you can use it. The universal naming conventions (UNCs), for example, will need to reflect ones that exist in your environment.

graphics/arrow.gif The Login Script-Explained

I'll walk through this script line-by-line and explain what it does. This is the format I'll use for most longer examples in this book: Presenting the script in its entirety first, and then again with line-by-line explanations. Because I haven't yet covered most of the concepts this script is using, I'll provide cross-references where appropriate. That way, you can jump straight to more detailed explanations if you want.

The script starts off by using MsgBox to display a simple message. Notice the comment line, which begins with a single quotation mark. You should use comments to help describe what your script is doing; I'll be sure to do that in all the examples I show you.

For details on MsgBox, see "Displaying Messages" in Chapter 6.


'Display Message

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

Next, the script maps the three drives according to the user's group membership. Notice that each is using the IsMemberOf() function to check the group membership, and the MapDrive subroutine to actually map the drive. Both of these are modules I created; I'll cover how they work in a bit.

For details on If…Then, see "Conditional Execution" in Chapter 10.


'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

Next, the script uses the custom GetIP() function to get the local IP address. Then, I use the InStr() and Mid() functions to pull out the third octet. GetIP() is a function I created, not one that's built into VBScript.

For details on InStr() and Mid(), see Chapter 8.


'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)

Finally, I use the custom MapPrinter command to map a printer based on the third octet.

For details on Select…Case, see "Conditional Execution" in Chapter 10.


'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

Next come the parts of the script that actually do all of the work. First is the MapDrive routine, which simply maps a network drive.

For details on the Network object, see "The Network Object" in Chapter 11.


Sub MapDrive(sLetter, sUNC)

 Set oNet = WScript.CreateObject("WScript.Network")

 oNet.MapNetworkDrive sLetter, sUNC)

End Sub

Next, the GetIP() function retrieves the local IP address by using Windows Management Instrumentation (WMI).

For an introduction to WMI and lots of examples, turn to Chapter 17.


Function GetIP()

 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

 GetIP = LocalIP

End Function

MapPrinter works similarly to MapDrive, only it also sets the mapped printer to be the default.


Sub MapPrinter(sUNC)

 Set oNet = WScript.CreateObject("WScript.Network")

 oNet.AddWindowsPrinterConnection sUNC

 oNet.SetDefaultPrinter sUNC

End Sub

Finally, the IsMemberOf() function checks to see if the current user is a member of the specified user group.


Function IsMemberOf(sGroupName)

  Set oNetwork = CreateObject("WScript.Network")

  sDomain = oNetwork.UserDomain

  sUser = oNetwork.UserName

  bIsMember = False

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

   sUser & ",user")

  For Each oGroup In oUser.Groups

    If oGroup.Name = sGroupName Then

      bIsMember = True

      Exit For

    End If

  Next

  IsMemberOf = bIsMember

End Function

That's it! The new logon script is ready for testing and debugging.

    Previous Section Table of Contents Next Section