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 >= 1 Then
Dim udtPOSOne : Set udtPOSOne = New Mapping
With udtPOSOne
.strLocalDrive = "P:"
.strUNCPath = "\\10.0." & STORE_NUMBER & ".101\dpalm"
.strPersistent = "False"
.strUsr = "somename"
.strPas = "somepass"
End With
alMappings.add(udtPOSOne)
End If
' POS 2
If NUM_REGS >= 2 Then
Dim udtPOSTwo : Set udtPOSTwo = New Mapping
With udtPOSTwo
.strLocalDrive = "Q:"
.strUNCPath = "\\10.0." & STORE_NUMBER & ".102\dpalm"
.strPersistent = "False"
.strUsr = "somename"
.strPas = "somepass"
End With
alMappings.add(udtPOSTwo)
End If
' POS 3
If NUM_REGS >= 3 Then
Dim udtPOSThree : Set udtPOSThree = New Mapping
With udtPOSThree
.strLocalDrive = "R:"
.strUNCPath = "\\10.0." & STORE_NUMBER & ".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 <> 0 then
oShell.LogEvent EVENT_FAIL, "Login script failed" & vbcrlf &_
"Error #: " & err.number & vbcrlf & "Error: " & err.description &_
"Failed to map " & udtMapping.strUNCPath & " to " & udtMapping.strLocalDrive
else
oShell.LogEvent EVENT_SUCCESS, "Login script successfully mapped " &_
udtMapping.strUNCPath & " to " & udtMapping.strLocalDrive
end if
Next