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.Shel l")
Set oNet = CreateObject("Wscript.Netw ork")
Set oFSO = CreateObject("Scripting.Fi leSystemOb ject")
strComputer = oNet.ComputerName
'------------------------- ---------- ----- Begin First Array -------------------------- ---------- ---------
cName = Array("HS1-706-WSFAC01","H S1-706-WSF AC02","HS1 -706-WSFAC 03","HS1-7 04-WSFAC01 ","HS1-704 -WSFAC02")
For Each strName In cName
oNamesDic.Add strName, ""
Next
If oNamesDic.Exists(strComput er) 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-HP4 250-R706"
Else 'Match Not Found
'Do Nothing
End If
cName = Nothing
strName = Nothing
'========= End First Array ============
'---------- Begin Second Array ----------------
cName = Array("HS1-704-WSFAC03","H S1-706-WSF AC04","HS1 -706-WSFAC 05","HS1-7 06-WSFAC06 ","HS1-706 -WSFAC07")
For Each strName In cName
oNamesDic.Add strName, ""
Next
If oNamesDic.Exists(strComput er) 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-CAN ONIR2270-R 833"
Else 'Match Not Found
'Do Nothing
End If
cName = Nothing
strName = Nothing
'============ End Second Array ===========
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.Shel
Set oNet = CreateObject("Wscript.Netw
Set oFSO = CreateObject("Scripting.Fi
strComputer = oNet.ComputerName
'-------------------------
cName = Array("HS1-706-WSFAC01","H
For Each strName In cName
oNamesDic.Add strName, ""
Next
If oNamesDic.Exists(strComput
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-HP4
Else 'Match Not Found
'Do Nothing
End If
cName = Nothing
strName = Nothing
'========= End First Array ============
'---------- Begin Second Array ----------------
cName = Array("HS1-704-WSFAC03","H
For Each strName In cName
oNamesDic.Add strName, ""
Next
If oNamesDic.Exists(strComput
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-CAN
Else 'Match Not Found
'Do Nothing
End If
cName = Nothing
strName = Nothing
'============ End Second Array ===========
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Chris
ASKER
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.Shel l")
Set oNet = CreateObject("Wscript.Netw ork")
Set oFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set oNamesDic = CreateObject("scripting.di ctionary")
strComputer = oNet.ComputerName
'------------------------- ---------- ----- Begin First Array -------------------------- ---------- ---------
cName = Array("HS1-706-WSFAC01","H S1-706-WSF AC02","HS1 -706-WSFAC 03","HS1-7 04-WSFAC01 ","HS1-704 -WSFAC02")
For Each strName In cName
oNamesDic.Add strName, ""
Next
If oNamesDic.Exists(strComput er) 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-HP4 250-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","H S1-706-WSF AC04","HS1 -706-WSFAC 05","HS1-7 06-WSFAC06 ","HS1-706 -WSFAC07")
For Each strName In cName
oNamesDic.Add strName, ""
Next
If oNamesDic.Exists(strComput er) 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-CAN ONIR2270-R 833"
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-HP4 250-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-HP4 250-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-HP4 250-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-HP4 250-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-HP4 250-R706
'
'ElseIf oNet.ComputerName = "HS1-706-WSSTU06" Then
' MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"
' oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-Can onIR2270-R 833"
'
'ElseIf oNet.ComputerName = "HS1-706-WSSTU02" Then
' MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"
' oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-Can onIR2270-R 833"
'
'ElseIf oNet.ComputerName = "HS1-706-WSSTU02" Then
' MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"
' oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-Can onIR2270-R 833"
'
'Else
' 'do nothing
'End If
'========================= ===== End of Elimination Portion of Script ========================== ==========
'------------------------- --------- Function Space to Map Drives -------------------------- ---------- ----
Function MapDrive(Drive, FileServer, Share)
If oFSO.DriveExists(FileServe r & 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.AddWindowsPrinterConn ection(Pri ntServer & Printer)
'Wscript.Echo("Mapping printer to " & PrintServer & Printer) '--> only for debugging the script
End If
End Function
'========================= ========== End of Map Printer Function ========================== ========== ====
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.Shel
Set oNet = CreateObject("Wscript.Netw
Set oFSO = CreateObject("Scripting.Fi
Set oNamesDic = CreateObject("scripting.di
strComputer = oNet.ComputerName
'-------------------------
cName = Array("HS1-706-WSFAC01","H
For Each strName In cName
oNamesDic.Add strName, ""
Next
If oNamesDic.Exists(strComput
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-HP4
Else 'Match Not Found
'Do Nothing
End If
oNamesDic.clear
cName = Nothing
strName = Nothing
'=========================
'-------------------------
cName = Array("HS1-704-WSFAC03","H
For Each strName In cName
oNamesDic.Add strName, ""
Next
If oNamesDic.Exists(strComput
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-CAN
Else 'Match Not Found
'Do Nothing
End If
oNamesDic.clear
cName = Nothing
strName = Nothing
'=========================
'-------------------------
'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-HP4
'
'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-HP4
'
'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-HP4
'
'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-HP4
'
'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-HP4
'
'ElseIf oNet.ComputerName = "HS1-706-WSSTU06" Then
' MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"
' oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-Can
'
'ElseIf oNet.ComputerName = "HS1-706-WSSTU02" Then
' MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"
' oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-Can
'
'ElseIf oNet.ComputerName = "HS1-706-WSSTU02" Then
' MapPrinter "\\HS1-706-SVPRT02", "\hs2-CanonIR2270-R833"
' oNet.SetDefaultPrinter "\\HS1-706-SVPRT02\hs1-Can
'
'Else
' 'do nothing
'End If
'=========================
'-------------------------
Function MapDrive(Drive, FileServer, Share)
If oFSO.DriveExists(FileServe
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
'=========================
'-------------------------
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
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.AddWindowsPrinterConn
'Wscript.Echo("Mapping printer to " & PrintServer & Printer) '--> only for debugging the script
End If
End Function
'=========================
oNamesDic.clear
Chris