asked on
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY
' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP Function
Dim objMemberships
' The array consists of these elements per line
'<Group Name>|<Printer Share>|<Default Flag>
' The Group Name can be ALL to map a printer for everyone
' and the default flag can be True, False, or omitted
' to set that printer as the default for that group
arrPrinters = Array( _
"ALL|\\server1\CommonPrinter", _
"Sydney-NO-2270MFD-Default|\\fs1\syndeymfd|True", _
"Sydney-NO-2270MFD|\\fs1\sydneymfd" _
)
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Section to map the network printers
For Each strPrinterSpec In arrPrinters
arrBits = Split(strPrinterSpec, "|")
strGroup = arrBits(0)
strPrinter = arrBits(1)
If UBound(arrBits) = 2 Then
blnDefault = arrBits(2)
If LCase(blnDefault) = "true" Then
blnDefault = True
ElseIf LCase(blnDefault) = "false" Then
blnDefault = False
Else
blnDefault = False
End If
Else
blnDefault = False
End If
If IsMemberOfGroup(objUser, strGroup) = True Then
On Error Resume Next
objNetwork.AddWindowsPrinterConnection strPrinter
If Err.Number <> 0 Then
MsgBox "Error mapping printer " & strPrinter & ". Error " & Err.Number & ": " & Err.Description
Err.Clear
ElseIf blnDefault = True Then
objNetwork.SetDefaultPrinter strPrinter
End If
On Error GoTo 0
End If
Next
Function IsMemberOfGroup(objADUser, strGroupCN)
Dim strGroupName
If IsEmpty(objMemberships) = True Then
Set objMemberships = CreateObject("Scripting.Dictionary")
objMemberships.Add LCase("ALL"), 0
If IsNull(objADUser.MemberOf) = False Then
If TypeName(objADUser.MemberOf) = "String" Then
objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0
Else
For Each strGroupName In objADUser.MemberOf
objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0
Next
End If
End If
End If
If objMemberships.Exists(LCase(strGroupCN)) = True Then
IsMemberOfGroup = True
Else
IsMemberOfGroup = False
End If
End Function
Option Explicit
Dim objPrinter
Set objPrinter = CreateObject("WScript.Network")
objPrinter.SetDefaultPrinter "\\ServerName\PrinterName"
' End of example VBScript
ASKER
ASKER
ASKER
The Microsoft Server topic includes all of the legacy versions of the operating system, including the Windows NT 3.1, NT 3.5, NT 4.0 and Windows 2000 and Windows Home Server versions.
TRUSTED BY
https://www.experts-exchange.com/questions/24842436/VBScript-to-assign-default-printer-based-on-user's-group-membership.html