A Sript to autodetect and map printers

This is part of a login script, used to automatically map printers in an Active Directory environment. It makes use of permissions set on the printer and active directory group assignments to work properly. If you have a printer “Kappa” that only “Site B” can print to, assign the “Site B” group with the ability to print, and remove the everyone entry. By creating an active directory group called “Kappa” consisting of people that can access the above said printer, this script will automatically set this printer to be their default. As a side note, the IsMember() function works recursively, allowing for nested group memberships.

The Script :

Set WSHNetwork = Wscript.CreateObject("Wscript.Network")

' set the local print server name here
Set servName = "printserv"

' remove old printers here
set cprinter=WSHnetwork.enumprinterconnections
for i=cprinter.count-1 to 1 step -2
on error resume next
if instr(cprinter.item(i),"[")<>0 or instr(cprinter.item(i),"\")<>0 then
WSHnetwork.removeprinterconnection cprinter.item(i)
end if
next


' this is the new manager for attaching printers.
' it enumerates all the printers on the server listed above
' and attempts to attach each of them.
' the ntfs settings for each device dictate which connections
' will be successful, and what is visible.
' the IsMember function (declared in the include.vbs above)
' is used to determine if a printer
' should be set as a default device.

Set cont = GetObject("WinNT://"&servName&",computer")
cont.Filter = Array("PrintQueue")
For Each printer In cont
On Error Resume Next

WScript.Echo "Adding: " & printer.Name
Output = WSHNetwork.AddWindowsPrinterConnection( printer.PrinterPath )
WScript.Echo Output
If IsMember( printer.name ) Then
WScript.Echo "Defaulting To:" & printer.Name
WSHNetwork.SetDefaultPrinter printer.PrinterPath
End If

Next


' The following code has been collected from usenet.

Function IsMember(strGroup)
If IsEmpty(objGroupList) Then
Set objGroupList = CreateObject("Scripting.Dictionary")
Call LoadGroups(objADObject)
End If
IsMember = objGroupList.Exists(strGroup)
End Function
Sub LoadGroups(oADObject)
Dim colstrGroups, objGroup, j
objGroupList.CompareMode = vbTextCompare
colstrGroups = oADObject.memberOf
If IsEmpty(colstrGroups) Then
Exit Sub
End If
If TypeName(colstrGroups) = "String" Then
Set objGroup = GetObject("LDAP://" & colstrGroups)
If Not objGroupList.Exists(objGroup.sAMAccountName) Then
objGroupList(objGroup.sAMAccountName) = True
Call LoadGroups(objGroup)
End If
Set objGroup = Nothing
Exit Sub
End If
For j = 0 To UBound(colstrGroups)
Set objGroup = GetObject("LDAP://" & colstrGroups(j))
If Not objGroupList.Exists(objGroup.sAMAccountName) Then
objGroupList(objGroup.sAMAccountName) = True
Call LoadGroups(objGroup)
End If
Next
Set objGroup = Nothing
End Sub

0 comments:

Post a Comment