HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Login using VBScript

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
loginvbscriptusing

Problem

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

Solution

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

' 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


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

Code Snippets

' 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

Context

StackExchange Code Review Q#83942, answer score: 6

Revisions (0)

No revisions yet.