Friday, November 16, 2012

VBScript: Logon Script w/Arrays

Every now and again someone enhances and/or improves something that someone else had built. On one such occasion I had earned kudos on using an array for adding printers to Windows clients in Active Directory. A serious time saver when it comes to add/removing printers in the script. Anyway here it is and feel free to modify and use it:


'**********************************************************************************
' Set Environment Variables
'*********************************************************************************
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set WSHShell = WScript.CreateObject("WScript.Shell")

On Error Resume Next

Domain = WSHNetwork.UserDomain
UserName = ""

While UserName = ""
UserName = WSHNetwork.UserName
MyGroups = GetGroups(Domain, UserName)
Wend

'*********************************************************************************
' Main Process:
'*********************************************************************************
GrpMeb UserName
ShowBox

Wscript.Quit

'*********************************************************************************
'Function: GetGroups
'*********************************************************************************
Function GetGroups(Domain, UserName)
Set objUser = GetObject("WinNT://" & Domain & "/" & UserName)
GetGroups=""
For Each objGroup In objUser.Groups
GetGroups=GetGroups & "[" & UCase(objGroup.Name) & "]"
Next
End Function

'********************************************************************************
'Function: InGroup
'********************************************************************************
Function InGroup(strGroup)
InGroup=False
If InStr(MyGroups,"[" & UCase(strGroup) & "]") Then
InGroup=True
End If
End Function

'*********************************************************************************
' MapDrives Subroutine
'*********************************************************************************
Sub MapDrive(sDrive,sShare)
On Error Resume Next
WSHNetwork.RemoveNetworkDrive sDrive, 1, 1
wscript.sleep 1000
Err.Clear
WSHNetwork.MapNetworkDrive sDrive,sShare
End Sub

'********************************************************************************
'Map Drives & Add Printers:
'********************************************************************************
Sub GrpMeb(UNAME)
If INGROUP ("Staff") Then
MapDrive "D:", "\\server\data"
End If

Dim objShell,objNetwork, strLocal, addPrinter, i, Printers

Set objNetwork = CreateObject("WScript.Network")
Set objShell = WScript.CreateObject ("WScript.shell")

Select Case True
Case INGROUP ("User Group1")
Printers = Array("Printer1", "Printer2")
objShell.run "net use U: \\server\Site1\%username%", 0,False

Case INGROUP ("User Group2")
Printers = Array("Printer1", "Printer2")
objShell.run "net use U: \\server\Site2\%username%", 0,False

Case Else

objShell.run "net use U: \\server\Main_Office\%username%", 0,False

'********************************************************************************
'Add Printers for Main Office users based on floor
'********************************************************************************
If Not INGROUP ("Floor1") And INGROUP ("HR") Then
Printers = Array("Printer1 Floor2", "Printer2 Floor2")

Else
Printers = Array("Printer1 Floor1", "Printer2 Floor1")

End IF
END SELECT

'********************************************************************************
'Add Printer Loop
'********************************************************************************
For Each Printer in Printers
objNetwork.AddWindowsPrinterConnection "\\server\" & Printer
Next

Set objShell = Nothing

'********************************************************************************
'Additional Map Drives:
'********************************************************************************
If INGROUP ("Special Project Users") Then
MapDrive "S:", "\\server\special"
End If

If INGROUP ("Apps") Then
MapDrive "A:", "\\server\apps"
End If

End Sub

No comments:

Post a Comment