Help with VBS Printer script

I'm trying to modify or even just get working, this script I found online to map printers based on group membership in AD - I think in theory it will work great, however it doesn't seem to work for me.  I'm usually OK to make vbscrips scripts work, but this one has me scratching my head a bit.

The code pasted below is from the website: "TechNet Scriptcenter" I did not include the modifications I've already made so not to confuse it with my changes.
on error resume next 
Dim objADSystemInfo, objUser, objMemberOf, objGroup, objGroupEnum, objNetwork, objPrinter 
dim i, bTroubleFlag 
 
Set objNetwork = CreateObject("Wscript.Network") 
 
'Get current user info from active directory Set objADSystemInfo = CreateObject("ADSystemInfo") 'bind to current user in active directory set objUser = GetObject("LDAP://" & objADSystemInfo.UserName) 
 
if objuser.description = "printer.trouble" then 
    bTroubleFlag = true 
    msgbox "Troubleshooting Printer Logon Script" 
end if 
 
Set objPrinter = objNetwork.EnumPrinterConnections 'Test to see if we have any printers mapped If objPrinter.Count > 0 Then 
    'The Printer array is Printer name, printer path that is why it is step 2 
    for i=1 to objPrinter.Count Step 2 
        'test to make sure it is a network printer 
        if instr(objPrinter.Item(i),"\\") <> 0 then 
            if bTroubleFlag then 
                msgbox "Deleting:" & vbcrlf & objPrinter.Item(i) 
            end if 
            objNetwork.RemovePrinterConnection objPrinter.Item(i),true,true 
        end if 
    next 
end if 
 
 
'Get an array of group names that the user is a member of objMemberOf = objUser.MemberOf for Each objGroup in objMemberOf 
    'Test to see if it is a printer group. all printer groups should be in the same OU 
    if (instr(objGroup,"OU=Printer-Groups") <> 0) then 
        'Bind to the group to get is description. The description contain the path to the printer 
        set objGroupEnum = GetObject("LDAP://" & objGroup) 
        if bTroubleFlag then 
            msgbox "Adding:" & vbcrlf & "[" & objGroupEnum.name & "]" & vbcrlf & objGroupEnum.description 
        end if 
        objNetwork.AddWindowsPrinterConnection objGroupEnum.description 
        set objGroupEnum = nothing 
    end if 
next 
'Repeat as above for the default printer for Each objGroup in objMemberOf 
    if (instr(objGroup,"OU=Printer-Default-Groups") <> 0) then 
        set objGroupEnum = GetObject("LDAP://" & objGroup) 
        if bTroubleFlag then 
            msgbox "Setting Default:" & vbcrlf & "[" & objGroupEnum.name & "]" & vbcrlf & objGroupEnum.description 
        end if 
        objNetwork.SetDefaultPrinter objGroupEnum.description 
        set objGroupEnum = nothing 
    end if
next
 
if bTroubleFlag then 
    msgbox "Printer Logon Script Finished" 
end if 

Open in new window


I get a lot of errors about unexpected "end if" and "next" statements, and if I comment them out then it doesn't map the printers from the groups I've created.  If tried everything my knowledge of vbscript will allow.

Thanks for your advice!
/Steve
LambtonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DansDadUKCommented:
There are too many bits commented out in your sample; these cause the unexpected "end if" and "next" errors.

Try:

on error resume next 
Dim objADSystemInfo, objUser, objMemberOf, objGroup, objGroupEnum, objNetwork, objPrinter 
dim i, bTroubleFlag 
 
Set objNetwork = CreateObject("Wscript.Network") 
 
'Get current user info from active directory
Set objADSystemInfo = CreateObject("ADSystemInfo")
'bind to current user in active directory
set objUser = GetObject("LDAP://" & objADSystemInfo.UserName) 
 
if objuser.description = "printer.trouble" then 
    bTroubleFlag = true 
    msgbox "Troubleshooting Printer Logon Script" 
end if 
 
Set objPrinter = objNetwork.EnumPrinterConnections 'Test to see if we have any printers mapped
If objPrinter.Count > 0 Then 
    'The Printer array is Printer name, printer path that is why it is step 2 
    for i=1 to objPrinter.Count Step 2 
        'test to make sure it is a network printer 
        if instr(objPrinter.Item(i),"\\") <> 0 then 
            if bTroubleFlag then 
                msgbox "Deleting:" & vbcrlf & objPrinter.Item(i) 
            end if 
            objNetwork.RemovePrinterConnection objPrinter.Item(i),true,true 
        end if 
    next 
end if 
 
 
'Get an array of group names that the user is a member of
 objMemberOf = objUser.MemberOf
 for Each objGroup in objMemberOf 
    'Test to see if it is a printer group. all printer groups should be in the same OU 
    if (instr(objGroup,"OU=Printer-Groups") <> 0) then 
        'Bind to the group to get is description. The description contain the path to the printer 
        set objGroupEnum = GetObject("LDAP://" & objGroup) 
        if bTroubleFlag then 
            msgbox "Adding:" & vbcrlf & "[" & objGroupEnum.name & "]" & vbcrlf & objGroupEnum.description 
        end if 
        objNetwork.AddWindowsPrinterConnection objGroupEnum.description 
        set objGroupEnum = nothing 
    end if 
next 
'Repeat as above for the default printer
for Each objGroup in objMemberOf 
    if (instr(objGroup,"OU=Printer-Default-Groups") <> 0) then 
        set objGroupEnum = GetObject("LDAP://" & objGroup) 
        if bTroubleFlag then 
            msgbox "Setting Default:" & vbcrlf & "[" & objGroupEnum.name & "]" & vbcrlf & objGroupEnum.description 
        end if 
        objNetwork.SetDefaultPrinter objGroupEnum.description 
        set objGroupEnum = nothing 
    end if
next
 
if bTroubleFlag then 
    msgbox "Printer Logon Script Finished" 
end if 

Open in new window


as a starting point.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LambtonAuthor Commented:
Great that works!  Thanks very much!  I see what it was, it was a weird line break that didn't copy over from the original correctly, thanks again!

If I add the users to the printer group it works like a charm, however if I add a group (the same user is a member of) instead to the printer group it doesn't map the printer.  Any ideas how I'd make the script do that so I don't have to add each of my 1500 users individually to each printer they need mapped?

Thanks again for finding that line in the wrong spot, really appreciate it!
/Steve
0
DansDadUKCommented:
>> ... works like a charm, however if I add a group (the same user is a member of) instead to the printer group it doesn't map the printer.  Any ideas ...

I'm retired and have no access to Active Drectory systems, so can't advise further on how to modify your script to cater for your requirement; sorry.
0
LambtonAuthor Commented:
That's OK - thanks for your initial assistance!  it's got the script working as designed at least.

Thanks again!
/Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.