[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBScript Array in Active Directory Logon Script

Posted on 2011-10-26
5
Medium Priority
?
326 Views
Last Modified: 2012-06-27
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 ===========
0
Comment
Question by:BRJacketHive
  • 3
  • 2
5 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37034211
Before each new use of the dictionary try clearing the values:

oNamesDic.clear

Chris
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 37034260
Apologies ... read it again and i'm talking rubbish.  But I note you do not instantiate the dictionary so added that as a line below:

Chris
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")
Set oNamesDic = CreateObject("scripting.dictionary")

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"
      Stop
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
    Stop
      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

Open in new window

0
 

Author Comment

by:BRJacketHive
ID: 37034283
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.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37034295
: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
0
 

Author Closing Comment

by:BRJacketHive
ID: 37034337
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 ========================================
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question