Previous Section Table of Contents Next Section

A Typical VBScript

Listing 3.1 shows the example script I'll be using in this chapter. This is actually a sneak preview; you'll see this script again in various forms in the next chapter. For now, don't worry much about what the script does or how it works; instead, just focus on what it looks like.

Listing 3.1. LoginScript.vbs. This is the sample I'll be using throughout this chapter.

'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 userObj.Groups

    If oGroup.Name = sGroupName Then

      bIsMember = True

      Exit For

    End If

  Next

  IsMemberOf = bIsMember

End Function

Remember that this script isn't designed to run as-is (skip ahead to the next chapter to find out what it does and what needs to change to make it execute properly in your environment). Instead, it's just intended to represent the structure of a good script.

    Previous Section Table of Contents Next Section