network printer mapping

Dear experts,

I have a vbs code that maps network printers. Is it possible to adapt the code (in vbs and not in powershell)
with the following points please:

-1 The script must check 2 AD groups:
    --one for user = UsrGroup
    --one for computers = CompGroup
- 2 verifying if the printers are already mapped

Scenario:
If user A is member of group "UsrGroup" and the computer where the user is logged on is also member of "Compgroup". The vbs script will map all the network printers that are defined in the code of vbs file.
If the 2 conditions are not done, then it will not map any network printers. Also if the script finds that the same printers are mapped it has to do nothing.

Thanks in advance

 
Dim multiPrinter, UNCpath1, UNCpath2
UNCpath1 = "\\Server1\printer1"
UNCpath2 = "\\Server2\printer2"
Set multiPrinter = CreateObject("WScript.Network") 
multiPrinter.AddWindowsPrinterConnection UNCpath1
multiPrinter.AddWindowsPrinterConnection UNCpath2
WScript.Echo "Your printer is mapped from : " & UNCpath1  _
& "and from : " & UNCpath2
WScript.Quit
' End of example VBScript

Open in new window

DigitBoyAsked:
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.

DigitBoyAuthor Commented:
Can this code to be adapted to the points that I mentioned?
Option Explicit
On Error Resume Next

Dim objShell : Set objShell = WScript.CreateObject(“WScript.Shell”)
Dim objNetwork : Set objNetwork = WScript.CreateObject(“WScript.Network”)
Dim objGroupList
Dim enumPrinters : Set enumPrinters = objNetwork.EnumPrinterConnections
Dim strWorkDir : strWorkDir = ObjShell.ExpandEnvironmentStrings(“%temp%”)
Dim strUser : strUser = objNetwork.UserName
Dim strDomain : strDomain = objNetwork.UserDomain
Dim strGroup
Dim objUser : Set objUser = GetObject(“WinNT://” & strDomain & “/” & strUser & “,user”)
Dim intCounter
Dim localPrinter : Set localPrinter = False
Dim strPrintServer

‘Set the name of the printserver
strPrintServer = “\\yourprintserver” 

‘Set script working directory to user %temp%

objShell.CurrentDirectory = strWorkDir

‘Remove all current network printers

‘For intCounter = 1 to enumPrinters.Count -1 step 2
‘objNetwork.RemovePrinterConnection enumPrinters.Item(intCounter), true
‘Next

‘Map standard printers for all users if you want these printers to be available for all users
‘Change the name printer01, 02 etc. to your printer sharename

‘objNetwork.AddWindowsPrinterConnection strPrintServer & “\printer01¿
‘objNetwork.AddWindowsPrinterConnection strPrintServer & “\printer02¿
‘objNetwork.AddWindowsPrinterConnection strPrintServer & “\printer03¿

‘Set standard default printer

‘objNetwork.SetDefaultPrinter strPrintServer & “\printer01¿

‘Check if a local printer exists on LPT or USB, if so set this printer to default

For intCounter = 0 to enumPrinters.Count -1 step 2
if Left(enumPrinters(intCounter),3)=”LPT” OR Left(enumPrinters(intCounter),3)=”USB” OR Left(enumPrinters(intCounter),3)=”DOT” Then
if Left(enumPrinters(intCounter+1),7)=”Acrobat” Then
Else
objNetwork.SetDefaultPrinter enumPrinters(intCounter+1)
localPrinter = True
end if
end if
Next

‘Map additional printers and change default printer if no local printer based on group membership.
‘This will check the group membership in the Active Directory.
‘If a user is member of that group it will add the printer.
‘If the user is member of the printer-default group it will set that printer as default printer.
‘Change the AD group name to your group names (The printersharename is the most logical).

strGroup = “printer01¿
if IsMember(strGroup) Then
objNetwork.AddWindowsPrinterConnection strPrintServer & “\printer01¿
if localPrinter = false Then
strGroup = “printer01-default”
if IsMember(strGroup) Then
objNetwork.SetDefaultPrinter strPrintServer & “\printer01¿
end if
end if
end if

strGroup = “printer02¿
if IsMember(strGroup) Then
objNetwork.AddWindowsPrinterConnection strPrintServer & “\printer02¿
if localPrinter = false Then
strGroup = “printer02-default”
if IsMember(strGroup) Then
objNetwork.SetDefaultPrinter strPrintServer & “\printer02¿
end if
end if
end if

strGroup = “printer03¿
if IsMember(strGroup) Then
objNetwork.AddWindowsPrinterConnection strPrintServer & “\printer03¿
if localPrinter = false Then
strGroup = “printer03-default”
if IsMember(strGroup) Then
objNetwork.SetDefaultPrinter strPrintServer & “\printer03¿
end if
end if
end if

‘For use with Windows 2003 built in fax server (Client must have XP fax client installed)
‘strGroup = “Fax Group”
‘if IsMember(strGroup) Then
‘objNetwork.AddWindowsPrinterConnection “\\FAXSERVER\Fax Printer$”
‘end if

‘Cleanup

Set objGroupList = Nothing
Set objUser = Nothing

‘Function to test group membership

Function IsMember(strGroup)

If IsEmpty(objGroupList) Then
Call LoadGroups
End If

IsMember = objGroupList.Exists(strGroup)

End Function

‘Subroutine to load user’s groups into dictionary object

Sub LoadGroups

Dim objGroup

Set objGroupList = CreateObject(“Scripting.Dictionary”)
objGroupList.CompareMode = vbTextCompare
For Each objGroup In objUser.Groups
objGroupList(objGroup.name) = True
Next

Set objGroup = Nothing

End Sub

Open in new window

0
RobSampsonCommented:
Hi, you can try a script like this.

"UserGroupA", "CompGroupA", and this line
arrPrintersToMap = Array("\\server1\printer1", "\\server1\printer2")

are all you should need to change.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objWinntComp = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.ComputerName & ",computer")
Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")
Set dctExistingPrinters = CreateObject("Scripting.Dictionary")
dctExistingPrinters.CompareMode = vbTextCompare
colPrinters = objNetwork.EnumPrinterConnections
For intNum = 0 To colPrinters.Count -1 Step 2
	dctExistingPrinters.Add LCase(colPrinters(intNum + 1)), 0
Next

If IsUserMemberOfGroup(objNetwork.UserDomain, objWinntUser, "UserGroupA") = True And IsCompMemberOfGroup(objNetwork.UserDomain, objWinntComp, "CompGroupA") = True Then
	arrPrintersToMap = Array("\\server1\printer1", "\\server1\printer2")
	For Each strPrinter In arrPrintersToMap
		If dctExistingPrinters.Exists(LCase(strPrinter)) = False Then
			AddobjNetwork.AddWindowsPrinterConnection strPrinter
		Else
			WScript.Echo strPrinter & " is already mapped."
		End If
	Next
End If	

Function IsCompMemberOfGroup(strUserDomain, objComp, strGroup) 'the user is a member of a specified group
	IsMemberCompOfGroup = False
	Dim objGroup
	On Error Resume Next
	Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")
	If Err.Number Then
		IsCompMemberOfGroup = "Error"
	Else
		IsCompMemberOfGroup = objGroup.IsMember(objComp.ADsPath & "$")
	End If
End Function

Function IsUserMemberOfGroup(strUserDomain, objUser, strGroup) 'the user is a member of a specified group
	IsUserMemberOfGroup = False
	Dim objGroup
	On Error Resume Next
	Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")
	If Err.Number Then
		IsUserMemberOfGroup = "Error"
	Else
		IsUserMemberOfGroup = objGroup.IsMember(objUser.ADsPath)
		'MsgBox objUser.ADsPath
	End If
End Function

Open in new window

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
RobSampsonCommented:
My recommendation is to accept my comment ID 37042881.

Rob.
0
hdhondtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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.