Login using VBScript I was tasked with re-writing a login VBScript we use on about 50 machines. The original script was hacked together by someone who clearly had no idea what they were doing (multiple lines that literally did nothing, including creating persistent shares, deleting them, then recreating them 4+ times in a ForEach loop)

I don't have any scripting experience in vbs, so I'm assuming there's a good amount of stuff here that should be cleaned up. The ultimate goal is to create up to three mapped drives (P:, Q:, R:) that point at registers in the location. Every site should have an identical set up other than the first two Consts, for ease of re-use.

Option Explicit

Const STORE_NUMBER = "29"
Const NUM_REGS = 3

Const EVENT_SUCCESS = 0
Const EVENT_FAIL = 1

Class Mapping
    Public strLocalDrive
    Public strUNCPath
    Public strPersistent
    Public strUsr
    Public strPas
End Class

Dim alMappings : Set alMappings = CreateObject("System.Collections.ArrayList")

Dim objNetwork : Set objNetwork = WScript.CreateObject("WScript.Network")
Dim oShell     : Set oShell = CreateObject("WScript.Shell")

' ## Mapping Objects ##

' POS 1
If NUM_REGS &gt;= 1 Then
    Dim udtPOSOne : Set udtPOSOne = New Mapping
    With udtPOSOne
         .strLocalDrive = "P:"
         .strUNCPath = "\\10.0." &amp; STORE_NUMBER &amp; ".101\dpalm"
         .strPersistent = "False"
         .strUsr = "somename"
         .strPas = "somepass"
    End With
    alMappings.add(udtPOSOne)
End If

' POS 2
If NUM_REGS &gt;= 2 Then
    Dim udtPOSTwo : Set udtPOSTwo = New Mapping
    With udtPOSTwo
         .strLocalDrive = "Q:"
         .strUNCPath = "\\10.0." &amp; STORE_NUMBER &amp; ".102\dpalm"
         .strPersistent = "False"
         .strUsr = "somename"
         .strPas = "somepass"
    End With
    alMappings.add(udtPOSTwo)
End If

' POS 3
If NUM_REGS &gt;= 3 Then
    Dim udtPOSThree : Set udtPOSThree = New Mapping
    With udtPOSThree
         .strLocalDrive = "R:"
         .strUNCPath = "\\10.0." &amp; STORE_NUMBER &amp; ".103\dpalm"
         .strPersistent = "False"
         .strUsr = "somename"
         .strPas = "somepass"
    End With
    alMappings.add(udtPOSThree)
End If

oShell.LogEvent EVENT_SUCCESS, "Mappings built"

Dim udtMapping
For Each udtMapping in alMappings
    ' Unmap the drive if it's currently mapped
    On Error resume Next
    objNetwork.RemoveNetworkDrive udtMapping.strLocalDrive, True
    On Error goto 0

    ' Map the drive
    objNetwork.MapNetworkDrive udtMapping.strLocalDrive, udtMapping.strUNCPath, _
                               udtMapping.strPersistent, udtMapping.strUsr, _
                               udtMapping.strPas

    ' Windows event logging
    if err.number &lt;&gt; 0 then
        oShell.LogEvent EVENT_FAIL, "Login script failed" &amp; vbcrlf &amp;_
                "Error #: " &amp; err.number &amp; vbcrlf &amp; "Error: " &amp; err.description &amp;_
                "Failed to map " &amp; udtMapping.strUNCPath &amp; " to " &amp; udtMapping.strLocalDrive
    else
        oShell.LogEvent EVENT_SUCCESS, "Login script successfully mapped " &amp;_
                udtMapping.strUNCPath &amp; " to " &amp; udtMapping.strLocalDrive
    end if

Next
Just a quick little note: that chunk here, should be parameterized and moved into a separate procedure:


' POS 1
If NUM_REGS &gt;= 1 Then
    Dim udtPOSOne : Set udtPOSOne = New Mapping
    With udtPOSOne
         .strLocalDrive = "P:"
         .strUNCPath = "\\10.0." &amp; STORE_NUMBER &amp; ".101\dpalm"
         .strPersistent = "False"
         .strUsr = "somename"
         .strPas = "somepass"
    End With
    alMappings.add(udtPOSOne)
End If



...instead of being copied over and repeated 3 times with a different strLocalDrive and strUNCPath.