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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a Default printer depending an group Membership 3

Status
Not open for further replies.

NTman2000

MIS
Sep 26, 2006
15
GB
I have cobbled to gether this script to allow me to automatically use 2003 AD (we have Win2k PC's) to map the default printer based on membership of 4 distribution groups
1st Floor User, 2ndFloor User etc

' New Logon script
On Error Resume Next

'4 initial object creation
Dim objNetwork, objGroup , UserObj
Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
DomainString = "Company.co.uk"
UserString = WSHNetwork.UserName

set objNetwork = WScript.createObject("WScript.Network")
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'14 Welcome Message
Dim strDomain, strUser
strDomain = ObjNetwork.UserDomain
strUser = ObjNetwork.UserName

MsgBox "Welcome to the " & strDomain & " Network, " & strUser & "!"

'21 Drive Mapping

objNetwork.MapNetworkDrive "G:", "\\Servername\Data1"
objNetwork.MapNetworkDrive "H:", "\\Servername\Accounts"
objNetwork.MapNetworkDrive "I:", "\\Servername\Documents"
objNetwork.MapNetworkDrive "J:", "\\Servername\Personnel"
objNetwork.MapNetworkDrive "K:", "\\Servername\Trescray Utils"
objNetwork.MapNetworkDrive "L:", "\\Servername\Electrical"
objNetwork.MapNetworkDrive "M:", "\\Servername\Archive"
objNetwork.MapNetworkDrive "N:", "\\Servername\Fuji"
objNetwork.MapNetworkDrive "O:", "\\Servername\Homedirs\" & strUser
objNetwork.MapNetworkDrive "P:", "\\Servername\Apps"
objNetwork.MapNetworkDrive "T:", "\\Servername\TestResults"
objNetwork.MapNetworkDrive "V:", "\\Servername\Fiber"
objNetwork.MapNetworkDrive "X:", "\\Servername\Fluor"
objNetwork.MapNetworkDrive "Z:", "\\Servername\Admin Documentation"

'38 Printer Mapping

objNetwork.AddWindowsPrinterConnection "\\Servername\First Floor C450", "Konica Minolta C450 PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername2\Second Floor C450", "Konica Minolta C450 PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername2\First Floor C450", "Konica Minolta C450 PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername\2nd Floor C450", "Konica Minolta C450 PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\First Floor - Laserjet 2200dn", "HP Laserjet 2200 Series PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername2\First Floor - Laserjet 2200dn", "HP Laserjet 2200 Series PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\Second Floor - Laserjet 4050", "HP Laserjet 4050 Series PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername2\Second Floor - LaserJet 4050", "HP Laserjet 4050 Series PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\StoresLJ3200", "HP Laserjet 4M"
objNetwork.AddWindowsPrinterConnection "\\Servername2\Stores Department - LaserJet 3200", "HP Laserjet 4M"

'55 Now check for group memberships and map appropriate drives
For Each GroupObj In UserObj.Groups

Select Case GroupObj.Name
'59 Check for group memberships and take needed action
Case "1stFloor User"
objNetwork.SetDefaultPrinter "\\Servername\First Floor C450"
Case "2ndFloor User"
objNetwork.SetDefaultPrinter "\\Servername\Second Floor C450"
Case "3rdFloor User"
objNetwork.SetDefaultPrinter "\\Servername\Second Floor C450"
Case "CAD User"
objNetwork.AddWindowsPrinterConnection "\\Servername\CAD Department - DesignJet 800", "HP DesignJet 800 42 by HP"
objNetwork.AddWindowsPrinterConnection "\\Servername2\CAD Department - DesignJet 800", "HP DesignJet 800 42 by HP"

End Select

Next

'74 Synchronizes the time with Main AD Server our NTP Server


The drives are mapped the printers added But I cannot get teh default printer section lines 55 -73 to select a group from AD by which to decide what floor they are on

Can anyody see what I have missed please ?
 
[1] For conceptual clarity, you have to understand WSHNetwork and objNetwork are the same object. So even you're prepared to waste some memory, it is symptomatic that you might not understand what it is involved.

[2] What it is involved is that starting from lines 55-73, it is where objUser begins to matter, no anywhere before.

[3] Is there any reason you hard code DomainString when you establish WSHNetwork and hence objUser, and refuse to use WSHNetwork.UserDomain as you use in the off-core function of welcoming message?
 
tsuji

Must admit to being a newby trying to get things done quick without really knowing it all

1. No I did not realsise that the WSHNetwork and objNetwork are the same so will need to edit this to get rid on one

2. Ok understand that

3. Again really a newby with scripts so no reason other than I thought that is what you do! I am trying to say if your on the 1st floor then this is your default printer if you are on the 2nd floor then that one is your printer
 
[4] In that case could you try to use userDomain here.
> Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
[tt] Set UserObj = GetObject("WinNT://" & [blue]WSHNetwork.UserDomain[/blue] & "/" & UserString)[/tt]

[5] Comment out the "on error resume next" to see what error turns up.
 
I'll add that you shouldn't be attempting to sync time via a script as mentioned in line 74. This is an automatic function of AD.

Pat Richard, MCSE MCSA:Messaging CNA
Microsoft Exchange MVP
Want to know how email works? Read for yourself -
 
Ok so I editied the line as advised and I have added a line at the end as below
MsgBox "Groupobj is" & GroupObj

This goes through without any errors but the msg box comes up empty and the default printer is not set

so I can see that the error is that the variable "GroupObj" is not being populated from AD but why.. perhaps you can tell me

Oh yes sorry about the set time, as I said at the begining this is a canablised script that I am trying to get to work as I want it I know the proper way is to write is from scratch but I don't really know how yet:)
 
NTman2000,

You might want to look at my FAQ which offers the answers you are looking for with taking actions based on group memberships. faq329-5798

Be aware that the Select Case statement is case sensitive. I suggest forcing either upper case or lower case with the use of UCASE or LCASE.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
If that part is fine, you should echo the .name of each item in the collection .groups return. Your select case check against it, make sure they would match at all.
[tt]
For Each GroupObj In UserObj.Groups
wscript.echo GroupObj.name & vbcrlf & GroupObject.adspath
Next
[/tt]
see what shows up.

 
I have not done the case statement but I did check carefully that the case 1stFloor User was exactly as it appears in AD
I have added the lines you mentioned For Next loop but nothing appears at all!
 
Then .groups return empty. That is the problem only you can investigate. You know at least the problem.
 
Mark,

atually it is just Company I ave tried altering this but to no avail had alook at your FAQ nice a clear will start rereading this amd my script. I think that the proble i sthat althought I tell it what to do if you ar eon the 1st floor the part that tells the script which floor you are on does not work
 
One last note: Have you really comment out "on error resume next" [2]? or you didn't understand what it means?
[tt] ' New Logon script
[red]'[/red]On Error Resume Next
[/tt]
 
I see one problem here... What happens if you are a member of multiple "floor" groups? Under the current script, the last group checked would get the default printer. An easy way to get around this is to establish a counter that verifies that the user isn't part of multiple groups. Simply establish the counter before getting into the For...Next statement.

Code:
strCounter = 0 ' Initialize counter
For Each GroupObj In UserObj.Groups

	Select Case UCase(GroupObj.Name) ' Convert to Upper Case for consistency

		Case "1STFLOOR USER"
			If strCounter > 0 Then
				WScript.Echo "User is member of multiple floors"
				Exit For ' Causes For...Next loop to end
			Else
				strCounter = strCounter + 1 ' Increment counter for next loop
			End If
			objNetwork.SetDefaultPrinter "\\Servername\First Floor C450"

	End Select

Next

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
tsuji
Yes I have remmed out the error ignore section the script looks like this now but does'nt like PSC scripting (or at least my attempt to use it)
' New Logon script
' On Error Resume Next

'4 initial object creation
Dim objNetwork, objGroup , UserObj
Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
DomainString = "DomainName"
UserString = WSHNetwork.UserName

set objNetwork = WScript.createObject("WScript.Network")
Set UserObj = GetObject("WinNT://" & WSHNetwork.UserDomain & "/" & UserString)
' Set UserObj = GetObject("WinNT://" & WSHNetwork.UserDomain & "/" & strUser)

'15 Welcome Message
Dim strDomain, strUser
strDomain = ObjNetwork.UserDomain
strUser = ObjNetwork.UserName

MsgBox "Welcome to the " & strDomain & " Network, " & strUser & "!"

'22 Drive Mapping

objNetwork.MapNetworkDrive "G:", "\\Servername\Data1"
objNetwork.MapNetworkDrive "H:", "\\Servername\Accounts"
objNetwork.MapNetworkDrive "I:", "\\Servername\Documents"
objNetwork.MapNetworkDrive "J:", "\\Servername\Personnel"
objNetwork.MapNetworkDrive "K:", "\\Servername\DomainName Utils"
objNetwork.MapNetworkDrive "L:", "\\Servername\Electrical"
objNetwork.MapNetworkDrive "M:", "\\Servername\Archive"
objNetwork.MapNetworkDrive "N:", "\\Servername\Fuji"
objNetwork.MapNetworkDrive "O:", "\\Servername\Homedirs\" & strUser
objNetwork.MapNetworkDrive "P:", "\\Servername\Apps"
objNetwork.MapNetworkDrive "T:", "\\Servername\TestResults"
objNetwork.MapNetworkDrive "V:", "\\Servername\Fiber"
objNetwork.MapNetworkDrive "X:", "\\Servername\Fluor"
objNetwork.MapNetworkDrive "Z:", "\\Servername\Admin Documentation"

'39 Adds the complete set of office Printers

objNetwork.AddWindowsPrinterConnection "\\Servername\First Floor C450", "Konica Minolta C450 PCL"
objNetwork.AddWindowsPrinterConnection "\\corenas2\Second Floor C450", "Konica Minolta C450 PCL"

objNetwork.AddWindowsPrinterConnection "\\corenas2\First Floor C450", "Konica Minolta C450 PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername\2nd Floor C450", "Konica Minolta C450 PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\First Floor - Laserjet 2200dn", "HP Laserjet 2200 Series PCL"
objNetwork.AddWindowsPrinterConnection "\\corenas2\First Floor - Laserjet 2200dn", "HP Laserjet 2200 Series PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\Second Floor - Laserjet 4050", "HP Laserjet 4050 Series PCL"
objNetwork.AddWindowsPrinterConnection "\\Corenas2\Second Floor - LaserJet 4050", "HP Laserjet 4050 Series PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\StoresLJ3200", "HP Laserjet 4M"
objNetwork.AddWindowsPrinterConnection "\\corenas2\Stores Department - LaserJet 3200", "HP Laserjet 4M"

'56 Now check for group memberships and select the cor
For Each GroupObj In UserObj.Groups

Select Case GroupObj.Name
'60 Check for group memberships and take needed action

strCounter = 0 ' Initialize counter
For Each GroupObj In UserObj.Groups

Select Case UCase(GroupObj.Name) ' Convert to Upper Case for consistency

Case "1STFLOOR USERS"
If strCounter > 0 Then
WScript.Echo "User is member of multiple floors"
Exit For ' Causes For...Next loop to end
Else
strCounter = strCounter + 1 ' Increment counter for next loop
End If
objNetwork.SetDefaultPrinter "\\Servername\First Floor C450"
Case "2NDFLOOR USERS"
If strCounter > 0 Then
WScript.Echo "User is member of multiple floors"
Exit For ' Causes For...Next loop to end
Else
strCounter = strCounter + 1 ' Increment counter for next loop
End If
objNetwork.SetDefaultPrinter "\\Servername\Second Floor C450"
Case "3RDFLOOR USERS"
If strCounter > 0 Then
WScript.Echo "User is member of multiple floors"
Exit For ' Causes For...Next loop to end
Else
strCounter = strCounter + 1 ' Increment counter for next loop
End If
objNetwork.SetDefaultPrinter "\\Servername\Second Floor C450"
Case "CAD USERS"
If strCounter > 0 Then
WScript.Echo "User is member of multiple floors"
Exit For ' Causes For...Next loop to end
Else
strCounter = strCounter + 1 ' Increment counter for next loop
End If
objNetwork.AddWindowsPrinterConnection "\\Servername\CAD Department - DesignJet 800", "HP DesignJet 800 42 by HP"
objNetwork.AddWindowsPrinterConnection "\\corenas2\CAD Department - DesignJet 800", "HP DesignJet 800 42 by HP"

End Select

Next

' 104 Testing that the groups have entries
MsgBox "Groupobj is" & GroupObj
MsgBox "UserObject is" & UserObj

It errors now on line 63 Char 19 "Invalid 'for' loop control variable.

Thanks Guys for your continuing patience with me

Andrew
 
You have duplicated the For Each and Select

'56 Now check for group memberships and select the cor
For Each GroupObj In UserObj.Groups

Select Case GroupObj.Name
'60 Check for group memberships and take needed action

strCounter = 0 ' Initialize counter
For Each GroupObj In UserObj.Groups

Select Case UCase(GroupObj.Name) ' Convert to Upper Case for consistency
Delete lines 56-62.

Unless you are adding users to multiple floor groups which seems it would defeat the purpose of that group, the issue PSC raised should not be an issue.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Mark,

Silly me I posted and though No your getting too lazy and I found that myself (I think that I am finally learning something) but even though I have remmed out lines 56 -62 this still will not function I get the drives mapped and the printers added but it still does not map any printer as the default ?

Andrew
 
I was simply suggesting that administrators, like everyone else, can make mistakes. My suggestion could save hours of troubleshooting why a user is getting the wrong default printer.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Here's some cleaned up code with comments on why code was moved or deleted...

[green]' On Error Resume Next

'3 initial object creation[/green]
Dim strDomain, strUser [green]' (Declare variables at top)[/green]
Dim WSHNetwork, objGroup , UserObj
Set WSHShell = CreateObject("WScript.Shell")
[green]' Set WSHNetwork = CreateObject("WScript.Network") ' (Redundant to line 12)[/green]
set objNetwork = WScript.createObject("WScript.Network")
DomainString = objNetwork.UserDomain [green]' (Used Network Object method from line 19) "DomainName"[/green]
UserString = objNetwork.UserName [green]' (Used network object method from line 20)[/green]

[green]' set objNetwork = WScript.createObject("WScript.Network") ' (Moved to line 8)
' Set UserObj = GetObject("WinNT://" & WSHNetwork.UserDomain & "/" & UserString)
' (Used existing objNetwork instead of WSHNetwork from line 7)[/green]
Set UserObj = GetObject("WinNT://" & objNetwork.UserDomain & "/" & UserString)

[green]'15 Welcome Message
' Dim strDomain, strUser ' (Moved to Line 4)
' strDomain = ObjNetwork.UserDomain ' (Redundant to line 9)
' strUser = ObjNetwork.UserName ' (Redundant to line 10)

' MsgBox "Welcome to the " & strDomain & " Network, " & strUser & "!"
' (Used existing variables instead of line 19 and 20 variables)[/green]
MsgBox "Welcome to the " & DomainString & " Network, " & UserString & "!"

[green]'26 Drive Mapping (Commented out lines to troubleshoot For...Next below)
' objNetwork.MapNetworkDrive "G:", "\\Servername\Data1"
' objNetwork.MapNetworkDrive "H:", "\\Servername\Accounts"
' objNetwork.MapNetworkDrive "I:", "\\Servername\Documents"
' objNetwork.MapNetworkDrive "J:", "\\Servername\Personnel"
' objNetwork.MapNetworkDrive "K:", "\\Servername\DomainName Utils"
' objNetwork.MapNetworkDrive "L:", "\\Servername\Electrical"
' objNetwork.MapNetworkDrive "M:", "\\Servername\Archive"
' objNetwork.MapNetworkDrive "N:", "\\Servername\Fuji"
' objNetwork.MapNetworkDrive "O:", "\\Servername\Homedirs\" & strUser
' objNetwork.MapNetworkDrive "P:", "\\Servername\Apps"
' objNetwork.MapNetworkDrive "T:", "\\Servername\TestResults"
' objNetwork.MapNetworkDrive "V:", "\\Servername\Fiber"
' objNetwork.MapNetworkDrive "X:", "\\Servername\Fluor"
' objNetwork.MapNetworkDrive "Z:", "\\Servername\Admin Documentation"
'41 Adds the complete set of office Printers (Commented out lines to troubleshoot For...Next below)
' objNetwork.AddWindowsPrinterConnection "\\Servername\First Floor C450", "Konica Minolta C450 PCL"
' objNetwork.AddWindowsPrinterConnection "\\corenas2\Second Floor C450", "Konica Minolta C450 PCL"
' objNetwork.AddWindowsPrinterConnection "\\corenas2\First Floor C450", "Konica Minolta C450 PCL"
' objNetwork.AddWindowsPrinterConnection "\\Servername\2nd Floor C450", "Konica Minolta C450 PCL"
' objNetwork.AddWindowsPrinterConnection "\\Servername\First Floor - Laserjet 2200dn", "HP Laserjet 2200 Series PCL"
' objNetwork.AddWindowsPrinterConnection "\\corenas2\First Floor - Laserjet 2200dn", "HP Laserjet 2200 Series PCL"
' objNetwork.AddWindowsPrinterConnection "\\Servername\Second Floor - Laserjet 4050", "HP Laserjet 4050 Series PCL"
' objNetwork.AddWindowsPrinterConnection "\\Corenas2\Second Floor - LaserJet 4050", "HP Laserjet 4050 Series PCL"
' objNetwork.AddWindowsPrinterConnection "\\Servername\StoresLJ3200", "HP Laserjet 4M"
' objNetwork.AddWindowsPrinterConnection "\\corenas2\Stores Department - LaserJet 3200", "HP Laserjet 4M"

'53 Now check for group memberships and select the cor[/green]
strCounter = 0 [green]' Initialize counter[/green]
For Each GroupObj In UserObj.Groups
WScript.Echo UCase(GroupObj.Name) [green]' (Insert echo command for troubleshooting)[/green]

Select Case UCase(GroupObj.Name) [green]' Convert to Upper Case for consistency
'59 Check for group memberships and take needed action

' strCounter = 0 ' Move to line 54
' For Each GroupObj In UserObj.Groups ' (Redundant to line 55)

' Select Case UCase(GroupObj.Name) ' (Redundant to line 58)[/green]

Case "1STFLOOR USERS"
If strCounter > 0 Then
WScript.Echo "User is member of multiple floors"
Exit For [green]' Causes For...Next loop to end[/green]
Else
strCounter = strCounter + 1 [green]' Increment counter for next loop[/green]
End If
objNetwork.SetDefaultPrinter "\\Servername\First Floor C450"

Case "2NDFLOOR USERS"
If strCounter > 0 Then
WScript.Echo "User is member of multiple floors"
Exit For [green]' Causes For...Next loop to end[/green]
Else
strCounter = strCounter + 1 [green]' Increment counter for next loop[/green]
End If
objNetwork.SetDefaultPrinter "\\Servername\Second Floor C450"

Case "3RDFLOOR USERS"
If strCounter > 0 Then
WScript.Echo "User is member of multiple floors"
Exit For [green]' Causes For...Next loop to end[/green]
Else
strCounter = strCounter + 1 [green]' Increment counter for next loop[/green]
End If
objNetwork.SetDefaultPrinter "\\Servername\Second Floor C450"

Case "CAD USERS"
[green]' (Remarked out following 6 lines because "CAD Users" group doesn't Select
' a default printer)
' If strCounter > 0 Then
' WScript.Echo "User is member of multiple floors"
' Exit For ' Causes For...Next loop to end
' Else
' strCounter = strCounter + 1 ' Increment counter for next loop
' End If[/green]
objNetwork.AddWindowsPrinterConnection "\\Servername\CAD Department - DesignJet 800", "HP DesignJet 800 42 by HP"
objNetwork.AddWindowsPrinterConnection "\\corenas2\CAD Department - DesignJet 800", "HP DesignJet 800 42 by HP"

End Select

Next

[green]' 109 Testing that the groups have entries
' MsgBox "Groupobj is" & GroupObj ' (This line caused script to crash)
' MsgBox "UserObject is" & UserObj ' (This line caused script to crash) [/green]


Good Luck!

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
What can I say but thanks, this now works for me and I would like to take the opportunity to thank you all for adding comments but most of all PSC who actually rewrote this for me as you can see above - Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top