WNottsC
asked on
Adding network printers
We have a fair few printers in our organisation and at the moment they are not published in AD and our director does not want the user to have to find a printer to add to their machine but would like the to be automatticaly added.
We have achieved this through a vbscript and an AD group the same name as the printer share that containes the computers the printer shoud be added to. The script will search the group membership of the computers untill it finds one that begins with "Printer Group", at which point it will use the rest of the name of the group which should match a printer share to add the printer. The issue I have not been able to sort out yet is when there are more than one network printer in the room. I need to add a loop to the vbscript to loop through all group membership and run add the printer each time a group starting "Printer Group" is found.
As well as the vbscript issue above surely there is a better way to automatically add the printers to the machines without a group for each printer or to add a group policy for each group if they are published in AD?
We have achieved this through a vbscript and an AD group the same name as the printer share that containes the computers the printer shoud be added to. The script will search the group membership of the computers untill it finds one that begins with "Printer Group", at which point it will use the rest of the name of the group which should match a printer share to add the printer. The issue I have not been able to sort out yet is when there are more than one network printer in the room. I need to add a loop to the vbscript to loop through all group membership and run add the printer each time a group starting "Printer Group" is found.
As well as the vbscript issue above surely there is a better way to automatically add the printers to the machines without a group for each printer or to add a group policy for each group if they are published in AD?
ASKER
did I mention I have 300 plus printers, his would be a large array to write. I just thought microsoft would have developed an easier way but I surpose that its what published printers in AD is for.
Eeek! OK, so that's out of the question!
Well, in your current script, does it query the user's group, but just stop after one? Could you show that group enumeration? It should be easy enough to be able to add a user to multiple groups and add the printer from each one.
Rob.
Well, in your current script, does it query the user's group, but just stop after one? Could you show that group enumeration? It should be easy enough to be able to add a user to multiple groups and add the printer from each one.
Rob.
ASKER
I have added the code I use for adding a printer. I think what I need is for the script to include a loop so it goes through every group member forthe computer and then if this starts with "Printer Group " it should then run the sub.
Dim sDNSDomain, oTrans, sNetBiosDomain, sAdsPath, sComputer, oUser
Dim colGroups, strGroup, v
Dim WshNetwork, PrinterShare
Set WshNetwork = WScript.CreateObject("WScript.Network")
' Determine DNS domain name from RootDSE object.
Dim oRoot : Set oRoot = GetObject("LDAP://RootDSE")
sDNSDomain = oRoot.Get("DefaultNamingContext")
' Use the NameTranslate object to find the NetBIOS domain name from the DNS domain name.
Set oTrans = CreateObject("NameTranslate")
oTrans.Init 3, sDNSDomain
oTrans.Set 1, sDNSDomain
sNetBIOSDomain = oTrans.Get(3)
' Use NameTranslate to convert the NT user name to the DistinguishedName required for the LDAP provider.
oTrans.Init 1, Left(sNetBIOSDomain, Len(sNetBIOSDomain) - 1)
oTrans.Set 3, sNetBIOSDomain & sNTName
sAdsPath = oTrans.Get(1)
' Bind to the user object in AD with the LDAP provider.
Set oUser = GetObject("LDAP://" & sAdsPath)
' Use NameTranslate to convert the NT name of the computer to the DistinguishedName required by LDAP.
' Computer names must end with "$".
sComputer = WshNetwork.ComputerName
oTrans.Set 3, sNetBIOSDomain & sComputer & "$"
sAdsPath = oTrans.Get(1)
' Bind to the computer object in AD with the LDAP provider.
Set oComputer = GetObject("LDAP://" & sAdsPath)
AddPrinter sAdsPath, "Printer Group " 'Modified by Wayne Hale
Sub AddPrinter(ByVal UserOrComputerDN, ByVal GroupName)
On Error Resume Next
Dim objUserOrComputer
Dim objGroup
Dim sGroup
Dim colGroups
'Bind user or computer AD object.
Set objUserOrComputer = GetObject("LDAP://" & UserOrComputerDN)
If ((Err.Number = 0) And (IsObject(objUserOrComputer))) Then
'Get the collection of groups the object is a member of.
colGroups = objUserOrComputer.MemberOf
If (Not IsEmpty(colGroups)) Then
'Object is a member of multiple groups.
'Iterate through the groups the user is a member of and see
'if any of them match the specified group.
'Loop through all groups
For Each sGroup In colGroups
Set objGroup = GetObject("LDAP://" & sGroup)
If ((Err.Number = 0) And (IsObject(objGroup))) Then
'Check if group matches search criteria
If InStr(LCase(objGroup.CN), LCase(GroupName)) Then 'Modified by Wayne Hale
'Add group to list of matching groups
'MsgBox "Printer Group Found"
WshNetwork.AddWindowsPrinterConnection "\\PRINT-server\" & LTrim(Replace(objGroup.CN, GroupName, "")) 'Modified by Wayne Hale
WshNetwork.SetDefaultPrinter "\\PRINT-server\" & LTrim(Replace(objGroup.CN, GroupName, ""))
End If
End If
Next
End If
End If
'Dispose objects
Set objGroup = Nothing
Set objUserOrComputer = Nothing
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Regards,
Rob.
Open in new window