Link to home
Start Free TrialLog in
Avatar of BRJacketHive
BRJacketHive

asked on

VBScript Array in Active Directory Logon Script

I am having difficulties with our student log on script, and I figured I'd ask for help here. Currently, our log on script processes drive mappings, printer mappings, and icon pushes using If Then Else and If Then Else If statements. They work well enough, but our script is becoming very large and processor intensive. I would like to streamline the script using multiple arrays, to save space, and processor utilization. Below, is the portion of the script I have put in place (it is a portion, and not all encompassing to save space in the post). I need (for the arrays) to process the computer name, not the individual. Keeping this in mind, and knowing that the array of computer names will grow to up to 35 machines per classrooms, and needing about 8 of these arrays in the script...

The Problem: It does not matter if the log in machine is in the array or not, I am getting all drives mapped, and both printers. The computer name I am using is HS1-704-WSFAC03 as a test machine, so, in theory, I should only be getting the maps and printer in the second array. I am getting both upon log on. What am I doing wrong? Any help is appreciated.

Lastly, I am using functions to map my printers and drives. They work perfectly, and are not included in this snippet. If it would help, I can provide the entire sample script.

Option Explicit
On Error Resume Next
Dim oShell, oNet, oFSO, cName, strComputer, oNamesDic, strName, i

Set oShell = CreateObject("Wscript.Shell")
Set oNet = CreateObject("Wscript.Network")
Set oFSO = CreateObject("Scripting.FileSystemObject")

strComputer = oNet.ComputerName
'---------------------------------------- Begin First Array ---------------------------------------------

cName = Array("HS1-706-WSFAC01","HS1-706-WSFAC02","HS1-706-WSFAC03","HS1-704-WSFAC01","HS1-704-WSFAC02")

For Each strName In cName
   oNamesDic.Add strName, "" 
Next

If oNamesDic.Exists(strComputer) Then 'Match Found
      MapDrive "S:", "\\HS1-706-SVFIL07", "\SHARED"
      MapDrive "T:", "\\HS1-706-SVSEC04", "\HOME"
      MapPrinter "\\HS1-706-SVPRT02", "\HS1-HP4250-R706"      
      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\HS1-HP4250-R706"
Else 'Match Not Found
      'Do Nothing
End If

cName = Nothing
strName = Nothing

'========= End First Array ============

'---------- Begin Second Array ----------------

cName = Array("HS1-704-WSFAC03","HS1-706-WSFAC04","HS1-706-WSFAC05","HS1-706-WSFAC06","HS1-706-WSFAC07")

For Each strName In cName
   oNamesDic.Add strName, "" 
Next

If oNamesDic.Exists(strComputer) Then 'Match Found
      MapDrive "W:", "\\HS1-FILE1-SV1", "\USERS"
      MapDrive "Y:", "\\BR2-202-SVFIL03", "\HOME"
      MapPrinter "\\HS1-706-SVPRT02", "\HS2-CANONIR2270-R833"      
      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\HS2-CANONIR2270-R833"
Else 'Match Not Found
      'Do Nothing
End If

cName = Nothing
strName = Nothing

'============ End Second Array ===========
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Before each new use of the dictionary try clearing the values:

oNamesDic.clear

Chris
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BRJacketHive
BRJacketHive

ASKER

Added oNamesDic.clear to the script, just above each cName = Nothing statement. Saved and ran the script. I still get all 4 drive mappings, and both printers.
:o) like I noted above, when I re-read I saw that would be the case but added an update above to actually initialise the dictionary before use which will hopefully help.

Chris
I cant believe I did that... Many thanks.
Added the declaration and it worked beautifully. If anyone would like to use this script, feel free. Included is the entire script.

Option Explicit
On Error Resume Next
Dim oShell, oNet, oFSO, cName, strComputer, oNamesDic, strName, i

Set oShell = CreateObject("Wscript.Shell")
Set oNet = CreateObject("Wscript.Network")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oNamesDic = CreateObject("scripting.dictionary")

strComputer = oNet.ComputerName

'---------------------------------------- Begin First Array ---------------------------------------------

cName = Array("HS1-706-WSFAC01","HS1-706-WSFAC02","HS1-706-WSFAC03","HS1-704-WSFAC01","HS1-704-WSFAC02")

For Each strName In cName
   oNamesDic.Add strName, "" 
Next

If oNamesDic.Exists(strComputer) Then 'Match Found
      MapDrive "S:", "\\HS1-706-SVFIL07", "\SHARED"
      MapDrive "T:", "\\HS1-706-SVSEC04", "\HOME"
      MapPrinter "\\HS1-706-SVPRT02", "\HS1-HP4250-R706"      
      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\HS1-HP4250-R706"
Else 'Match Not Found
      'Do Nothing
End If

oNamesDic.clear
cName = Nothing
strName = Nothing

'======================================== End First Array ===============================================

'-------------------------------------- Begin Second Array ----------------------------------------------

cName = Array("HS1-704-WSFAC03","HS1-706-WSFAC04","HS1-706-WSFAC05","HS1-706-WSFAC06","HS1-706-WSFAC07")

For Each strName In cName
   oNamesDic.Add strName, "" 
Next

If oNamesDic.Exists(strComputer) Then 'Match Found
      MapDrive "W:", "\\HS1-FILE1-SV1", "\USERS"
      MapDrive "Y:", "\\BR2-202-SVFIL03", "\HOME"
      MapPrinter "\\HS1-706-SVPRT02", "\HS2-CANONIR2270-R833"      
      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\HS2-CANONIR2270-R833"
Else 'Match Not Found
      'Do Nothing
End If

oNamesDic.clear
cName = Nothing
strName = Nothing

'======================================= End Second Array ===============================================

'----------------------------- Need to Eliminate in Favor for Arrays ------------------------------------

'If oNet.ComputerName = "HS1-706-WSSTU01" Then
'      MapDrive "S:", "\\HS1-706-SVFIL07", "\SHARED"
'      MapDrive "T:", "\\HS1-706-SVSEC04", "\Home"
'      MapPrinter "\\HS1-706-SVPRT02", "\HS1-HP4250-R706"      
'      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-HP4250-R706
'      
'ElseIf oNet.ComputerName = "HS1-706-WSSTU02" Then
'      MapDrive "S:", "\\HS1-706-SVFIL07", "\SHARED"
'      MapDrive "T:", "\\HS1-706-SVSEC04", "\Home"
'      MapPrinter "\\HS1-706-SVPRT02", "\HS1-HP4250-R706"      
'      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-HP4250-R706
'      
'ElseIf oNet.ComputerName = "HS1-706-WSSTU03" Then
'      MapDrive "S:", "\\HS1-706-SVFIL07", "\SHARED"
'      MapDrive "T:", "\\HS1-706-SVSEC04", "\Home"
'      MapPrinter "\\HS1-706-SVPRT02", "\HS1-HP4250-R706"      
'      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-HP4250-R706
'      
'ElseIf oNet.ComputerName = "HS1-706-WSSTU04" Then
'      MapDrive "S:", "\\HS1-706-SVFIL07", "\SHARED"
'      MapDrive "T:", "\\HS1-706-SVSEC04", "\Home"
'      MapPrinter "\\HS1-706-SVPRT02", "\HS1-HP4250-R706"      
'      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-HP4250-R706
'      
'ElseIf oNet.ComputerName = "HS1-706-WSSTU05" Then
'      MapDrive "S:", "\\HS1-706-SVFIL07", "\SHARED"
'      MapDrive "T:", "\\HS1-706-SVSEC04", "\Home"
'      MapPrinter "\\HS1-706-SVPRT02", "\HS1-HP4250-R706"      
'      oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-HP4250-R706
'      
'ElseIf oNet.ComputerName = "HS1-706-WSSTU06" Then
'      MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"      
'   oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-CanonIR2270-R833"
'  
'ElseIf oNet.ComputerName = "HS1-706-WSSTU02" Then
'      MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"      
'    oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-CanonIR2270-R833"
'    
'ElseIf oNet.ComputerName = "HS1-706-WSSTU02" Then
'      MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"      
'    oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-CanonIR2270-R833"
'    
'Else
'      'do nothing
'End If
'============================== End of Elimination Portion of Script ====================================
'---------------------------------- Function Space to Map Drives ----------------------------------------


Function MapDrive(Drive, FileServer, Share)
 If oFSO.DriveExists(FileServer & Share) = True Then
     oNet.MapNetworkDrive Drive, FileServer & Share
     'Wscript.Echo("Mapping drive " & Drive & " to " & FileServer & Share) '--> only for debugging the script
 Else
     Wscript.Echo("Cannot Map " & Drive & " to " & FileServer & Share & " ,please inform your instructor")
End If
End Function

'=================================== End of Map Drive Function ==========================================

'-------------------------------- Function Space to Map Printers ----------------------------------------

Function MapPrinter(PrintServer, Printer)
 Dim oPrinter, PrtServer, PrtName, Prt
 Prt       = PrintServer & Printer  'Strip backslashes from UNC names
 PrtServer = Left(Prt,InStr(3,Prt, "\") - 1)
 PrtServer = Right(Left(Prt,InStr(3,Prt, "\") - 1), Len(PrtServer) - 2)
 PrtName   = Right(Prt, Len(Prt) - Len(PrtServer) - 3)
 On Error Resume Next
 Set oPrinter = GetObject("WinNT://" & oNet.UserDomain & "/" & PrtServer & "/" & PrtName)
 If (oPrinter.Name = "" OR oPrinter.Class <> "PrintQueue") Then
     'Wscript.Echo("Printer " & PrintServer & Printer & " is not a valid network printer") '--> only for debugging the script
 Else
     oNet.AddWindowsPrinterConnection(PrintServer & Printer)
     'Wscript.Echo("Mapping printer to " & PrintServer & Printer) '--> only for debugging the script
 End If
End Function
'=================================== End of Map Printer Function ========================================