Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting local printer default after mapping network printers

Status
Not open for further replies.

chriskdixon

IS-IT--Management
Aug 22, 2007
17
US
Dim objNetwork
Dim objPrinters
Dim ADSysInfo
Dim ComputerName

on error resume next

Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections
Set ADSysInfo = CreateObject("ADSystemInfo")
Set ComputerName = GetObject("LDAP://" & ADSysInfo.ComputerName)

If Instr(ADSysInfo.ComputerName, "OU=IT Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\it"
objNetwork.AddWindowsPrinterConnection "\\serverdc\server"
objNetwork.SetDefaultPrinter("\\serverdc\it")

ElseIf Instr(ADSysInfo.ComputerName, "OU=News Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\draft"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset1"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset2"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset3"
objNetwork.AddWindowsPrinterConnection "\\serverdc\desk"
objNetwork.AddWindowsPrinterConnection "\\serverdc\user1"
objNetwork.SetDefaultPrinter("\\serverdc\draft")

If Instr(ADSysInfo.ComputerName, "OU=Sports") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\sports"
objNetwork.SetDefaultPrinter("\\serverdc\sports")

End If

ElseIf Instr(ADSysInfo.ComputerName, "OU=Sales Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\sales"
objNetwork.AddWindowsPrinterConnection "\\serverdc\xerox"
objNetwork.SetDefaultPrinter("\\serverdc\sales")

ElseIf Instr(ADSysInfo.ComputerName, "OU=Business Office Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\business"
objNetwork.AddWindowsPrinterConnection "\\serverdc\check"
objNetwork.AddWindowsPrinterConnection "\\serverdc\xerox"
objNetwork.AddWindowsPrinterConnection "\\serverdc\business color laser"
objNetwork.SetDefaultPrinter("\\serverdc\business")

ElseIf Instr(ADSysInfo.ComputerName, "OU=Promotions Computers") Then

objNetwork.AddWindowsPrinterConnection "\\serverdc\promo"
objNetwork.AddWindowsPrinterConnection "\\serverdc\draft"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset1"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset2"
objNetwork.AddWindowsPrinterConnection "\\serverdc\qset3"
objNetwork.SetDefaultPrinter("\\serverdc\promo")

End If


Set WSHPrinters = objNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'Find local printers
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) <> "\\" Then
objNetwork.SetDefaultPrinter _
(WSHPrinters.Item(LOOP_COUNTER +1))
End If
Next

I tried this script and I get a ERROR on line 108 Char 1 Expected End
800A03F6
 
Is there something missing? or doing wrong?
ERROR on line 108 Char 1 Expected End
800A03F6


 
This is the Actually Code below

Dim objNetwork
Dim objPrinters
Dim ADSysInfo
Dim ComputerName

Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections
Set ADSysInfo = CreateObject("ADSystemInfo")
Set ComputerName = GetObject("LDAP://" & ADSysInfo.ComputerName)


If Instr(ADSysInfo.ComputerName, "OU=IT ") Then

objNetwork.AddWindowsPrinterConnection= "\\opprint\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\NeoProfen"
objNetwork.SetDefaultPrinter "\\server\PrinterName"


ElseIf Instr(ADSysInfo.ComputerName, "OU=Regulatory") Then

objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\Melperone"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\Melperone"


If Instr(ADSysInfo.ComputerName, "OU=Finance") Then
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

End if

If Instr(ADSysInfo.ComputerName, "OU=Accounting") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

ElseIf Instr(ADSysInfo.ComputerName, "OU=BusDev") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

If Instr(ADSysInfo.ComputerName, "OU=Marketing") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

End if

If Instr(ADSysInfo.ComputerName, "OU=Sales & Marketing") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

ElseIf Instr(ADSysInfo.ComputerName, "OU=Medical Affairs") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

If Instr(ADSysInfo.ComputerName, "OU=Operations") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

End if

If Instr(ADSysInfo.ComputerName, "OU=PV") Then

objNetwork.AddWindowsPrinterConnection="\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\Winstrol"

ElseIf Instr(ADSysInfo.ComputerName, "OU=Stats & Data Mgmt") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"
End If
Set WSHPrinters = objNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'Find local printers
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) <> "\\" Then
objNetwork.SetDefaultPrinter _
(WSHPrinters.Item(LOOP_COUNTER +1))
End If

NEXT
 
I seemed to had resolved my issue with Expected END but when the Script execute just Locally on my machine I have not yet applied it to AD and I know that I'm in a OU why is it not added the corresponding printers?
 
have an xml file or an ini file in this type of format

'global.ini
[MapPrinters]
GroupNameA=\\server\PrinterName1
GroupNameA=\\server\PrinterName2
GroupNameB=\\server\PrinterName1
GroupNameC=\\server\PrinterName4


+ read MapPrinter section of the xml or ini file into memory 'dict of array
+ put the user group membership and machine group membership into a dictionary object
+ compare MapPrinter dic/array against user and machine group membership dic object, build a list of unique printer to map....call MapPrinter Function passing the unique list of printers to map

wash and rinse and repeat above logic (and code) for MapDrive, StartThreads, WriteRegKeys, DisplayMeessages modules.

once tested you will never have to touch your code, you can document and get location staff to do the admim
 
Thanks mrmovie I just found of from a colleague that we do use OU and that if I were to search within Active Directory is Members Of so I have to re-structure the code. would any1 have knowledge on that?
 
SORRY for the bad English Repost


Thanks mrmovie I just found out from a colleague that we do not use OU and that search by Member Of within Active Directory so I have to re-structure the code. would any1 have knowledge on that?
 
if you use machine OU location for a string comparison to decide what to do then you would just need to change the bit which compares the target'd MapPrinters entries. instead of comparing against group membership do a string comparison
 
If Instr(ADSysInfo.ComputerName, "OU=PV") Then

I need to remove the OU statement and replace it to look elsewhere for the Security Group Can you illustrate for me.

For instance when I get my gpresults which is
USER SETTINGS
--------------
CN=Christopher Dixon,OU=Users,OU=Deerfield,DC=XXXX,DC=xxxxxx,DC=
com
Last time Group Policy was applied: 8/22/2007 at 4:17:58 PM
Group Policy was applied from: Dxxxxxx.xxxxxx.xxxxxxxx.com
Group Policy slow link threshold: 500 kbps

Applied Group Policy Objects
-----------------------------
Deerfield Logon
Default Domain Policy
Don't Allow IM Programs to Run
Redirect My Documents
Local Group Policy

The following GPOs were not applied because they were filtered out
-------------------------------------------------------------------
Prevent AutoUpdate of IE7
Filtering: Not Applied (Empty)

Disable Windows Firewall
Filtering: Not Applied (Empty)

The user is a part of the following security groups:

----------------------------------------------------
Domain Users
Everyone
BUILTIN\Administrators
BUILTIN\Users
NT AUTHORITY\INTERACTIVE
NT AUTHORITY\Authenticated Users
LOCAL
IT <-------------------------------

U:\>

I want it to see what group im in which is IT then Run the following
If Instr(ADSysInfo.ComputerName, "OU=IT ") Then

objNetwork.AddWindowsPrinterConnection= "\\opprint\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"
__________________________________________________
It have some issues to searching in AD does the following look ok.

Dim objNetwork<---------

Dim objPrinters<---------

Dim ADSysInfo<---------

Dim ComputerName<---------


on error resume next

Set objNetwork = CreateObject("WScript.Network")<---------

Set objPrinters = objNetwork.EnumPrinterConnections<---------

Set ADSysInfo = CreateObject("ADSystemInfo")<---------

Set ComputerName = GetObject("LDAP://" & ADSysInfo.ComputerName)<---------


 
ComputerName object i would suggest should be named objComputer

dont use On Error Resume Next.....unless you really have to
 
So mrmovie I should change Set ComputerName = GetObject("LDAP://" & ADSysInfo.ComputerName)

to

Set objComputer = GetObject("LDAP://" & ADSysInfo.Computer)

and change every If Instr(ADSysInfo.ComputerName, "OU=XX")
to
If Instr(ADSysInfo.Computer, "OU=XX")

what about the OU=XX will that remain OU.
 
This is the Program as of now. It Run but still did not install the corresponding printers

Dim objNetwork
Dim objPrinters
Dim ADSysInfo
Dim ComputerName
Dim objComputer

Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections
Set ADSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject("LDAP://" & ADSysInfo.ComputerName)


If Instr(ADSysInfo.ComputerName, "OU=IT ") Then

objNetwork.AddWindowsPrinterConnection= "\\opprint\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\NeoProfen"
objNetwork.SetDefaultPrinter "\\server\PrinterName"


ElseIf Instr(ADSysInfo.ComputerName, "OU=Regulatory") Then

objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\Melperone"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\Melperone"


If Instr(ADSysInfo.ComputerName, "OU=Finance") Then
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

End if

If Instr(ADSysInfo.ComputerName, "OU=Accounting") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

ElseIf Instr(ADSysInfo.ComputerName, "OU=BusDev") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

If Instr(ADSysInfo.ComputerName, "OU=Marketing") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

End if

If Instr(ADSysInfo.ComputerName, "OU=Sales & Marketing") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

ElseIf Instr(ADSysInfo.ComputerName, "OU=Medical Affairs") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

If Instr(ADSysInfo.ComputerName, "OU=Operations") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"

End if

If Instr(ADSysInfo.ComputerName, "OU=PV") Then

objNetwork.AddWindowsPrinterConnection="\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\Winstrol"

ElseIf Instr(ADSysInfo.ComputerName, "OU=Stats & Data Mgmt") Then

objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\PrinterName"
End If
Set WSHPrinters = objNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'Find local printers
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) <> "\\" Then
objNetwork.SetDefaultPrinter _
(WSHPrinters.Item(LOOP_COUNTER +1))
End If

NEXT
WScript.Quit
 
To add I also change all the WSHPrinter to objPrinter and I put a WScript.Echo "PrinterAdded" Shown Below and it did display but I checked my Printers and they were not added instead I logged into the Print Server and and under the printer and faxes it seemed to have added in there with example. HP1300 PDC6(DIXON) so it seems that whomever logs on to the server remotely it adds what ever printer to the printer server that on the local machine like it mirrors it.

in
If Instr(ADSysInfo.ComputerName, "OU=IT ") Then

objNetwork.AddWindowsPrinterConnection= "\\opprint\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\NeoProfen"
objNetwork.SetDefaultPrinter "\\server\PrinterName"
WScript.Echo "PrinterAdded"

 
It seems there is an overdose of info which contains confusing irrelevant ones. Let me see if we can clear some up.

[1] >I need to remove the OU statement and replace it to look elsewhere for the Security Group
So you don't want to check if the computer is in some ou, but instead to check if it is in some security groups.

[1.1] >what about the OU=XX will that remain OU.
If so, it will _not_ remin OU!

[2] If that is the case, and that "IT", "Regulatory", "Finance"... etc are the name of the security groups, then you have to do different things.

[3] Also that the computer can be member of different groups, so what do you want if that happens?...

 
1./1.1 Well I gather the gpresult from a different user today and the infomation came up has
CN=Persons Name,OU=Users,OU=Deerfield,DC=XXXX,DC=xxxxxx,DC=com
This User's belongs to these Security Groups
Domain Users
Everyone
BUILTIN\Administrators
BUILTIN\Users
NT AUTHORITY\INTERACTIVE
NT AUTHORITY\Authenticated Users
LOCAL
Finance<-------------------------------
So instead of referencing the OU I need it to search the users security group

I not a scriptor nor programmer but I will need some assist with setting me on the right path.

3. if they are a in two security group they both have the same printer mapped for both groups. nothing different from the two from the look of the setup here its a very small company
 
This is the same for every user
CN=Whomever,OU=Users,OU=Deerfield,DC=XXXX,DC=xxxxxx,DC=com
 
I need it to search within the Users Security Settings illustration below
____________________________
NOT COMPUTER SETTINGS
------------------
CN=DIXON,OU=Computers,OU=Deerfield,DC=xxxxxxxxx,DC=xxxxxxxx,DC=com
Last time Group Policy was applied: 8/24/2007 at 1:36:15 PM
Group Policy was applied from: xxxxx.xxxxxxxxx.xxxxxxxxxx.com
Group Policy slow link threshold: 500 kbps

Applied Group Policy Objects
-----------------------------
Default Domain Policy
Prevent AutoUpdate of IE7
Disable Windows Firewall
Local Group Policy

The following GPOs were not applied because they were filtered out
-------------------------------------------------------------------
Don't Allow IM Programs to Run
Filtering: Not Applied (Empty)

Redirect My Documents
Filtering: Not Applied (Empty)

Deerfield Logon
Filtering: Not Applied (Empty)

The computer is a part of the following security groups:
--------------------------------------------------------
BUILTIN\Administrators
Everyone
BUILTIN\Users
NT AUTHORITY\NETWORK
NT AUTHORITY\Authenticated Users
DIXON$
Domain Computers


USER SETTINGS
--------------
CN=Christopher Dixon,OU=Users,OU=xxxxxxxxxxx,DC=xxxxxxxxxx,DC=xxxxxxxxxx,DC=
com
Last time Group Policy was applied: 8/24/2007 at 1:08:12 PM
Group Policy was applied from: ovation8.corporate.ovationpharma.com
Group Policy slow link threshold: 500 kbps

Applied Group Policy Objects
-----------------------------
Deerfield Logon
Default Domain Policy
Don't Allow IM Programs to Run
Redirect My Documents
Local Group Policy

The following GPOs were not applied because they were filtered out
-------------------------------------------------------------------
Prevent AutoUpdate of IE7
Filtering: Not Applied (Empty)

Disable Windows Firewall
Filtering: Not Applied (Empty)

The user is a part of the following security groups:
----------------------------------------------------
Domain Users
Everyone
BUILTIN\Administrators
BUILTIN\Users
NT AUTHORITY\INTERACTIVE
NT AUTHORITY\Authenticated Users
LOCAL
IT<------------------ This what I need it to grab or search for.
xxxxxxxx
xxxxxxxx
 
My understanding would be:
[1] A: positive
[2] A: positive
[3] A: those groups being checked would be unique, users/computers won't be member of multiple groups being checked upon (It does not exclude them being member of some other groups (built-in or not) not being checked upon.)

[4] In that case, try this as a start.
[tt]
Set objNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject("LDAP://" & ADSysInfo.ComputerName)
objComputer.getInfo
agroups=objComputer.getEx("memberOf")
for each sgroup in agroup
scn=getobject("LDAP://" & sgroup).cn
select case ucase(scn)
case "CN=IT"
objNetwork.AddWindowsPrinterConnection= "\\opprint\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection= "\\server\NeoProfen"
objNetwork.SetDefaultPrinter "\\server\PrinterName"
[blue]exit for[/blue]
case "CN=REGULATORY"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\Melperone"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.AddWindowsPrinterConnection "\\server\PrinterName"
objNetwork.SetDefaultPrinter "\\server\Melperone"
[blue]exit for[/blue]
[blue]'etc etc on all the cases of interest[/blue]
case else
'do nothing else??? if not in any of the above group
end select
next
'etc etc
[/tt]
ps: I have looked into your printer mapping, just copied and pasted one and two of your cases shown.
 
Amendment
ps: I have [red]not[/red] looked into your printer mapping, just copied and pasted one and two of your cases shown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top